OpenCV:霍夫变换中无法得到红线

发布于 2025-01-14 23:31:48 字数 1825 浏览 1 评论 0原文

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

#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;
}

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

关于如何显示颜色线有什么建议吗?

I have written a simple code to perform Hough transform and display the lines. The code is as follows,

#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;
}

I am unable to get a red line (or any color) in the output Image in the window "Hough". I just get a black and white image. I'm also running a simple Canny edge detection before the Hough transform. Could that be causing an issue?

Any suggestions on how I could get a color line on to be displayed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2025-01-21 23:31:48

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

You are drawing Hough lines on the gray image of canny.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文