OpenCV morphological operation

Source: Internet
Author: User
OpenCV morphological OperationCategory: Opencv2010-07-30 10:23 4548 people read reviews (4) Favorites Report DST image processing algorithm null grid tool

OpenCV morphological operation

First, the basic principle of image corrosion expansion refinement

1. The basic principle of image refinement
The concept of morphological processing of ⑴ image
Morphological processing in Digital image processing refers to the use of digital morphology as a tool to extract image components that are useful for expressing and depicting area shapes, such as boundaries, skeletons, and convex shells, as well as morphological filtering, thinning, and pruning for preprocessing or post-processing. The main interest in image morphology processing is two value image.
In a binary image, All Black pixels are set as the complete morphological description of the image, and the components of the binary image are Z2 elements. Assuming binary image A and morphological processing of structural element B is defined on the Cartesian grid of the set, the grid value of 1 is the element of the collection, when the origin of the structure element is shifted to point (X, y), as sxy, for the sake of simplicity, the structure element is 3x3, and all 1, under this restriction, The logical operation is the decision to output the result.

The logical operation of ⑵ binary image
Although the logic operation is very simple in nature, it is a powerful supplementary method to realize the image processing algorithm based on morphology. The main logical operations used in image processing are: and, or and non (complement), which can be combined to form other logical operations.

⑶ Expansion and corrosion
Expansion and corrosion These two operations are the basis of morphological processing, and many morphological algorithms are based on these two operations.
① expansion
is based on the image of B relative to its own origin and shifted by Z to the image. A by B expansion is the set of all displacement Z, in this way, and a at least one element is overlapping. We can rewrite the above style as:
Structural element B can be thought of as a convolution template, except that the expansion is based on set operations, and convolution is based on arithmetic operations, but the process is similar.
⑴ with structural element B, scan every pixel of image a
⑵ the "and" operation with the structure element and the two value image it covers
⑶ If all is 0, the pixel of the resulting image is 0. Otherwise the 1

② corrosion
The entire process of etching A and b,b to a in Z is as follows:
⑴ with structural element B, scan every pixel of image a
⑵ the "and" operation with the structure element and the two value image it covers
⑶ If all is 1, the pixel of the resulting image is 1. Otherwise the 0
The result of the corrosion treatment is to reduce the original two value image by one turn.

⑷ hit (match) or hit-no-change
Suppose that set a is a collection of 3 subsets X, Y and Z, the goal of hitting (matching) is to find the position of x in a, we set X to be enclosed in a small window w, and the local background of the x associated with W is defined as the difference of the set (W-x), Then x in a can be accurately fitted position set is by x pair A of corrosion by (W-x) to a complement set AC corrosion intersection, this intersection is we are looking for position, we use set B to represent the set of x and X, we can make b= (B1,B2), here b1=x,b2= (w-x) , a match to B in a can be expressed as:
A⊙b
We call morphology on the hit or hit not in the transformation.

⑸ Opening and closing operation
Open operation is the first corrosion, post-expansion treatment.

Closed operation is the first expansion, post-corrosion treatment.

(6) Refinement
Image thinning is generally used as an image preprocessing technology to extract the skeleton of the source image, that is, the original image of the line width greater than 1 pixels of the line refinement into only one pixel width, the formation of a "skeleton", the formation of the skeleton can be relatively easy to analyze the image, such as the extraction of image features.
The basic idea of refinement is "layers of deprivation", that is, from the edge of the line to start a layer of inward deprivation, until the line left a pixel so far. The image thinning greatly compresses the original image data amount, and maintains its basic topological structure unchanged, which lays the foundation for the application of feature extraction in character recognition. The refinement algorithm should meet the following conditions:
① the bar area into a thin line;
② Thin Line and the center of the original bar area;
The ③ thin line should maintain the topological characteristics of the original image.
Refinement is divided into serial refinement and parallel refinement, the serial refinement is to detect the point to meet the refinement conditions, while removing the refinement point, parallel refinement is to detect the refinement point when the deletion of the point is only marked, and in the detection of the full image after the removal of the point to be refined.
The commonly used image thinning algorithm has hilditch algorithm, Pavlidis algorithm and Rosenfeld algorithm.
Note: Before the thinning algorithm, the image should be binary, that is, the image contains only "black" and "white" two colors.

Specific detailed image morphological data reference: http://wenku.baidu.com/view/1923d18fcc22bcd126ff0ccc.html

Second, OpenCV morphological operation correlation function

1, Morphologyex Advanced morphological transformation

void Cvmorphologyex (const cvarr* SRC, cvarr* DST, cvarr* temp,
iplconvkernel* element, int operation, int iterations=1);
Src
Enter an image.
Dst
The output image.
Temp
Temporary images, which in some cases require
Element
Structural elements
Operation
Types of morphological operations:
Cv_mop_open-Open operation
Cv_mop_close-closed operation
Cv_mop_gradient-Morphological gradients
Cv_mop_tophat-"Top Hat"
Cv_mop_blackhat-"Black Hat"
Iterations
Expansion and corrosion times.
The function Cvmorphologyex a number of advanced morphological transformations based on the basic operation of expansion and corrosion:

Open operation
Dst=open (src,element) =dilate (Erode (src,element), Element)
Closed operation
Dst=close (src,element) =erode (dilate (src,element), Element)
Morphological gradients
Dst=morph_grad (src,element) =dilate (src,element)-erode (src,element)
"Top Hat"
Dst=tophat (src,element) =src-open (src,element)
"Black Hat"
Dst=blackhat (src,element) =close (src,element)-src
Temporary image temp is required in the morphological gradient as well as in the In-place mode for "Top Hat" and "Black Hat" operations.

2. dilate use arbitrary structural elements to expand the image

void cvdilate (const cvarr* SRC, cvarr* DST, iplconvkernel* element=null, int iterations=1);
Src
Enter an image.
Dst
The output image.
Element
A structural element used for expansion. If NULL, a 3x3 rectangular structure element is used
Iterations
Number of expansions
The function Cvdilate expands the input image with the specified structure element, which determines the neighborhood shape for each pixel with the smallest value:
Dst=dilate (src,element): DST (x, y) =max ((× ', ') ' in Element) src (x+x ', Y+y ')
function Support (in-place) mode. Expansion can be repeated (iterations) times. For color images, each color channel is processed separately.

3, erode use any structural elements corrosion image

void Cverode (const cvarr* SRC, cvarr* DST, iplconvkernel* element=null, int iterations=1);
Src
Enter an image.
Dst
The output image.
Element
Structural elements for corrosion. If NULL, a 3x3 rectangular structure element is used
Iterations
Number of corrosion
The function cverode the input image using the specified structural element, which determines the neighborhood shape for each pixel with the minimum value:
Dst=erode (src,element): DST (x, y) =min ((× ', ') ' in Element) src (x+x ', Y+y ')
The function may be local, without the need for additional storage space. Corrosion can be repeated (iterations) times. For color images, each color channel is processed separately.

Note: Createstructuringelementex creates structural elements; releasestructuringelement removes structural elements.

Three, OpenCV morphological example code:

1, corrosion, expansion, open operation, closed operation

Content reference: Http://blog.csdn.net/gnuhpc/archive/2009/06/21/4286177.aspx

/*******************************

Mathematical form operations, the most common basic operations have seven kinds,

respectively: corrosion, expansion, open operation, closed operation, hit, refinement and coarsening,

They are the basis of all morphology.

********************************/

#include "Cv.h"

#include "highgui.h"

#include <stdlib.h>

#include <stdio.h>

Iplimage *src=0;

Iplimage *dst=0;

Iplconvkernel *element=0;//declares a structural element

int element_shape=cv_shape_rect;//elements of a rectangular shape

int max_iters=10;

int open_close_pos=0;

int erode_dilate_pos=0;

void openclose (int pos)

{

int n=open_close_pos-max_iters;

int an=n>0?n:-n;

element = Cvcreatestructuringelementex (an*2+1, an*2+1,an,an,element_shape,0);//Create Structure element

if (n<0)

{

Cverode (src,dst,element,1);//Corrosion image

Cvdilate (dst,dst,element,1);//Expansion image

}

Else

{

Cvdilate (dst,dst,element,1);//Expansion image

Cverode (src,dst,element,1);//Corrosion image

}

Cvreleasestructuringelement (&element);

Cvshowimage ("Open/close", DST);

}

void erodedilate (int pos)

{

int n=erode_dilate_pos-max_iters;

int an=n>0?n:-n;

element = Cvcreatestructuringelementex (an*2+1,an*2+1,an,an,element_shape,0);

if (n<0)

{

Cverode (src,dst,element,1);

}

Else

{

Cvdilate (src,dst,element,1);

}

Cvreleasestructuringelement (&element);

Cvshowimage ("Erode/dilate", DST);

}

int main (int argc,char **argv)

{

Char *filename =ARGC ==2?argv[1]:(char *) "lena.jpg";

if (src = cvloadimage (filename,1)) = = 0)

return-1;

Dst=cvcloneimage (SRC);

Cvnamedwindow ("Open/close", 1);

Cvnamedwindow ("Erode/dilate", 1);

Open_close_pos = Erode_dilate_pos = Max_iters;

Cvcreatetrackbar ("Iterations", "Open/close", &open_close_pos,max_iters*2+1,openclose);

Cvcreatetrackbar ("Iterations", "Erode/dilate", &erode_dilate_pos,max_iters*2+1,erodedilate);

for (;;)

{

int C;

OpenClose (Open_close_pos);

Erodedilate (Erode_dilate_pos);

C= cvwaitkey (0);

if (c==27)

{

Break

}

Switch (c) {

Case ' E ':

Element_shape=cv_shape_ellipse;

Break

Case ' R ':

Element_shape=cv_shape_rect;

Break

Case '/R ':

Element_shape= (element_shape+1)%3;

Break

Default

Break

}

}

Cvreleaseimage (&SRC);

Cvreleaseimage (&DST);

Cvdestroywindow ("Open/close");

Cvdestroywindow ("Erode/dilate");

return 0;

}

/*****************************

Corrosion and swelling, it seems like a pair of reciprocal operations, in fact, the two operations do not have an inverse relationship.

Open and closed operations are based on the irreversible nature of corrosion and expansion.

The process of first corrosion and swelling is called open operation.

The closed operation is obtained by the execution of another different sequence of corrosion and swelling,

The closed operation is the first expansion of the corrosion process, its function is to fill the body of small holes, connect adjacent objects, smooth its boundaries,

At the same time, the area is not obviously changed.

******************************/

2, OpenCV realize two value image refinement

Content reference: Http://blog.csdn.net/byxdaz/archive/2010/06/02/5642669.aspx

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.