Attention! This forum will be made read-only by Dec-20. Please migrate to https://forum.opencv.org. Most of existing active users should've received invitation by e-mail.
Ask Your Question

Revision history [back]

just a sample code to show an alternative way

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"


void main()
{
    const cv::Mat in = cv::imread("15996646131571621.jpg");

    cv::Mat src;
    cv::dilate(in, src, cv::Mat(), cv::Point(-1, -1), 2);
    cv::erode(src, src, cv::Mat(), cv::Point(-1, -1), 2);

    cv::Mat hsv;
    cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);

    std::vector<cv::Mat> split_s;
    cv::split(hsv, split_s);

    split_s[1] = split_s[1] > 70;
    cv::dilate(split_s[1], split_s[1], cv::Mat());

    std::vector<std::vector<cv::Point> > contours;
    findContours(split_s[1], contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);

    for (size_t i = 0; i < contours.size(); i++)
    {
        cv::Rect r = cv::boundingRect(contours[i]);

        cv::Mat resized;
        cv::resize(in(r), resized, cv::Size(5, 5), 0, 0);
        cv::Scalar mean_s = cv::mean(resized);

        if( (mean_s[0] > 120 ) & (mean_s[0]< 200) )
        {
            cv::drawContours(in, contours, i, cv::Scalar(0, 255, 0), 1);
            cv::rectangle(in, r, cv::Scalar(0, 0, 255), 1);
        }
    }

    cv::imshow("result", in);
    cv::waitKey();
}

EDIT : there is an open source project OpenCFU

here is just a sample simple code to show an alternative way

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"


void main()
{
    const cv::Mat in = cv::imread("15996646131571621.jpg");

    cv::Mat src;
    cv::dilate(in, src, cv::Mat(), cv::Point(-1, -1), 2);
    cv::erode(src, src, cv::Mat(), cv::Point(-1, -1), 2);

    cv::Mat hsv;
    cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);

    std::vector<cv::Mat> split_s;
    cv::split(hsv, split_s);

    split_s[1] = split_s[1] > 70;
    cv::dilate(split_s[1], split_s[1], cv::Mat());

    std::vector<std::vector<cv::Point> > contours;
    findContours(split_s[1], contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);

    for (size_t i = 0; i < contours.size(); i++)
    {
        cv::Rect r = cv::boundingRect(contours[i]);

        cv::Mat resized;
        cv::resize(in(r), resized, cv::Size(5, 5), 0, 0);
        cv::Scalar mean_s = cv::mean(resized);

        if( (mean_s[0] > 120 ) & (mean_s[0]< 200) )
        {
            cv::drawContours(in, contours, i, cv::Scalar(0, 255, 0), 1);
            cv::rectangle(in, r, cv::Scalar(0, 0, 255), 1);
        }
    }

    cv::imshow("result", in);
    cv::waitKey();
}