The following advice is meant for just starting the investigation. I may have missing some important points which may have caused the crash. But if you do solve the crash issue, please do share your findings so that all of us can learn from it.
(1)
Does the crash happen in debug build or release build? If you are using release build, try make a debug build and run it. A debug build may sometimes pinpoint the cause of corruption more precisely and earlier in time.
(2)
Can you make sure the size of *pImg
is at least (500, 500)
?
(3)
Although cvtColor(src, dest)
is capable of in-place operation according to some people, I wouldn't bet on it. (noted below) Try call cvtColor
with a new Mat dest;
object as output --- which does not have to be initialized with size and type, as all it does is allocating a smart reference header. Keep in mind this is just for investigating why the crash occurs. It is not meant for general advice.
It takes moderately advanced C++ knowledge and code-reading into the OpenCV library to provide a technically accurate explanation of why it will/will not work. (I wouldn't try this at home.)
(4)
Finally, make sure your own code and the OpenCV library are compiled with the same version of Microsoft Visual C++ compiler.
As noted in Breaking Changes in Visual C++ and Potential errors passing C/C++ objects across DLL boundaries, the object layouts of STL classes are subject to change as you upgrade the version of Visual C++.
(5)
As a side note, keep in mind that both HoughLines
and HoughLinesP
carry a remark saying that the image may be modified by the function. Also, the constructor syntax Mat(Mat, Rect)
, and see also this is for creating an ROI on the original image, which means the two Mat
objects share the same memory region, and modifications to one Mat
are visible through the other Mat
. To eliminate this as a possible cause of error, try one of the two things:
- Replace
Mat center(*pImg, bounds);
with Mat center = (*pImg)(bounds).clone();
- Replace
HoughLinesP(...);
with center.setTo(cv::Scalar(0.0));
The above two code replacements are only used for investigating the cause of crash. They are not general advice. In other words, it is acceptable to allow the image to be modified by HoughLinesP
through an ROI Mat
, if this is exactly what you want.
do not use pointers to cv::Mat. you probably break it's internal refcounts.
i.e, the way you'doing it now, how should cvtColor allocate a different object for the grayscale Mat ? that's looks impossible given your pointers there)
Not sure what the concern is. Using the same Mat as input & output could be an issue, but why should dereferencing an object pointer be causing problems?
again, please get rid of the pointers.