OpenCV: HSV inRange returns binary image squashed and repeated

Scratch17 picture Scratch17 · May 26, 2014 · Viewed 7.9k times · Source

I'm having a bit of trouble with an image that I'm converting for colour recognition.

The function looks like this:

void PaintHSVWindow(cv::Mat img){
cv::Mat HSV, threshold; 
cvtColor(img, HSV, COLOR_BGR2HSV);
inRange(HSV, cv::Scalar(HMin, SMin, VMin), cv::Scalar(HMax, SMax, VMax), threshold);
Mat erodeElement = getStructuringElement(MORPH_RECT, cv::Size(3, 3));
Mat dilateElement = getStructuringElement(MORPH_RECT, cv::Size(8, 8));
erode(threshold, threshold, erodeElement);
dilate(threshold, threshold, dilateElement);
cv::resize(threshold, threshold, cv::Size(360, 286));
MyForm::setHSVWindow(threshold);

}

And the output looks as follows:

Input on the left, output on the right, filtering to find the yellow ball

On the left is the input. On the right is supposed to be the same image, converted to HSV, filtered between the given thresholds to find the yellow ball, eroded and dilated to remove the smaller contours, and displayed in half the size of the original image. Instead, it takes the expected image and squashes 3 of them in the same space.

Any guesses as to why this would happen?


UPDATE 1:

OK, since it appears that running findContours on the image on the right-hand size still gives me the proper output, i.e. the contours from the distorted, 3-times-copied right-side image can be pasted into the right position on the left-side input image, I've decided to just take the distorted image and crop it for display purposes. It will only ever be used to find the contours of a given HSV range in an image, and if it serves that purpose, I'm happy.

Answer

Adi Shavit picture Adi Shavit · Jun 12, 2014

As @Nallath comments, this is apparently a channel issue. According to the documentation, the output of inRange() should be a 1-channel CV_8U image which is the logical AND of all channel inclusives.

Your result means that somewhere along the way threshold is being treated like a 3-channel plane-order image.

What version of OpenCV are you using?

I suggest that you show threshold between every step to find the place where this conversion happens. This might be a bug that should be reported.