OpenCV:无法在 C++ 中运行 Canny 边缘检测;

发布于 2025-01-14 18:01:51 字数 1225 浏览 0 评论 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;

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

int main()
{

    VideoCapture cap(0);

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

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

        int ret = waitKey(1);

        CannyThreshold();

        if(ret == 'q')
            break;
    }

    cap.release();

    return 0;
}

当我运行代码时,出现以下运行时错误。 (我使用的是 OpenCV 4)

错误:(-215:断言失败)ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > > 0 && ksize.height % 2 == 1 in function 'createGaussianKernels'

关于如何解决此错误有什么建议吗?

I have written a simple code to perform canny edge detection on a live stream. The code is as shown below,

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

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

int main()
{

    VideoCapture cap(0);

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

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

        int ret = waitKey(1);

        CannyThreshold();

        if(ret == 'q')
            break;
    }

    cap.release();

    return 0;
}

I get the following run-time error when I run the code. (I'm using OpenCV 4)

error: (-215:Assertion failed) ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1 in function 'createGaussianKernels'

Any suggestions on how I can solve this error?

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

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

发布评论

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

评论(1

表情可笑 2025-01-21 18:01:51

问题是 GaussianBlur 无法接受内核大小 1。在代码中将其更正为 3x3 或 5x5,如下所示

#include <opencv2/core/utility.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
    VideoCapture cap;
    Mat frame;
    Mat image; // from cap to image
    Mat src_gray;

    Mat dst;
    Mat detected_edges;


    const String window_name = "Canny Edge Detector - VideoCapture";
    int lowThreshold = 10;
    const int max_lowThreshold = 100;
    const int ratio = 3;
    const int kernel_size = 3;

    int FPS;
    int frame_width, frame_height;

    int camNum = 0;
    cap.open(camNum);


    if (!cap.isOpened())
    {

        cout << "***Could not initialize capturing...***\n";
        cout << "Current parameter's value: \n";
        return -1;
    }


    FPS = cap.get(CAP_PROP_FPS);
    frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
    frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
    dst.create(frame_width, frame_height, CV_8UC3);

    //cout << CV_8UC3;

    while (true)
    {

        cap >> frame;
        if (frame.empty())
            break;


        frame.copyTo(image);

        // Convert the image to grayscale
        cvtColor(image, src_gray, COLOR_BGR2GRAY);

        //![reduce_noise]
        /// Reduce noise with a kernel 3x3
        blur(src_gray, detected_edges, Size(3, 3));
        //![reduce_noise]

        //![canny]
        /// Canny detector
        Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
        //![canny]

        /// Using Canny's output as a mask, we display our result
        //![fill]
        dst = Scalar::all(0);
        //![fill]

        //![copyto]
        image.copyTo(dst, detected_edges);
        //![copyto]


        //![display]
        imshow(window_name, dst);

        if (waitKey(1000 / FPS) >= 0)
            break;

    }




    return 0;
}


The issue is GaussianBlur cant accept kernel size of 1. Correct it to 3x3 or 5x5 in your code as follows

#include <opencv2/core/utility.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
    VideoCapture cap;
    Mat frame;
    Mat image; // from cap to image
    Mat src_gray;

    Mat dst;
    Mat detected_edges;


    const String window_name = "Canny Edge Detector - VideoCapture";
    int lowThreshold = 10;
    const int max_lowThreshold = 100;
    const int ratio = 3;
    const int kernel_size = 3;

    int FPS;
    int frame_width, frame_height;

    int camNum = 0;
    cap.open(camNum);


    if (!cap.isOpened())
    {

        cout << "***Could not initialize capturing...***\n";
        cout << "Current parameter's value: \n";
        return -1;
    }


    FPS = cap.get(CAP_PROP_FPS);
    frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
    frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
    dst.create(frame_width, frame_height, CV_8UC3);

    //cout << CV_8UC3;

    while (true)
    {

        cap >> frame;
        if (frame.empty())
            break;


        frame.copyTo(image);

        // Convert the image to grayscale
        cvtColor(image, src_gray, COLOR_BGR2GRAY);

        //![reduce_noise]
        /// Reduce noise with a kernel 3x3
        blur(src_gray, detected_edges, Size(3, 3));
        //![reduce_noise]

        //![canny]
        /// Canny detector
        Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
        //![canny]

        /// Using Canny's output as a mask, we display our result
        //![fill]
        dst = Scalar::all(0);
        //![fill]

        //![copyto]
        image.copyTo(dst, detected_edges);
        //![copyto]


        //![display]
        imshow(window_name, dst);

        if (waitKey(1000 / FPS) >= 0)
            break;

    }




    return 0;
}


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