Heap Curroption error in findContours - opencv

I have a heap corruption error because of the cv::findContours function. I need help figuring out the solution for this problem.
int GetEndPoints(cv::Mat image)
{
cv::Mat imgBW = cv::Mat::zeros(image.rows, image.cols, CV_8UC1);
cv::cvtColor(image, imgBW, CV_BGR2GRAY);
std::cout << std::endl << imgBW.channels();
cv::threshold(imgBW, imgBW, 150, 255, cv::THRESH_BINARY);
cv::namedWindow("image", 0);
cv::imshow("image", imgBW);
std::vector<std::vector<cv::Point>> contours;
cv::Mat image1 = image.clone();
// Find the Contours
std::cout << std::endl << imgBW.channels();
cv::findContours(imgBW, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
cv::drawContours(image1, contours, -1, cv::Scalar(255, 0, 0), 2, 8);
cv::namedWindow("contours", 0);
cv::imshow("contours", image1);
cv::waitKey();
return 0;
}

You shoud check number of contours you found. I think you get error when there is no contours in contours variable.

Related

OpenCV: convert .raw(by photoshop cs6) to Mat return grey

I'm relatively new to OpenCV and i've stumpled upon a problem. I read the data from an .raw produced by photoshop and tried to convert it into Mat.But when I used 'imgshow' with the Mat called 'dst', the image was grey.
if ((err = fopen_s(&file, filename, "rb")) != 0)
cout << err << endl;
else
printf("The file was opened\n");
fread(data, sizeof(char), nsize, file);
cv::Mat temp(height, width, CV_8UC1, data);
cv::Mat mtep[3];
temp.copyTo(mtep[0]);
temp.copyTo(mtep[1]);
temp.copyTo(mtep[2]);
cv::Mat mergeM(height, width, CV_8UC3);
merge(mtep, 3, mergeM);
mergeM.convertTo(dst, CV_32FC3, 1/255.0);

using getoptiamlnewcameramatrix to recover all the original image

enter image description here
this is my program output
and this is my distorted imageenter image description here
to get the original image`s whole pixel ,I used the getoptimalnewcameramatrix,but the output is terrible ,my intrix and distcoffes is totally correct.
here is my program
Mat src = imread("E:\\40_office\\distorted_bot\\0.jpg");
Size newsize(2280,3072);
cout << src.rows;
namedWindow("", WINDOW_AUTOSIZE);
imshow("", src);
waitKey();
Mat mapx,mapy;//size(x,y)
Mat cameraMatrix = (Mat_<double>(3, 3) << 1224.1, 0, 761.7497, 0, 1209.8, 1043.6, 0, 0, 1);
//Mat cameraMatrix = (Mat_<double>(3, 3) << 1224.1, 0, 1141.7497, 0, 1209.8, 1442.6, 0, 0, 1);
//Mat cameraMatrix = (Mat_<double>(3, 3) << 1209.8, 0, 1043.6, 0, 1224.1, 761.7497, 0, 0, 1);
Mat distCoeffs = (Mat_<double>(5, 1) << -0.3649, 0.1451, -0.0273, -0.000035214,0.0012);
double alpha = 1;
Mat newcameramatrix = getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, src.size(), alpha, newsize,0);
cout << newcameramatrix<<"src.size"<<src.size();
fisheye::initUndistortRectifyMap(cameraMatrix, distCoeffs,Mat(), newcameramatrix ,newsize, CV_16SC2, mapx,mapy);
Mat newimage=Mat(newsize,CV_8UC3);
remap(src, newimage, mapx, mapy, INTER_LINEAR);
imwrite("C:\\Users\\wk\\Desktop\\1_cali100.jpg", newimage);
return;

OpenCV absdiff() Error: Sizes of input arguments do not match

I have error on absdiff(grayImg, bgImg, diffImg);
and I don't understand why this is wrong.
VideoCapture capture;
capture.open(0);
if (!capture.isOpened()) {
printf("error\n");
return -1;
}
Mat grayImg;
Mat diffImg;
Mat bgImg = imread("BgImg.jpg");
Mat frame;
std::vector<std::vector<cv::Point> > contours;
for (;;) {
capture >> frame;
cvtColor(frame, grayImg, CV_RGB2GRAY);
absdiff(grayImg, bgImg, diffImg);
threshold(diffImg, diffImg, 50, 255, CV_THRESH_BINARY);
findContours(diffImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
drawContours(frame, contours, -1, CV_RGB(255, 0, 0), 1, 8);
imshow("capture", frame);
imshow("diffImg", diffImg);
char chKey = waitKey(10);
if (chKey == 27) break;
}
destroyAllWindows();
return 0;

Opencv app crashes when using external camera

was using contours for object identification. The code worked well with images and I modified the code to identify objects in real time with camera input. Things work well with my laptop's integrated cam but crashes after a few seconds when using an external camera. The external camera worked fine with a few other applications I developed using opencv. The camera is a 20MP camera. Please look at the code and help me figure out what might be wrong. My processor is good enough to handle images with high resolutions. It seems that the app crashes when I introduce an object in front of the cam which was not there before when the app started up.
include <iostream>
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
using namespace cv; using namespace std;
int main()
{
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;
int x = 0;
int y = 0;
VideoCapture xps(0);
Mat src;
while (1)
{
xps.read(src);
vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;
Mat thr(src.rows, src.cols, CV_8UC1);
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 80, 255, THRESH_BINARY_INV);
findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i< contours.size(); i++) // iterate through each contour.
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i;
bounding_rect = boundingRect(contours[i]);
}
}
Scalar color(255, 255, 255);
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy);
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
x = bounding_rect.x + bounding_rect.width / 2;
y = bounding_rect.y + bounding_rect.height / 2;
circle(src, Point(x, y), 1, Scalar(0, 0, 255));
imshow("src", src);
imshow("largest Contour", dst);
waitKey(2);
}
}
I believe the crashes are due the contours which might not be found. To avoid this problem, use a flag and if the contours are found, then draw them.
bool found = findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
/* for loop */
if(found)
{
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy);
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
x = bounding_rect.x + bounding_rect.width / 2;
y = bounding_rect.y + bounding_rect.height / 2;
circle(src, Point(x, y), 1, Scalar(0, 0, 255));
}

What's the difference between Mat::clone and Mat::copyTo?

I know 'copyTo' can handle mask. But when mask is not needed, can I use both equally?
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-clone
Actually, they are NOT the same even without mask.
The major difference is that when the destination matrix and the source matrix have the same type and size, copyTo will not change the address of the destination matrix, while clone will always allocate a new address for the destination matrix.
This is important when the destination matrix is copied using copy assignment operator before copyTo or clone. For example,
Using copyTo:
Mat mat1 = Mat::ones(1, 5, CV_32F);
Mat mat2 = mat1;
Mat mat3 = Mat::zeros(1, 5, CV_32F);
mat3.copyTo(mat1);
cout << mat1 << endl;
cout << mat2 << endl;
Output:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
Using clone:
Mat mat1 = Mat::ones(1, 5, CV_32F);
Mat mat2 = mat1;
Mat mat3 = Mat::zeros(1, 5, CV_32F);
mat1 = mat3.clone();
cout << mat1 << endl;
cout << mat2 << endl;
Output:
[0, 0, 0, 0, 0]
[1, 1, 1, 1, 1]
This is the implementation of Mat::clone() function:
inline Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
So, as #rotating_image had mentioned, if you don't provide mask for copyTo() function, it's same as clone().
Mat::copyTo is for when you already have a destination cv::Mat that (may be or) is already allocated with the right data size. Mat::clone is a convenience for when you know you have to allocate a new cv::Mat.
copyTo doesn't allocate new memory in the heap which is faster.

Resources