Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi,

for me it works with a simple threshold:

int main(int argc, char** argv)
{

    cv::Mat img = cv::imread("img_test.png", CV_LOAD_IMAGE_GRAYSCALE);

    if (!img.data) {
        std::cout << "Error reading image" << std::endl;
        return EXIT_FAILURE;
    }

    cv::Mat output;
    cv::threshold(img, output, 210, 255, CV_THRESH_BINARY);

    cv::imshow("Thresholded img", output);
    cv::waitKey(0);

    std::vector<std::vector<cv::Point>> contours;

    cv::findContours(output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    std::cout << contours.size() << " contours have been found! " << std::endl;

}

Then I use cv::findContours to find the right number of blobs. If you have multiple different images it could be useful to filter the images first to eliminate the salt and pepper noise. But you have to be cautious not to eliminate your blobs :D.

Your question got probably downvoted because you didn't really show us what you tried with OpenCV but just said "basic thresholding techniques didn't work". Seems like they do ;).

Hi,

for me it works with a simple threshold:threshold + CLAHE:

int main(int argc, char** argv)
{

    cv::Mat img = cv::imread("img_test.png", CV_LOAD_IMAGE_GRAYSCALE);

    if (!img.data) {
        std::cout << "Error reading image" << std::endl;
        return EXIT_FAILURE;
    }

    // Added contrast limited adaptive histogram equalization
    // More parameters are possible within cv::createCLAHE();
    cv::Mat output;
    cv::threshold(img, cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
    clahe->setClipLimit(2);

    clahe->apply(img,output);


    cv::threshold(output, output, 210, 22, 255, CV_THRESH_BINARY);
     cv::imshow("Original image", img);
    cv::imshow("Thresholded img", output);
    cv::waitKey(0);

    std::vector<std::vector<cv::Point>> contours;

    cv::findContours(output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    std::cout << contours.size() << " contours have been found! " << std::endl;

}

Then I use cv::findContours to find the right number of blobs. If you have multiple different images it could be useful to filter the images first to eliminate the salt and pepper noise. But you have to be cautious not to eliminate your blobs :D.blobs.

Your question got probably downvoted because you didn't really show us what you tried with OpenCV OpenCV.

Edit: I think a filtering of the image is necessary before binarization. I used for instance CLAHE but just said "basic thresholding techniques didn't work". Seems like they do ;).

other filters could also work. After that I proceed with the simple thresholding.

image description