2

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.

TFSM
  • 53
  • 1
  • 9
  • 2
    Presumably; this has something to do with chanel conversions. In most cases, these things happen when 3 channels are expected, but one is provided (or the other way around). Try printing in between results. – Nallath May 26 '14 at 15:32
  • indeed, the image should have been converted to black-and-white first, perhaps – Ashalynd May 26 '14 at 15:45
  • The only step in between is the HSV conversion, and that looks as I would expect it. It's the inRange function returning the frame split into 3 like that. – TFSM May 27 '14 at 11:02
  • 1
    Also @Ashalynd converting it to black and white removes all the colour information that I need to, y'know, identify colours with. – TFSM May 27 '14 at 13:18
  • Could you post your values for the HSV ranges. That way we don't have to guess when trying to reproduce your problem. – Aurelius May 27 '14 at 16:10
  • @Aurelius, here's the specs for the HSV range. `H[26, 33], S[133, 255], V[75, 191]` Although I'm not sure if the range information will help. It comes back as 3 images regardless of the range. – TFSM May 28 '14 at 08:02
  • The odd thing is, applying the findContour function to the image on the right gives me the expected results; i.e. if I draw the contours over the main image on the left, it lines up... very strange... – TFSM May 28 '14 at 13:36
  • MyForm::setHSVWindow(threshold);, //<-- what does it do ? i guess, you just got a problem blitting b/w images correctly – berak May 29 '14 at 08:05
  • @berak Perhaps. That's not a bad assumption. I'll check into my routine used to post those images. But I've just managed to crop the image and display the black and white image, so it works fine as is. – TFSM May 30 '14 at 12:58
  • What version of OpenCV are you using? – Adi Shavit Jun 12 '14 at 06:24

1 Answers1

-1

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.

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137