简体   繁体   中英

OpenCV finding contours

The following code for finding contours in an image does not give any compilation errors. However, on running I get the error "Open cv:Assertion failed (size.width > 0 && size.height > 0)" in the OpenCV imshow file.

I tried the code with just the imshow function, removing everything after it, and the code runs fine, hence the file location does not seem to be a problem!

Any help would be much appreciated. Thanks in advance!

#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv\cvaux.h>
#include <opencv\cxcore.h>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int main() {
    Mat img1;
    Mat output;

    Mat img = imread("blue.jpg"); 

    cvtColor(img, img1, CV_BGR2GRAY);
    threshold(img1, output, 176, 255, CV_THRESH_BINARY);
    imshow("hi", output);
    vector<vector<Point>> Contours;
    vector<Vec4i> hier;
    Mat final;

    findContours(img1, Contours, hier, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

    for (int i = 0; i < Contours.size(); i++)
    {
        drawContours(final, Contours, i, Scalar(0, 255, 0), 1, 8, hier);
    }

    imshow("result", final);

    waitKey();
}

You are drawing to a non initialized matrix ( final ) here:

Mat final;
....
drawContours(final, Contours, i, Scalar(0, 255, 0), 1, 8, hier);

You should initialize final first, like:

Mat final = img.clone();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM