Ask Your Question

Revision history [back]

There are 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, add 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

There are 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, add 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

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