Morphological filtering of "computer vision"

Source: Internet
Author: User

Morphological filtering of "computer vision"

Label (Space delimited): "Image processing" "Signal processing"

Copyright NOTICE: This article for Bo Master original article, reprint please indicate source http://blog.csdn.net/lg1259156776/.

Note: This paper mainly wants to find out the application of morphological filtering in image processing and signal processing, and it is very intuitive to obtain the effect of opening and closing operation through corrosion expansion, while in real-time filtering of data, morphological filtering can be used.

Basic knowledge of morphological filtering

Principle: In the Special domain operation form-the structural element (sturcture elements), the region corresponding to the two value image is given a specific logical operation at each pixel location. The arithmetic structure is the corresponding pixel of the output image. The effect depends on the structure element size content and the logical operation nature.

Expansion, corrosion, opening and closing operations are the most basic transformations of mathematical morphology.

Structural elements are simply defined as the structure of a pixel (shape) and an Origin (also known as an anchor point), using morphological filtering involves applying this structural element to each pixel of an image, and when the origin of the structure element is aligned with the given pixel, it defines a set of pixels for morphological operations in the intersection of the image. In principle, structural elements can be any shape, but usually use simple shapes such as squares, circles, and diamonds, while the origin is centered (based on efficiency considerations).

Corrosion and expansion Two filter operations are also performed on the set of pixels around each pixel (neighborhood), which is defined by the structure element. When applied to a given pixel, the anchor point of the structure element is aligned with the pixel's position, and all the pixels that intersect with him are included in the current pixel collection. Corrosion replaces the current pixel as the smallest pixel value found in the pixel collection, and the expansion is replaced by the largest pixel value found in the Pixel collection. Of course, for binary images, each pixel can only be replaced by white pixels or black pixels.

This section of the discussion, you can refer to the OPENCV Computer Vision Programming Manual. For the difference between corrosion and expansion, can be imagined, corrosion, if the structure of a given pixel touches the background, then the pixel is set to the background, and in the case of expansion, if the foreground is touched, the pixel is set to the foreground, so it is obvious that the corrosion operation after the size of the object will be reduced, The size of the object increases after the expansion operation, while some of the inner holes are filled.

In fact, etching an image two times is like letting structural elements inflate themselves and then corrode the same image, which in turn is appropriate for expansion.

Here are two ways to say:

    1. The corrosion operation of the image is equal to the negative of the expansion operation of the image negative;
    2. The expansion operation of the image is equal to the negative of the corrosion operation of the image negative;

In fact, it is the corrosion of the target, which is equivalent to the expansion of the background, and vice versa.

The simplest application of corrosion is to eliminate irrelevant details, while the simplest application of swelling is bridging the cracks. Open operation is the first corrosion and re-expansion, open operations generally break the narrow gap and eliminate the thin protrusion, while the closed operation usually eliminates the narrow gap and the long fine chasm, eliminate small holes, and fill the contour line break. The combination of open and closed operations can make the contour of the object smooth .

Open operation is the first corrosion and then expansion, and the closed operation is the first expansion and then corrosion;
When examining the results of a closed filter , it is possible to see a small hole in the white foreground object being filled, which simultaneously connects multiple adjacent objects, and basically, holes or gaps that cannot completely contain structural elements will be removed by the filter . In turn, the open filter removes the smaller object from the scene because it cannot completely contain structural elements.

These filters are usually used in object detection, and the closed filter re-connects the objects that are mistakenly segmented into fragments, while the open filter removes the small pixel blocks (blobs) caused by the image noise points. Therefore, it is helpful to use them in video sequences, if the two value image being tested is used in succession, the resulting image will only show the main objects in the scene. If noise is preferred, it can be opened and then closed, but it is possible to remove some scattered objects.

It is important to note that there is no effect on using the same open or closed operation for an image multiple times, since the first closed (open) operation fills the hole in the image and applies the same filter again without any change to the image. In mathematical terms, these operations are idempotent.

Edge and corner detection of images using morphological filtering

Morphological filtering can be used to detect specified features in an image.
One way to compare images is to view grayscale images as "contours" ( such as watershed image segmentation algorithms ): Bright areas represent peaks, and dark areas represent valleys, and the edges of images correspond to cliffs. If you corrode an image, it will cause the valley to be expanded and the cliffs reduced. Conversely, if you inflate an image, the cliff will increase. But in both cases, the middle part (large valleys and plateaus) remains essentially the same.

On the basis of the above understanding, if we do poorly on the result of corrosion and swelling of the image, we can extract the boundary of the image: Because of the boundary region, they are completely different. (In fact, we can also use the result of corrosion or swelling with the source image to make similar results, but the extracted boundary will be relatively thin). As you can see, the larger the structure element, the thicker the boundary. In OpenCV, this work can be accomplished by setting the 4th parameter of the morphological operation function Morphologyex to morph_gradient.

The use of morphological manipulation to obtain corner points is slightly more complex, it tried four different structural elements, the basic method is to a picture first corrosion, in the expansion . However, these two operations use different structural elements . The selection of these structural elements allows the lines to remain unchanged, but because of their respective effects, the edges at the corners are affected. We combine a picture to illustrate:

The first picture is the original image. After being inflated by a cross-shaped element, the edge of the block is expanded, and the cross-shaped element is not affected because it does not hit the corner point. The middle block describes the result, and the expanded image is then corroded by the diamond element, which restores most of the edges to the original position, but the previously not-inflated corner is pushed inward, and then the left square is visible, and he lacks the obvious corner points. The same process is repeated by X-shape and square elements. The two element structure is a rotated version of the previous element, which captures the corner point after the 45° rotation. Finally, the results of the two-time process are evaluated and the corner features are extracted.

The code can refer to reference 3.

Image processing of morphological filtering

General corrosion operations on the two-value graph for processing, corrosion operations such as whether the central location of the pixel point is the same as the surrounding area of the pixel color (that is, whether it is a white point, that is, whether the value is 255), if consistent, then the point becomes black (the value is 0)

Corrosion operations in the OPENCV:

CVAPI(void)  cvErode( const CvArr* src, CvArr* dst,                      IplConvKernel* element CV_DEFAULT(NULL),                      int iterations CV_DEFAULT(1) );

The first two parameters are familiar, the third parameter is used to pass the template information, the default is (NULL), that is 3*3 template, the fourth parameter is the number of iterations (that is, the corrosion operation done several times);

The expansion operation in the OPENCV is actually an anti-operation of corrosion:

CVAPI(void)  cvDilate( const CvArr* src, CvArr* dst,                       IplConvKernel* element CV_DEFAULT(NULL),                       int iterations CV_DEFAULT(1) );

Test code:
#include "stdafx.h"
#include "Cv.h"
#include "highgui.h"

int main(){    IplImage *img= cvLoadImage("C:/fu.jpg");//读取图片    cvNamedWindow("Example1",CV_WINDOW_AUTOSIZE);    cvNamedWindow("Example2",CV_WINDOW_AUTOSIZE);    cvNamedWindow("Example3",CV_WINDOW_AUTOSIZE);    cvShowImage("Example1",img);//在Example1显示图片    //    cvCopy(img,temp);    IplImage* temp=cvCreateImage( //创建一个size为image,三通道8位的彩色图        cvGetSize(img),        IPL_DEPTH_8U,        3        );    cvErode(img,temp,0,1);//腐蚀    cvShowImage("Example2",temp);    cvDilate(img,temp,0,1);//膨胀    cvShowImage("Example3",temp);    cvWaitKey(0);//暂停用于显示图片    cvReleaseImage(&img);//释放img所指向的内存空间并且    cvDestroyWindow("Example1");    cvDestroyWindow("Example2");    cvDestroyWindow("Example3");    return 0;}


All of these are handled in the case of template 3*3, and we need to make our own templates if we want to use our own defined templates.

CVAPI(IplConvKernel*)  cvCreateStructuringElementEx(            int cols, int  rows, int  anchor_x, int  anchor_y,            int shape, int* values CV_DEFAULT(NULL) );

The first two parameters are the size of the definition template, the next two parameters are the coordinates of the reference point (for example, the default 3*3 template reference point coordinates is 2*2), the fifth parameter is the type of the template (can be a rectangle, a cross, an ellipse, or even a user-defined shape), the last parameter is when using a custom shape, The shape of the template is passed through value.

Type of Template:

CVAPI(void)  cvReleaseStructuringElement( IplConvKernel** element ); //释放模板所占用的内存

Custom 5*5, reference point (3,3) for the test code of the rectangle template:

  #include "stdafx.h" #include "cv.h" #include "highgui.h" int main () {iplimage *img= cvloadimage ("C:/fu.jpg")    ;//Read picture Cvnamedwindow ("Example1", cv_window_autosize);    Cvnamedwindow ("Example2", cv_window_autosize);    Cvnamedwindow ("Example3", cv_window_autosize);    Cvshowimage ("Example1", IMG);//display pictures in Example1//Cvcopy (IMG,TEMP);    iplimage* temp=cvcreateimage (//Create a size of image, three-Channel 8-bit color graph cvgetsize (img), ipl_depth_8u, 3);    Iplconvkernel * MyModel;    Mymodel=cvcreatestructuringelementex (//Custom 5*5, reference point (3,3) rectangle template 5,5,2,2,cv_shape_rect);    Cverode (img,temp,mymodel,1);    Cvshowimage ("Example2", temp);    Cvdilate (img,temp,mymodel,1);    Cvshowimage ("Example3", temp);    Cvwaitkey (0);//pause for displaying picture cvreleasestructuringelement (&mymodel);    Cvreleaseimage (&img);//Release the memory space that the IMG points to and Cvdestroywindow ("Example1");    Cvdestroywindow ("Example2");    Cvdestroywindow ("Example3"); return 0;}  


Signal processing of morphological filtering

The mathematical morphology method can be understood as a small ball with a certain diameter rolled over a specific path, the various signals can be regarded as the path of the pits. Because all the noise has a common feature one by one high-frequency low-peak (they constitute very complex, by various mixing in the eye signal of other components or eyeball fast, small, space size not more than 1 "motion caused), these pits diameter is significantly smaller than the diameter of the small ball, Therefore, the rolling trajectory of the ball sphere will not be disturbed by noise. One by one the ball spherical globe rolling trajectory can be regarded as the signal after the mathematical morphology processing. The mathematical morphology operators used in this system include corrosion operation, expansion operation, open operation and closed operation.

The simplest application of corrosion is to eliminate irrelevant details, while the simplest application of swelling is bridging the cracks. Open operation is the first corrosion and re-expansion, open operations generally break the narrow gap and eliminate the thin protrusion, while the closed operation usually eliminates the narrow gap and the long fine chasm, eliminate small holes, and fill the contour line break. The combination of open and closed operations can make the contour of the object smooth .

#include <stdio.h> #include <fcntl.h>//#include <sys/types.h>//#include <sys/stats.h># Include <time.h> #define N 5//structural elements. The size setting is based on the number of midpoint of a period of filtering the waveform.          For example: Sample rate 250, filter 50hz,//Because 50hz has 5 points in a cycle, so n is set to 5.    float x[3*n+2]={0.0};void openoperate (float input[],float dilation[])//Open operation: First corrosion and then expansion {int i,k,t;    float tmp;    t=2*n+3;    float erosion[2*n+3];        for (k=0;k<t;k++) {tmp=input[k];        for (i=k+1;i<k+n;i++) {if (Tmp>input[i]) tmp=input[i];    } erosion[k]=tmp;    } t=t-3;        for (k=0;k<t;k++) {tmp=erosion[k];        for (i=k+1;i<k+n;i++) {if (Tmp<erosion[i]) tmp=erosion[i];    } dilation[k]=tmp;    }}float closeoperate (float input[])//closed operation: First expands and then corrodes {int i,k,t;    float tmp;    t=n*2-1;    float Dilation[n];        for (k=0;k<n;k++) {tmp=input[k];           for (i=k+1;i<k+n;i++) { if (Tmp<input[i]) tmp=input[i];    } dilation[k]=tmp;    } Tmp=dilation[0];    for (k=1;k<n;k++) {if (Tmp>dilation[k]) tmp=dilation[k]; } return TMP;    int main () {clock_t start,end;    Double duration;    FILE *fd,*m_fd;    float buffer;    float Filter_data;    float openresult[2*n-1];    M_fd=fopen ("D:/data.txt", "r+");        if (m_fd==null) {perror ("open error!");    return-1;      } fd=fopen ("D:/data1.txt", "w+");        if (fd==null) {perror ("open error!");    return-1;    } start=clock ();          while (FSCANF (M_FD, "%f", &buffer)!=eof) {x[3*n+1]=buffer;          Openoperate (X,openresult);          Filter_data=closeoperate (Openresult);          fprintf (FD, "%f", filter_data);    for (int y=0;y<3*n+1;y++) x[y]=x[y+1];    } end=clock ();    Duration= (Double) (End-start)/clocks_per_sec;    printf ("%f seconds\n", duration);    Fclose (FD);    Fclose (M_FD); return 0;}

As shown in the results, morphological filtering was performed on the interference of the original ocular data, wherein the structure element was 17 and the window width was 5. It is shown that morphology has a particularly obvious effect on the peak filtering.

Figure two, the morphological filtering algorithm added to the QT, the eye electric real-time filtering processing, the effect

This part of the "citation" is mainly from reference 2

Summary

Previously thought that morphological filtering only in the image processing has been used, and now read the reference 2, only to find that they are a bit complacent, the knowledge of the ins and outs of the lack of clarity, word, swallowed, not clear thinking direction, and only grasp the details of the specific skills, do not pay attention to the top layer design, so will have this feeling.

In the process of data processing, the filtering methods commonly used are median filtering, mean filtering, FIR,IIR, Kalman filtering, adaptive filtering and so on. And a lot of knowledge are consistent, must not die learn rote.

Reference documents:
1. http://blog.csdn.net/thefutureisour/article/details/7574819
2. http://m.blog.csdn.net/blog/gylltq/33799347
3. OpenCV 2 Computer Vision Programming Manual

2015-11-28 Study Note Zhang Bongyi

Morphological filtering of "computer vision"

Related Article

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.