OpenCV Morphological Operation 1

Source: Internet
Author: User

Morphological operation refers to a series of image processing operations based on shape, including expansion, corrosion, binary value, open operation, closed operation, top hat algorithm, black hat algorithm, morphological gradient, etc., the most basic morphological operation is expansion and corrosion.

I. Expansion

The first need to clear a concept, expansion and corrosion are aimed at the image of the lighter areas, expansion is the area of light is more, and corrosion is the dark area more.

The function of expansion is mainly to eliminate the noise, separate the image elements, in the image operation, sometimes need to detect some shapes in the image, and these shapes are connected together, not good separate detection, expansion can cut these shapes (very small connection position), or the image has a small dark spots, Perhaps the effects on the camera, swelling, can also eliminate these small dark spots

The basic idea of expansion is that the image is convolution with a kernel function, and the maximum value in the result is taken as the result, so that the highlighted region in the image grows. The shape of this nucleus, the anchor point can be set, OPENCV provides the API for us to get the kernel.

Api:mat getstructuringelement (int core shape, size kernel size, point anchor location)

Note: The kernel shape can take square morph_rect, cross Morph_cross, oval Morph_ellipse

Anchor position Default value point ( -1,-1), take shape center

This API allows you to obtain the appropriate compute cores, and the next calculation of the expansion function is

Api:void dilate (source image, target image, expansion nucleus, anchor point, int iteration number, int boundary pattern, int boundary is constant when boundary value)

Note: The API supports In_place (the source image can do the destination image parameter, the algorithm modifies the data within the source image), the iteration number defaults to 1

Examples are as follows

Mat srcimage;//Expansion const int G_DILATEITERMAX = 100;//iteration number int G_ndilateitervalue;const int g_dilatecoremax = 100;//core size int G_ Ndilatecorevalue; Mat dilateimage;void ondilateitertrackbar (int pos,void* userData); void Ondilatecoresizetrackbar (int pos,void* USERDATA); int main (int argc,char* argv) {srcimage = Imread ("f:\\opencv\\opencvimage\\erode_dilate.jpg"); Namedwindow ( "src image"); Namedwindow ("dilate image"); g_ndilateitervalue = 1;g_ndilatecorevalue = 5;createtrackbar ("Inter count", " dilate image ", &g_ndilateitervalue, G_dilateitermax,ondilateitertrackbar); Createtrackbar (" Core size "," dilate Image ", &g_ndilatecorevalue, G_dilatecoremax,ondilatecoresizetrackbar); Ondilateitertrackbar (g_ ndilateitervalue,0); MoveWindow ("src image", 0, 0), MoveWindow ("dilate image", Srcimage.cols, 0); Imshow ("src image", Srcimage); Waitkey (0); return 0;} Adjust the number of iterations void Ondilateitertrackbar (int pos,void* userData) {if (pos = = 0| |   G_ndilatecorevalue = = 0) {imshow ("dilate image", srcimage);    } else {   if (g_ndilatecorevalue%2 = = 0) {g_ndilatecorevalue++;       } Mat core = Getstructuringelement (Morph_rect, Size (G_ndilatecorevalue,g_ndilatecorevalue));       Dilate (Srcimage, Dilateimage, Core,point ( -1,-1), g_ndilateitervalue);   Imshow ("dilate image", dilateimage); }}//adjust kernel size void ondilatecoresizetrackbar (int pos,void* userData) {if (pos = = 0 | | g_ndilateitervalue = = 0) {imshow   ("dilate image", srcimage);       } else {if (g_ndilatecorevalue%2 = = 0) {g_ndilatecorevalue++;       } Mat core = Getstructuringelement (Morph_rect, Size (G_ndilatecorevalue,g_ndilatecorevalue));       Dilate (Srcimage, Dilateimage, Core,point ( -1,-1), g_ndilateitervalue);   Imshow ("dilate image", dilateimage); }}

  

Two. Corrosion

Corrosion and expansion on the contrary, is to seek local minimum value of the operation, the light will be reduced, where the black will increase, in the image connected to close areas, eliminating the noise caused by the highlight

Api:void Erode (source, purpose, nucleus, Anchor point, iteration count, Edge type, edge is constant when boundary value);

Note: Corrosion and expansion APIs are in the same form

Using code

Corrosion Mat srcimage;const int g_erodeitermax = 100;int g_nerodeitervalue;const int g_erodecoremax = 100;int G_nErodeCoreValu E Mat erodeimage;void onerodeitertrackbar (int pos,void* userData); void Onerodecoresizetrackbar (int pos,void* userData); int main (int argc,char* argv) {srcimage = Imread ("f:\\opencv\\opencvimage\\erode_dilate.jpg"); Namedwindow ("src image"    );   Namedwindow ("erode image");   G_nerodeitervalue = 1;   G_nerodecorevalue = 5;   Createtrackbar ("Inter count", "Erode image", &g_nerodeitervalue, G_erodeitermax,onerodeitertrackbar);   Createtrackbar ("Core size", "Erode image", &g_nerodecorevalue, G_erodecoremax,onerodecoresizetrackbar);    Onerodeitertrackbar (g_nerodeitervalue, 0); MoveWindow ("src image", 0, 0); MoveWindow ("Erode image", Srcimage.cols, 0), imshow ("src image", srcimage); Waitkey (0); return 0;}  Adjust the number of iterations void Onerodeitertrackbar (int pos,void* userData) {if (pos = = 0 | | g_nerodecorevalue = = 0) {imshow ("erode   Image ", srcimage); } else {if (g_nerodecorevalue%2 = = 0) {g_nerodecorevalue++;       } Mat core = Getstructuringelement (Morph_rect, Size (G_nerodecorevalue,g_nerodecorevalue), point ( -1,-1));       Erode (Srcimage, Erodeimage, Core,point ( -1,-1), g_nerodeitervalue);   Imshow ("Erode image", erodeimage); }}//adjust kernel size void onerodecoresizetrackbar (int pos,void* userData) {if (pos = = 0 | | g_nerodeitervalue = = 0) {imshow ("   Erode image ", srcimage);       } else {if (g_nerodecorevalue%2 = = 0) {g_nerodecorevalue++;       } Mat core = Getstructuringelement (Morph_rect, Size (G_nerodecorevalue,g_nerodecorevalue), point ( -1,-1));       Erode (Srcimage, Erodeimage, Core,point ( -1,-1), g_nerodeitervalue);   Imshow ("Erode image", erodeimage); }}

  

Three. Morphological filtering algorithm

The advanced operations of morphology are often built on the basis of expansion and corrosion operations

1. Open operation: Open operation is a first corrosion, after the expansion of the process, used in the image to eliminate small objects, at the slender point of the separation of objects at the edge of the smooth large objects while not significantly changing the volume of the object.

2. Closed operation: The process of first expansion and corrosion, can be used to eliminate small black holes in the object

3. Morphological gradient: The difference between the expansion map and the corrosion map, the operation of the two-value image, the edges of the clumps can be highlighted, and morphological gradients can be used to preserve the object's edge contour.

4. Top hat: The difference between the source image and the result of the open operation is often used to separate patches brighter than the neighboring point, in a specific large background of a picture, while small objects have a more regular situation, you can use the Top_hat operation to extract the background

5. Black Hat: The difference between the result of the closed operation and the source image highlights the darker areas around the contour of the source image, often used to separate patches darker than the neighboring point.

Core Api:void Morpholgyex (source, target, int operation Flag, Mat morphological operation kernel, point anchor, int iteration number, int boundary pattern, int boundary is constant boundary value).

Note: The values of morphological operation flags are as follows: Morph_open Open operation Morph_close closed operation Morph_grenient morphological gradient morph_tophat cap morph_blackhat black Hat morph_erode corrosion Morph_ Dilate expansion

The morphological operation core is the kernel used in the front expansion corrosion.

Examples of use are:

1. Open operation closed operation morphology gradient three union

SOURCE ¡ä Figure ª? Mat srcimage;//Open a op? const int G_openitermax = 100;int g_nopenitervalue;const int g_opencoremax = 100;int G_nopencorevalu E Mat openimage;void onopenitertrackbar (int pos,void* userData); void Onopencoresizetrackbar (int pos,void* userData);// Closed à? const int g_closeitermax = 100;int g_ncloseitervalue;const int g_closecoremax = 100;int g_nclosecorevalue; Mat closeimage;void oncloseitertrackbar (int pos,void* userData); void Onclosecoresizetrackbar (int pos,void* userData) ¡ì Ladder ¬y degree ¨¨const int g_gredientitermax = 100;int g_ngredientitervalue;const int g_gredientcoremax = 100;int g_ Ngredientcorevalue; Mat gredientimage;void ongredientitertrackbar (int pos,void* userData); void Ongredientcoresizetrackbar (int pos,void*      USERDATA); int main (int argc,char* argv[]) {srcimage = Imread ("f:\\opencv\\opencvimage\\morpholgy.jpg");   G_nopenitervalue = 1;   G_nopencorevalue = 5;   Namedwindow ("open image"); Createtrackbar ("ITER count", "open image", &g_nopenitervalue, G_openitermax,onopenitertrackbar,0);   Createtrackbar ("Core size", "open image", &g_nopencorevalue, g_opencoremax,onopencoresizetrackbar,0);      Onopencoresizetrackbar (g_nopencorevalue, 0);   G_nclosecorevalue = 5;   G_ncloseitervalue = 1;   Namedwindow ("close image");   Createtrackbar ("ITER count", "Close image", &g_ncloseitervalue, g_closeitermax,oncloseitertrackbar,0);   Createtrackbar ("Core size", "Close image", &g_nclosecorevalue, g_closecoremax,onclosecoresizetrackbar,0);      Onclosecoresizetrackbar (g_nclosecorevalue, 0);   G_ngredientcorevalue = 5;   G_ngredientitervalue = 1;   Namedwindow ("gredient image"); Createtrackbar ("ITER count", "gredient image", &g_ngredientitervalue, G_gredientitermax,ongredientitertrackbar,   0); Createtrackbar ("Core size", "gredient image", &g_ngredientcorevalue, g_gredientcoremax,ongredientitertrackbar,0   );      Ongredientitertrackbar (g_ngredientitervalue, 0);     Imshow ("src image", srcimage);   MoveWindow ("src image", 0, 0); MoveWindow ("OpeN Image ", Srcimage.cols, 0);   MoveWindow ("Close image", srcimage.cols*2, 0);      MoveWindow ("gredient image", srcimage.cols*3, 0);   Waitkey (0); return 0;} void Onopenitertrackbar (int pos,void* userData) {if (G_nopencorevalue = = 0| |   G_nopenitervalue = = 0) {imshow ("open image", srcimage);       } else {if (g_nopencorevalue%2 = = 0) g_nopencorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nopencorevalue,g_nopencorevalue));       Morphologyex (Srcimage, Openimage, Morph_open, Core,point ( -1,-1), g_nopenitervalue);   Imshow ("Open image", openimage); }}void onopencoresizetrackbar (int pos,void* userData) {if (G_nopencorevalue = = 0| |   G_nopenitervalue = = 0) {imshow ("open image", srcimage);       } else {if (g_nopencorevalue%2 = = 0) g_nopencorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nopencorevalue,g_nopencorevalue)); Morphologyex (Srcimage, Openimage, Morph_open, Core,point ( -1,-1), G_nopenItervalue);   Imshow ("Open image", openimage); }}void oncloseitertrackbar (int pos,void* userData) {if (G_nclosecorevalue = = 0| |   G_ncloseitervalue = = 0) {imshow ("Close image", srcimage);       } else {if (g_nclosecorevalue%2 = = 0) g_nclosecorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nclosecorevalue,g_nclosecorevalue));       Morphologyex (Srcimage, Closeimage, Morph_close, Core,point ( -1,-1), g_ncloseitervalue);   Imshow ("Close image", closeimage); }}void onclosecoresizetrackbar (int pos,void* userData) {if (G_nclosecorevalue = = 0| |   G_ncloseitervalue = = 0) {imshow ("Close image", srcimage);       } else {if (g_nclosecorevalue%2 = = 0) g_nclosecorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nclosecorevalue,g_nclosecorevalue));       Morphologyex (Srcimage, Closeimage, Morph_close, Core,point ( -1,-1), g_ncloseitervalue);   Imshow ("Close image", closeimage); }}void OngredientitertracKbar (int pos,void* userData) {if (G_ngredientcorevalue = = 0| |   G_ngredientitervalue = = 0) {imshow ("gredient image", srcimage);       } else {if (g_ngredientcorevalue%2 = = 0) g_ngredientcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_ngredientcorevalue,g_ngredientcorevalue));       Morphologyex (Srcimage, Gredientimage, Morph_gradient, Core,point ( -1,-1), g_ngredientitervalue);   Imshow ("gredient image", gredientimage); }}void ongredientcoresizetrackbar (int pos,void* userData) {if (G_ngredientcorevalue = = 0| |   G_ngredientitervalue = = 0) {imshow ("gredient image", srcimage);       } else {if (g_ngredientcorevalue%2 = = 0) g_ngredientcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_ngredientcorevalue,g_ngredientcorevalue));       Morphologyex (Srcimage, Gredientimage, Morph_gradient, Core,point ( -1,-1), g_ngredientitervalue);   Imshow ("gredient image", gredientimage); }}

  

2. Top Hat Black Hat combined

Mat srcimage;//top £¤ cap ¡àtophatconst int g_tophatitermax = 100;int g_ntophatitervalue;const int g_tophatcoremax = 100;int G_n Tophatcorevalue; Mat tophatimage;void ontophatitertrackbar (int pos,void* userData); void Ontophatcoresizetrackbar (int pos,void* UserData);//black ¨² cap ¡àconst int g_blackhatitermax = 100;int g_nblackhatitervalue;const int g_blackhatcoremax = 100;int g_ Nblackhatcorevalue; Mat blackhatimage;void onblackhatitertrackbar (int pos,void* userData); void Onblackhatcoresizetrackbar (int pos,void*      USERDATA); int main (int argc,char* argv[]) {srcimage = Imread ("f:\\opencv\\opencvimage\\morpholgy.jpg");   G_ntophatitervalue = 1;   G_ntophatcorevalue = 5;   Namedwindow ("tophat image");   Createtrackbar ("ITER count", "tophat image", &g_ntophatitervalue, g_tophatitermax,ontophatitertrackbar,0);   Createtrackbar ("Core size", "tophat image", &g_ntophatcorevalue, g_tophatcoremax,ontophatcoresizetrackbar,0);      Ontophatcoresizetrackbar (g_ntophatcorevalue, 0); G_nblackhatcorevalue =5;   G_nblackhatitervalue = 1;   Namedwindow ("Blackhat image"); Createtrackbar ("ITER count", "Blackhat image", &g_nblackhatitervalue, G_blackhatitermax,onblackhatitertrackbar,   0); Createtrackbar ("Core size", "Blackhat image", &g_nblackhatcorevalue, G_blackhatcoremax,   onblackhatcoresizetrackbar,0);      Onblackhatcoresizetrackbar (g_nblackhatcorevalue, 0);      Imshow ("src image", srcimage);   MoveWindow ("src image", 0, 0);   MoveWindow ("Tophat image", Srcimage.cols, 0);      MoveWindow ("Blackhat image", srcimage.cols*2, 0);   Waitkey (0); return 0;} void Ontophatitertrackbar (int pos,void* userData) {if (G_ntophatcorevalue = = 0| |   G_ntophatitervalue = = 0) {imshow ("tophat image", srcimage);       } else {if (g_ntophatcorevalue%2 = = 0) g_ntophatcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_ntophatcorevalue,g_ntophatcorevalue));       Morphologyex (Srcimage, Tophatimage, Morph_tophat, Core,point ( -1,-1), g_ntophatitervalue); Imshow ("tophat image", tophatimage); }}void ontophatcoresizetrackbar (int pos,void* userData) {if (G_ntophatcorevalue = = 0| |   G_ntophatitervalue = = 0) {imshow ("tophat image", srcimage);       } else {if (g_ntophatcorevalue%2 = = 0) g_ntophatcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_ntophatcorevalue,g_ntophatcorevalue));       Morphologyex (Srcimage, Tophatimage, Morph_tophat, Core,point ( -1,-1), g_ntophatitervalue);   Imshow ("Tophat image", tophatimage); }}void onblackhatitertrackbar (int pos,void* userData) {if (G_nblackhatcorevalue = = 0| |   G_nblackhatitervalue = = 0) {imshow ("Blackhat image", srcimage);       } else {if (g_nblackhatcorevalue%2 = = 0) g_nblackhatcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nblackhatcorevalue,g_nblackhatcorevalue));       Morphologyex (Srcimage, Blackhatimage, Morph_blackhat, Core,point ( -1,-1), g_nblackhatitervalue); Imshow ("Blackhat image", BlackhatiMage); }}void onblackhatcoresizetrackbar (int pos,void* userData) {if (G_nblackhatcorevalue = = 0| |   G_nblackhatitervalue = = 0) {imshow ("Blackhat image", srcimage);       } else {if (g_nblackhatcorevalue%2 = = 0) g_nblackhatcorevalue++;       Mat core = Getstructuringelement (Morph_rect, Size (G_nblackhatcorevalue,g_nblackhatcorevalue));       Morphologyex (Srcimage, Blackhatimage, Morph_blackhat, Core,point ( -1,-1), g_nblackhatitervalue);   Imshow ("Blackhat image", blackhatimage); }}

  

OpenCV Morphological Operation 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.