使用opencv的android Houghlines

使用opencv的android Houghlines,android,opencv,opencv3.0,Android,Opencv,Opencv3.0,我正在尝试使用opencv3对四边形对象进行透视校正。我设法显示了这些行,还使用Imgproc.HoughLinesP()实现了Houghlines,并尝试使用Imgproc.lines()突出显示这些行,但输出没有成功。下面是我的代码,我还附上了我的输出图像 请让我知道发生了什么错误以及应该做什么 Mat initImg; // initial image Mat greyImg; // converted to grey Mat lines = new Mat(); int thresho

我正在尝试使用opencv3对四边形对象进行透视校正。我设法显示了这些行,还使用
Imgproc.HoughLinesP()
实现了Houghlines,并尝试使用
Imgproc.lines()
突出显示这些行,但输出没有成功。下面是我的代码,我还附上了我的输出图像

请让我知道发生了什么错误以及应该做什么

Mat initImg; // initial image
Mat greyImg; // converted to grey

Mat lines = new Mat();
int threshold = 50;
int minLineSize = 20;
int lineGap = 10;

initImg = Imgcodecs.imread(imgLoc, 1);
greyImg = new Mat();
Imgproc.cvtColor(initImg, greyImg, Imgproc.COLOR_BGR2GRAY);
Bitmap bitm = Bitmap.createBitmap(greyImg.cols(), greyImg.rows(),Bitmap.Config.ARGB_8888);

Imgproc.blur(greyImg, greyImg, new Size(3.d, 3.d));
Imgproc.adaptiveThreshold(greyImg, greyImg, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);

Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
        minLineSize, lineGap);
// lines returns rows x columns and rows is always 1. I dont know why please help me to understand
for (int x = 0; x < lines.cols(); x++) {

    double[] vec = lines.get(0, x);
    double[] val = new double[4];

    double x1 = vec[0],
            y1 = vec[1],
            x2 = vec[2],
            y2 = vec[3];

    System.out.println(TAG+"Coordinates: x1=>"+x1+" y1=>"+y1+" x2=>"+x2+" y2=>"+y2);
    Point start = new Point(x1, y1);
    Point end = new Point(x2, y2);

    Imgproc.line(greyImg, start, end, new Scalar(0,255, 0, 255), 3);

}

Utils.matToBitmap(greyImg, bitm);

if(bitm!=null){
    Toast.makeText(getApplicationContext(), "Bitmap not null", Toast.LENGTH_SHORT).show();
    iv.setImageBitmap(bitm);
}else{
    Toast.makeText(getApplicationContext(), "Bitmap null", Toast.LENGTH_SHORT).show();
}
Mat initImg;//初始图像
Mat greyImg;//变成灰色
垫线=新垫();
int阈值=50;
int minLineSize=20;
int-lineGap=10;
initImg=Imgcodecs.imread(imgLoc,1);
greyImg=新材料();
Imgproc.cvt颜色(初始、灰色、Imgproc.COLOR\u bgr2灰色);
Bitmap bitm=Bitmap.createBitmap(greyImg.cols(),greyImg.rows(),Bitmap.Config.ARGB_8888);
Imgproc.blur(greyImg,greyImg,新尺寸(3.d,3.d));
Imgproc.adaptiveThreshold(greyImg,greyImg,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY_INV,15,4);
Imgproc.HoughLinesP(greyImg,lines,1,Math.PI/180,阈值,
最小线尺寸、线间距);
//行返回行x列,行始终为1。我不知道为什么请帮我理解
对于(int x=0;x”+x1+“y1=>”+y1+“x2=>”+x2+“y2=>”+y2);
点开始=新点(x1,y1);
点端点=新点(x2,y2);
Imgproc.line(greyImg,开始,结束,新标量(0255,0255),3);
}
Utils.matToBitmap(greyImg,bitm);
如果(位!=null){
Toast.makeText(getApplicationContext(),“位图不为空”,Toast.LENGTH_SHORT.show();
iv.设置图像位图(bitm);
}否则{
Toast.makeText(getApplicationContext(),“位图为空”,Toast.LENGTH_SHORT.show();
}
我的输出:


我终于找到了时间线。在灰色图像中没有显示houghlines,我在灰色图像中提取了houghlines,但在彩色图像中绘制了houghlines,它工作了

Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
                minLineSize, lineGap);

        for (int x = 0; x < lines.rows(); x++)
        {
            double[] vec = lines.get(x, 0);
            double x1 = vec[0],
                    y1 = vec[1],
                    x2 = vec[2],
                    y2 = vec[3];
            Point start = new Point(x1, y1);
            Point end = new Point(x2, y2);
            double dx = x1 - x2;
            double dy = y1 - y2;

            double dist = Math.sqrt (dx*dx + dy*dy);

            if(dist>300.d)  // show those lines that have length greater than 300
                Imgproc.line(initImg, start, end, new Scalar(0,255, 0, 255),5);// here initimg is the original image.

        }
Imgproc.HoughLinesP(greyImg,lines,1,Math.PI/180,阈值,
最小线尺寸、线间距);
对于(int x=0;x300.d)//显示长度大于300的行
line(initImg,start,end,new Scalar(0255,0255),5);//这里initImg是原始图像。
}