Android OpenCV绘制霍夫线

Android OpenCV绘制霍夫线,android,opencv,hough-transform,Android,Opencv,Hough Transform,我试图在android手机上使用OpenCV来检测线路。我修改了“教程1基础-2”。使用OpenCV相机的示例。我也用它作为例子。 然而,我得到了奇怪的分数(至少我认为是奇怪的数字)。在1000到-1000的范围内 我不完全理解代码(主要是关于加/减1000*(a或-b)的部分) 最后,我一点也看不到台词 谁能帮我一下吗?如果您需要更多信息,也请告诉我 capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); Imgproc.Cann

我试图在android手机上使用OpenCV来检测线路。我修改了“教程1基础-2”。使用OpenCV相机的示例。我也用它作为例子。 然而,我得到了奇怪的分数(至少我认为是奇怪的数字)。在1000到-1000的范围内

我不完全理解代码(主要是关于加/减1000*(a或-b)的部分)

最后,我一点也看不到台词

谁能帮我一下吗?如果您需要更多信息,也请告诉我

capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
Imgproc.HoughLines(mIntermediateMat, mLines, 1, Math.PI/180, 100);

Scalar color = new Scalar(0, 0, 255);

double[] data;
double rho, theta;
Point pt1 = new Point();
Point pt2 = new Point();
double a, b;
double x0, y0;
for (int i = 0; i < mLines.cols(); i++)
{
    data = mLines.get(0, i);
    rho = data[0];
    theta = data[1];
    a = Math.cos(theta);
    b = Math.sin(theta);
    x0 = a*rho;
    y0 = b*rho;
    pt1.x = Math.round(x0 + 1000*(-b));
    pt1.y = Math.round(y0 + 1000*a);
    pt2.x = Math.round(x0 - 1000*(-b));
    pt2.y = Math.round(y0 - 1000 *a);
    Core.line(mIntermediateMat, pt1, pt2, color, 3);
}

Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);

Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);

if (Utils.matToBitmap(mRgba, bmp))
    return bmp;

bmp.recycle();
return null;
capture.retrieve(mGray、Highgui.CV\u CAP\u ANDROID\u GREY\u FRAME);
Imgproc.Canny(mIntermediateMat公司经理,80100);
Imgproc.HoughLines(mIntermediateMat,mLines,1,Math.PI/180100);
标量颜色=新标量(0,0,255);
双[]数据;
双ρ,θ;
点pt1=新点();
点pt2=新点();
双a,b;
双x0,y0;
for(int i=0;i
我正在用它在我的框架中找到线条,然后把它们画出来

这是我的密码。。。希望这有帮助

    Mat mYuv = new Mat();
    Mat mRgba = new Mat();
    Mat thresholdImage = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
    mYuv.put(0, 0, data);
    Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
    Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_RGB2GRAY, 4);
    Imgproc.Canny(thresholdImage, thresholdImage, 80, 100, 3);
    Mat lines = new Mat();
    int threshold = 50;
    int minLineSize = 20;
    int lineGap = 20;

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

    for (int x = 0; x < lines.cols(); x++) 
    {
          double[] vec = lines.get(0, x);
          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);

          Core.line(mRgba, start, end, new Scalar(255,0,0), 3);

    }

    Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);

    if (Utils.matToBitmap(mRgba, bmp))
         return bmp;
Mat mYuv=new Mat();
Mat mRgba=新Mat();
Mat thresholdImage=新Mat(getFrameHeight()+getFrameHeight()/2,getFrameWidth(),CvType.CV_8UC1);
mYuv.put(0,0,数据);
Imgproc.cvtColor(mYuv,mRgba,Imgproc.COLOR_YUV420sp2RGB,4);
CVT颜色(mRgba,阈值图像,Imgproc.COLOR_RGB2GRAY,4);
Imgproc.Canny(阈值图像,阈值图像,80,100,3);
垫线=新垫();
int阈值=50;
int minLineSize=20;
int-lineGap=20;
Imgproc.HoughLinesP(thresholdImage,lines,1,Math.PI/180,threshold,minLineSize,lineGap);
对于(int x=0;x
您将线绘制到
mIntermediateMat
图像,但返回
mRgba
图像。这就是为什么你看不到台词

b
的-1000..1000范围是正确的
HoughLines
返回直线角度和与零的距离(也称为rho和θ)。要绘制它们,需要将它们转换为两点。1000是图像尺寸,如果要绘制2000x2000图像,请将其增加到2000,否则线条将不会穿过整个图像


HoughLines
的算法不同于
HoughLinesP
HoughLines
仅查找跨越整个图像的线条
HoughLinesP
返回较短的线段。

这是我的visual studio代码,希望对您有所帮助

void drawLines(Mat &input, const std::vector<Vec2f> &lines) {
for (int i = 0; i < lines.size(); i++) {
    float alpha = CV_PI/2-atan(input.rows/input.cols);
    float r_max;
    float r_min;
    float r = lines[i][0];
    float theta = lines[i][1];
    if (theta<alpha || theta>CV_PI-alpha) {
        r_max = input.cols*cos(theta);
        r_min = input.rows*sin(theta);
        if (r > r_max) {
            Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
            Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
            line(input, pt1, pt2, Scalar(255, 0, 0), 1);
        }
        else if (r < r_max && r > r_min) {
            Point pt1(r / cos(theta), 0);
            Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
            line(input, pt1, pt2, Scalar(255, 0, 0), 1);
        }
        else {
            Point pt1(r / cos(theta), 0);
            Point pt2(0, r / sin(theta));
            line(input, pt1, pt2, Scalar(255, 0, 0), 1);
        }

    }
    else {
            r_min = input.cols*cos(theta);
            r_max = input.rows*sin(theta);
            if (r > r_max) {
                Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
                Point pt2((r - input.rows*sin(theta)) / cos(theta), input.rows);
                line(input, pt1, pt2, Scalar(0, 0, 255), 1);
            }
            else if (r < r_max && r > r_min) {
                Point pt1(input.cols, (r - input.cols*cos(theta)) / sin(theta));
                Point pt2(0, r / sin(theta));
                line(input, pt1, pt2, Scalar(0, 0, 255), 1);
            }
            else {
                Point pt1(r / cos(theta), 0);
                Point pt2(0, r / sin(theta));
                line(input, pt1, pt2, Scalar(0, 0, 255), 1);
            }
    }

}
void绘图线(材料和输入,常量标准::向量和线){
对于(int i=0;ir_最大值){
点pt1(input.cols,(r-input.cols*cos(θ))/sin(θ));
点pt2((r-input.rows*sin(θ))/cos(θ),input.rows);
行(输入,pt1,pt2,标量(255,0,0),1);
}
否则如果(rr\u最小值){
点pt1(r/cos(θ),0);
点pt2((r-input.rows*sin(θ))/cos(θ),input.rows);
行(输入,pt1,pt2,标量(255,0,0),1);
}
否则{
点pt1(r/cos(θ),0);
点pt2(0,r/sin(θ));
行(输入,pt1,pt2,标量(255,0,0),1);
}
}
否则{
r_min=input.cols*cos(θ);
r_max=input.rows*sin(θ);
如果(r>r_最大值){
点pt1(input.cols,(r-input.cols*cos(θ))/sin(θ));
点pt2((r-input.rows*sin(θ))/cos(θ),input.rows);
行(输入,pt1,pt2,标量(0,0,255),1);
}
否则如果(rr\u最小值){
点pt1(input.cols,(r-input.cols*cos(θ))/sin(θ));
点pt2(0,r/sin(θ));
行(输入,pt1,pt2,标量(0,0,255),1);
}
否则{
点pt1(r/cos(θ),0);
点pt2(0,r/sin(θ));
行(输入,pt1,pt2,标量(0,0,255),1);
}
}
}
这里有两张关于我发布的代码逻辑的图表

阿尔法的解释

r_-max和r_-min的解释


你能发布完整的代码吗请看一下Giusephey我想在图像中突出显示的部分添加一个矩阵你能指导我吗?@Brian ONeil你能告诉我什么是getFramHeight()和getFramWidth()方法引用?在Opencv SDK v3中,您应该使用Imgproc.line而不是Core.line,Core.line不再存在。此外,在Opencv 3.2(我不确定旧版本)中,您应该迭代
.rows()
(而不是
.cols()
),然后