Removing blobs from a binary image

前端 未结 2 1909
感情败类
感情败类 2021-02-14 19:10

I have a binary image which contain few blobs.

I want to remove blobs which are less than a certain area.

Can any one suggest me a way?

I am using Open-C

2条回答
  •  我在风中等你
    2021-02-14 19:45

    You can do something like this:

    // your input binary image
    // assuming that blob pixels have positive values, zero otherwise
    Mat binary_image; 
    
    // threashold specifying minimum area of a blob
    double threshold = 100;
    
    vector> contours;
    vector hierarchy;
    vector small_blobs;
    double contour_area;
    Mat temp_image;
    
    // find all contours in the binary image
    binary_image.copyTo(temp_image);
    findContours(temp_image, contours, hierarchy, CV_RETR_CCOMP,
                                                      CV_CHAIN_APPROX_SIMPLE);
    
    // Find indices of contours whose area is less than `threshold` 
    if ( !contours_all.empty()) {
        for (size_t i=0; i

提交回复
热议问题