OpenCV (twenty-nine): Image corrosion

1. Principle of image corrosion

        The erosion operation works by sliding a structural element (also called a kernel or template) over the image and comparing it to the corresponding pixels in the image. If all pixels of a structural element match the pixels at the corresponding position in the image, then the pixel value at that position remains unchanged. If any pixel of the structural element does not match the pixel at the corresponding position in the image, the pixel value at that position is set to 0 (or other specified pixel value), thereby changing the shape and structure of the image.

2. Image corrosion purpose: 

  • Remove tiny objects from images
  • Separate two objects that are close together

3. Structuring element generation function getStructuringElement()

Mat cv::getStructuringElement ( int  shape,

Size   ksize,

Point anchor = point(-1,-1)

  • shape: type of structural element.
  • ksize: The size of the structural element.
  • anchor: The position of the center point. The default parameter is the geometric center point of the structural element.

The type parameters of the structural elements are:

4. Image erosion operation function erode()

void cv::erode ( InputArray  src,

OutputArray    dst,

InputArray     kernel,

Point          anchor = Point(-1,-1),

int                iterations = 1,

int               cborderType = BORDER CONSTANT,

const Scalar & borderValue = morphologyDefaultBorderValue()

)

  • src: The input image to be corroded. The number of channels of the image can be arbitrary, but the data type of the image must be one of CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
  • dst: The corroded output image has the same size and data type as the input image src.
  • kermel: Structural elements used for corrosion operations. You can enter them yourself or generate them using the getStructuringElement0) function.
  • anchor: The position of the center point in the structural element. The default parameter is the geometric center point of the structural element.
  • iterations: the number of corrosions.
  • borderType: Pixel extrapolation selection flag.
  • borderValue: The border value that the border does not change.

5. Sample code:

//绘制包含区域函数
void drawState(Mat image, int number, Mat centroids, Mat stats, String string) {
    RNG rng(10086);
    vector<Vec4b> colors;
    for(int i=0;i<number;i++){
        //使用均匀分布的随机数确定颜色
        Vec4b vec4=Vec4b(rng.uniform(0,256),rng.uniform(0,256),rng.uniform(0,256),rng.uniform(0,256));
        colors.push_back(vec4);
    }
    //以不同颜色标记出不同的连通域
    for(int i=1;i<number;i++){
        //中心位置
        int center_x=centroids.at<double>(i,0);
        int center_y=centroids.at<double>(i,1);
        //矩形边框
        int x=stats.at<int>(i,CC_STAT_LEFT);
        int y=stats.at<int>(i,CC_STAT_TOP);
        int w=stats.at<int>(i,CC_STAT_WIDTH);
        int h=stats.at<int>(i,CC_STAT_HEIGHT);
        int area=stats.at<int>(i,CC_STAT_AREA);
        //中心位置绘制
        circle(image,Point(center_x,center_y),2,Scalar(0,255,0),2,8,0);
        //外接矩形
        Rect rect(x,y,w,h);
        rectangle(image,rect,colors[i],1,8,0);
        putText(image, format("%d",i),Point(center_x,center_y),FONT_HERSHEY_SIMPLEX,0.5,Scalar(255,0,255),1);
    }
    imwrite("/sdcard/DCIM/"+string+".png",image);
}

void Image_corrosion(Mat image){
    Mat img2;
    copyTo(image,img2,image);//克隆一个单独的图像,用于后期图像绘制
    Mat rice,riceBW;
    //将图像转成二值图像,用于统计连通域
    cvtColor(image,rice,COLOR_BGR2GRAY);
    threshold(rice,riceBW,50,255,THRESH_BINARY);

    Mat out,stats,centroids;
    //统计图像中连通域的个数
    int number= connectedComponentsWithStats(riceBW,out,stats,centroids,8,CV_16U);
    drawState(image,number,centroids,stats,"Uncorroded_connected");//绘制图像
    Mat strucr1= getStructuringElement(0,Size(3,3));//矩形结构元素
    //Mat strucr1= getStructuringElement(1,Size(3,3));//十字结构元素

    erode(riceBW,riceBW,strucr1);//对图像进行腐蚀
    number= connectedComponentsWithStats(riceBW,out,stats,centroids,8,CV_16U);
    drawState(img2,number,centroids,stats,"corroded_connected");
}

Guess you like

Origin blog.csdn.net/weixin_63357306/article/details/132759158