Ask Your Question

Combine a Haar Cascade with color detection

asked 2017-01-30 07:06:41 -0500

sumosaurus gravatar image

I am attempting to detect an object with my webcam. I am successfully detecting the object but also detecting a LOT of false positives. Is there any way to filter my detections using a color? The object that I am trying to detect is red - I want to only draw a rectangle around my object if it matches the cascade AND it happens to be red. Any help would be greatly appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-30 07:51:57 -0500

updated 2017-01-30 07:53:58 -0500

There are at least two ways you can go about this.

1 - Post-process detection to eliminate some false positives

Take the ROI of each detection and use simple color segmentation to eliminate non-red false positives: This small function returns a 0-1 factor of presence of red pixels in image.

double getRedFactor(cv::Mat bgrMat)
    cv::Mat hsv;
    std::vector<cv::Mat> hsvChannels;

    cv::cvtColor(bgrMat, hsv, CV_BGR2HSV);
    cv::split(hsv, hsvChannels);

    cv::Mat H = hsvChannels[0];
    cv::Mat S = hsvChannels[1];
    cv::Mat V = hsvChannels[2];

    cv::Mat red = (H < 20) | (H > 200); //-- this is a rough segmentation, can be tweaked for optimized results

    double sumRedPixels  = cv::sum(red)[0] / 255;

    double redFactor = sumRedPixels / (red.cols * bgr.rows); //-- A factor that ranges between 0 to 1 according to the presence of red pixels

    return redFactor;

2 - Add color features to your classifier in order to decrease false detections

There is an algorithm inspired in the Viola-Jones Haar Cascade that improves its results by, among other things, adding color features to the feature space. I think OpenCV has an working implementation of this algorithm, it is called Integral Channel Features and it is much more robust that a plain Haar Cascade.

Here is the reference page of this algorithm

edit flag offensive delete link more


Cheers Pedro. I'll check this out and mark as the answer if I can implement one of these methods :-)

sumosaurus gravatar imagesumosaurus ( 2017-01-30 07:56:42 -0500 )edit

sadly, "Integral Channel Features" were removed from xobjdetect in current opencv3.2

berak gravatar imageberak ( 2017-01-30 08:04:28 -0500 )edit

Oh.... I was just trying to find it....

sumosaurus gravatar imagesumosaurus ( 2017-01-30 08:05:27 -0500 )edit

It is a pretty complex implementation. If there is no ready-to-use solution for ICF then I suggest that you do not pursue that solution.

Pedro Batista gravatar imagePedro Batista ( 2017-01-30 08:10:13 -0500 )edit

Question Tools

1 follower


Asked: 2017-01-30 07:06:41 -0500

Seen: 2,267 times

Last updated: Jan 30 '17