Ask Your Question
0

cv::threshold crash

asked 2013-07-22 14:52:46 -0600

redfood gravatar image

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.

edit retag flag offensive close merge delete

Comments

src.convertTo(src, CV_8UC1, 1.0/256.0, 0); ? In this way you'll set your complete image to 0...

Guanta gravatar imageGuanta ( 2013-07-22 15:46:18 -0600 )edit

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.

redfood gravatar imageredfood ( 2013-07-22 15:57:29 -0600 )edit

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?

Guanta gravatar imageGuanta ( 2013-07-22 16:10:45 -0600 )edit

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.

redfood gravatar imageredfood ( 2013-07-22 16:46:09 -0600 )edit

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).

redfood gravatar imageredfood ( 2013-07-22 16:54:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-07-23 03:44:59 -0600

Guanta gravatar image

Solution: change everything to CV_32F first and after cv::threshold convert the result back to CV_8U. Why this works instead of CV_8U is not completely clear to me, maybe addWeighted doesn't give the correct output due to the uchar-type.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-22 14:52:46 -0600

Seen: 2,427 times

Last updated: Jul 23 '13