Reputation: 580
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();
}
Upvotes: 2
Views: 635
Reputation: 41765
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();
Upvotes: 2