cv::threshold crash
I hope I'm doing something idiotic and there is a simple fix for this but, every time I try to take a threshold on an image it crashes.
I'm trying to do simple background subtraction and then take a threshold on the resultant image (to later use as a mask). Here is the relevant code:
src.convertTo(src, CV_8UC1, 1.0/256.0, 0);
if(firstFrame) {
src.copyTo(background);
firstFrame = false;
}
cv::addWeighted(src, .001, background , .999, 0, background);
cv::absdiff(src, background, difference);
cv::threshold(difference, thresh, 100, 1, cv::THRESH_BINARY);
src, background,difference, and thresh are all created as cv::Mat In the header.
The code crashes on the last like when I try the call to cv::theshold. I can call imshow on src, background, and difference and they all look reasonable.
I'm using OpenCV 2.4.5 under c++ using VS2010.
Any help or suggestions would be appreciated.
src.convertTo(src, CV_8UC1, 1.0/256.0, 0);
? In this way you'll set your complete image to 0...Why? The src was origionaly 16UC1 and I scale down the color space. What is wrong woth that? Like I said, I call I show on the other images and they look fine. Then threshold crashed.
Ah, excuse me, I thought your src would be CV_8UC1. So, I guess your background is than also of type CV_8UC1, otherwise your addWeighted wouldn't work. Hmm, looks actually pretyy solid to me. You could try to change everything to CV_32F first and after cv::threshold convert the result back to CV_8U. One last question: your input image is already a one channel image, or?
Yes. The input image is CV_16UC1. All the other Mats are just created as cv::Mat background; cv::Mat foreground; cv::Mat difference; cv::Mat thresh; In the header. I assume this uses the default constructor. I'll try to explicitly set their size and types and see if that makes a difference.
That did it! Converting everything to CV_32F fixed the problem. Is this an OpenCV bug or did I miss something in the documentation that says you need to yous 32F? (If you want some Karma, answer the question and I'll accept it).