Now, continuing on the same concept of 2D Convolution, Eroding and Dilating works on the form and structure of the image.
In previous tutorials, we slid a kernel over an image and did operations like summing up all the element-wise multiplication pairs and storing it in an anchor point.
In this tutorial, what we are gonna do is to calculate the minimum(Eroding) and maximum(Dilating) values of the element-wise multiplication and store it the anchor point.
For simplicity, we take all the kernel elements as ones.
Try visualizing this.
Now, slide the kernel over your image(2D Convolution).
Because all kernel elements are ones, applying this kernel(in Eroding) means replacing each pixel value with the minimum value in a rectangular region surrounding the pixel. You can imagine that this will cause the black areas in the image to “encroach” into the white areas (because pixel value for white is higher than that for black).
Dilating the image is the same, the only difference being that the response if defined as the maximum of
element-wise multiplications instead of minimum. This will cause the white regions to encroach into black regions.
Input image:

Result:
Eroding
Dilating
In previous tutorials, we slid a kernel over an image and did operations like summing up all the element-wise multiplication pairs and storing it in an anchor point.
In this tutorial, what we are gonna do is to calculate the minimum(Eroding) and maximum(Dilating) values of the element-wise multiplication and store it the anchor point.
For simplicity, we take all the kernel elements as ones.
Try visualizing this.
Now, slide the kernel over your image(2D Convolution).
Because all kernel elements are ones, applying this kernel(in Eroding) means replacing each pixel value with the minimum value in a rectangular region surrounding the pixel. You can imagine that this will cause the black areas in the image to “encroach” into the white areas (because pixel value for white is higher than that for black).
Dilating the image is the same, the only difference being that the response if defined as the maximum of
element-wise multiplications instead of minimum. This will cause the white regions to encroach into black regions.
#include#include #include using namespace std; using namespace cv; int main() { Mat img=imread("images/j.jpg"),img2,img3; Mat st_elem=getStructuringElement(MORPH_RECT,Size(5,5)); //I am defining my own kernel matrix here //MORPH_RECT: Rectangular Morphological Matrix //Size(5,5): 5x5 size matrix // You can vary the size and shape erode(img,img2,st_elem); dilate(img,img3,st_elem); namedWindow("Image"); namedWindow("Image2"); namedWindow("Image3"); imshow("Image",img); imshow("Image2",img2); imshow("Image3",img3); while(char(waitKey(0))!='q'){} return 0; }
Input image:

Result:
Eroding
Dilating
-------------------------------------------------
More sources:
FILTERING(Optional)


No comments:
Post a Comment