【问题标题】:OpenCV: Unable to get a red line in Hough transformOpenCV:无法在霍夫变换中得到红线
【发布时间】:2022-09-29 09:23:38
【问题描述】:

我编写了一个简单的代码来执行霍夫变换并显示线条。代码如下,

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;

Mat img;
Mat display;
Mat temp;

void CannyThreshold()
{
    cvtColor(img, display, COLOR_RGB2GRAY);

    // GaussianBlur(display,display,Size(7,7),3,3);
    GaussianBlur(display, display, Size(1, 1), 1,1);

    // printf(\"%d\\n\",lowThreshold);

    Canny(display,display,lowThreshold,3);

    imshow(\"Canny\",display);
}

void Hough()
{
    
    Canny(temp,display,50,3);

    vector<Vec2f> lines; // will hold the results of the detection
    HoughLines(display, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection    

    for( size_t i = 0; i < lines.size(); i++ )
    {
        float rho = lines[i][0], theta = lines[i][1];
        Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));
        line(display, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
    }

    printf(\"Lines = %ld\\n\",lines.size());

    imshow(\"Hough\",display);

}

int main()
{

    VideoCapture cap(0);

    namedWindow(\"Canny\");
    createTrackbar(\"Min Threshold: \",\"Canny\",&lowThreshold,max_lowThreshold);

    while(1)
    {
        cap.read(img);

        temp = img;
        CannyThreshold();
        Hough();

        waitKey(1);
    }

    cap.release();

    return 0;
}

我无法在窗口“霍夫”的输出图像中得到一条红线(或任何颜色)。我只得到一张黑白图像。我还在霍夫变换之前运行了一个简单的 Canny 边缘检测。这会导致问题吗?

关于如何显示颜色线的任何建议?

    标签: c++ opencv image-processing


    【解决方案1】:

    您正在 canny 的灰色图像上绘制霍夫线。

    【讨论】:

    【解决方案2】:

    您需要确保将彩色图像放入霍夫变换中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多