CV_Assert fail error in ximgproc module
Problem : I have opencv 3.0 configured with extra modules on my system. I want to use the structured edge detector implemented in module ximgproc module. link : http://docs.opencv.org/trunk/d0/da5/t...
But it is giving following assert fail error on every image I tried till now.
512 512
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0])) in cv::Mat::ptr, file C:\Program Files\OpenCV\opencv3.0\sources\modules\core\include\opencv2/core/mat.inl.hpp, line 750
Press any key to continue . . .
This error is caused by the function
detectEdges(src,dst)
Actual file path on my system is opencv3.0\sources\modules\core\include\opencv2/core/mat.inl.hpp . I am using the model file given in samples ('model.yml.gz')
Thanks
Code used:
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/utility.hpp"
#include "ximgproc/include/opencv2/ximgproc.hpp"
#include "ximgproc\include\opencv2\ximgproc\structured_edge_detection.hpp"
using namespace std;
using namespace cv;
using namespace cv::ximgproc;
int main(int argc, const char** argv)
{
std::string modelFilename = "model.yml.gz";
std::string inFilename = "01.png"; // "lena.jpg"
std::string outFilename = "" ;//parser.get<std::string>("o");
Mat image = imread(inFilename,1);
if (image.empty())
{
printf("Cannot read image file: %s\n", inFilename.c_str());
return -1;
}
Size size = image.size();
std::cout << size.height << " " << size.width << std::endl;
if ((size.height < 31) || (size.width < 20)) {
if (size.height < 31)
size.height = 31;
if (size.width < 15)
size.width = 15;
resize(image, image, size, 0.0, 0.0, INTER_AREA);
}
size.width = 400;
size.height = 213;
resize(image, image, size, 0.0, 0.0, INTER_AREA);
image.convertTo(image, DataType<float>::type, 1.0 / 255.0);
Mat edges = Mat(image.rows,image.cols , image.type(), float(0.0));
Ptr<RFFeatureGetter> rfptr = createRFFeatureGetter();
Ptr<StructuredEdgeDetection> pDollar =
createStructuredEdgeDetection(modelFilename, rfptr);
//cerr << "reaching till here " << endl;
pDollar->detectEdges((const Mat)image , edges);
//cerr << " unreached " << endl;
if (outFilename == "")
{
namedWindow("edges", 1);
imshow("edges", edges);
waitKey(0);
}
else
imwrite(outFilename, 255 * edges);
return 0;
}
Since this happens for every image, there is probably something wrong in your code.
i cannot reproduce your error.
You mean this code is running properly on your system?
ok, buffer overflow in structured_edge_detection.cpp, line 243 (probably due to your weird resize())
it seems to have a problem if height < width
So what is the solution ? Changing the input image(where width < height) doesn't improves the situation .
^^ hmm, true. 1st idea was wrong.
i'm now assuming, it's a rounding error.
changing cvRound to cvCeil in line 191 made it work for me. could you try ?
So Is it a bug or something is wrong with my approach or model or image due to which cvRound is required to change to cvCeil ? And if not a bug can you provide the correct way for doing it ?
i think it is a bug.
(unfortunately, i do not understand enough of it yet, to know, if the cvCeil fix is the correct one)
"Is there any good explanation or tutorial online ?" - sorry, i don't know. you could try to read the paper
How to make the modelFile like "model.yml"?