Ask Your Question
0

Hue thresholding [closed]

asked 2019-02-11 01:55:45 -0600

updated 2019-02-11 02:48:21 -0600

I have calculated hue histogram then filtered pixels of the source image with low hue numbers. Threshold is marked by vertical white line on the histogram.

image description

Can anybody explain why such poor results?

image description

Here is the code:

cvtColor(src, src2, CV_BGR2HSV);

float peak_hue = peak_ind * bin_range;
float peak_hue1 = peak_hue - 2 * bin_range;
float peak_hue2 = peak_hue + 2 * bin_range;
Mat src4(src2.size(), CV_8UC3, Scalar(0, 0, 0));
Mat src5;
for (int i = 0; i < src2.rows; i++) {
    for (int j = 0; j < src2.cols; j++) {
        pix = src2.at<Vec3b>(i, j);
        if (peak_hue1 <= pix[0] && pix[0] < peak_hue2) { //h
            src4.at<Vec3b>(i, j) = pix;
        }
    }
}
cvtColor(src4, src5, CV_HSV2BGR);
imshow("Extracted foreground", src5);
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ya_ocv_user
close date 2019-02-11 08:05:45.398159

Comments

and where's your code ?

berak gravatar imageberak ( 2019-02-11 01:57:10 -0600 )edit
1

comments : use inRange instead for loop. You will get a mask and then setTo to change pixel value

LBerger gravatar imageLBerger ( 2019-02-11 02:56:37 -0600 )edit

@LBerger. It is not in range. If you looking at pic on right side. you see alot of noising. Meant, if the valuse is too lower, you get alot of nosies. Set to higher valuies, will do a good job. Youn have to play around with it.

supra56 gravatar imagesupra56 ( 2019-02-11 09:17:35 -0600 )edit

inRange should work too, only it requires more arrays so consumes more memory. This may be a problem for large images.

ya_ocv_user gravatar imageya_ocv_user ( 2019-02-11 09:39:30 -0600 )edit

@ya_ocv_user consumes more memory? you can give a scalar for parameter lowerb and higjherb

inRange(src,peak_hue1,peak_hue2,mask)
src4.setTo(src2,mask)
LBerger gravatar imageLBerger ( 2019-02-11 10:14:48 -0600 )edit

This is better, but still mask is an array.

ya_ocv_user gravatar imageya_ocv_user ( 2019-02-11 10:29:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-02-11 07:58:28 -0600

There was a bug in my program, not in OpenCV. More lines of code were needed to find it. They draw those 2 white threshold lines:

i1 = peak_ind - 5;
if (i1 < 0) i1 = 0;
i2 = peak_ind + 5;
if (i2 >= histSize) i2 = histSize - 1;
line(histImage, Point(bin_w*i1, 0), Point(bin_w*i1, hist_h), Scalar(255, 255, 255), 1, 8, 0);
line(histImage, Point(bin_w*i2, 0), Point(bin_w*i2, hist_h), Scalar(255, 255, 255), 1, 8, 0);

You see that threshold window is different. If I change:

float peak_hue1 = peak_hue - 5 * bin_range;
float peak_hue2 = peak_hue + 5 * bin_range;

the result will be:

image description

edit flag offensive delete link more

Comments

It is not a bug. You spent more time to solve your problem. Doesn't that mean it is BUG?

supra56 gravatar imagesupra56 ( 2019-02-11 09:12:11 -0600 )edit

Btw, I was going to ask you about threshold, but i'm using pthon.

supra56 gravatar imagesupra56 ( 2019-02-11 09:20:36 -0600 )edit

Classical bug. Did you ever use de-bug-ger? :) If it is about threshold in general, not python-specific - come on, ask. I learned python in the past, but currently don't use it.

ya_ocv_user gravatar imageya_ocv_user ( 2019-02-11 09:34:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-11 01:55:45 -0600

Seen: 898 times

Last updated: Feb 11 '19