Application and extension of image filter function IMFilter function

Source: Internet
Author: User
Tags image filter

A) recognize the IMFilter function

IMFilter function is called implement linear spatial filter function, the main function can realize multi-dimensional array filtering, in the image domain is to filter the image.
Filtering is a unified concept, in the field of image, image removal noise point, image extraction edge, image smoothing, blurring, enhancement and so on can be regarded as filtering.
usage : B = IMFilter (a,h)
B = IMFilter (A,h,option1,option2,...)
or writing:
B = IMFilter (f, W, Filtering_mode, Boundary_options, size_options);
(in more detail in the MATLAB environment Help query IMFilter function) posted here to help the file as follows:

II) Recognition Template H

It can be said that the essence of the IMFilter function is the structure of the template H, the difference between H determines that you achieve different results, then what is a template? For example, if you want to extract the noise point of the image now, we know that since it is the noise point, then the pixel value of the noise point and its surrounding points should be very far from the pixel value, here Select the noise point of the upper and lower left and right four directions, each direction with the value of the noise point minus the corresponding direction of the value of , if the higher the value, is not to indicate the more noise point of the point, then the above process can be calculated with a template, this template is:

0 -1 0
-1 4 -1
0 -1 0

Using this template to filter the image can be used to get the noise (also can get edge, because the edge also conforms to this feature), as shown in:

The graph is through the template H = [0-1 0;-1 4-1;0-1 0] filter, the corresponding MATLAB code is imfilter (I,H);
In fact, the operation of this filter function is very simple, for an image of the operation of a pixel, first look at the template size, such as 3*3, and then extract the pixel near the 3*3 field of 9 pixels, the 9 pixels corresponding to the number of the template is multiplied, Add all the values of the multiplication (obviously there will be positive or negative, not all positive values), this and as the pixel point filter after the value, then each pixel in the image to do this operation, you will get a copy of the original image size of the same filter image, such as the above picture.
The importance of the image filtering function by the above-known template. Like the above template, this template is also called the Laplace template, similar to the edge detection operator, canny operator, sobel operator, etc., related to:
http://www.kongzhi.net/cases/caseview.php?id=2368
For example, the operator of the edge detection vertical bar can be expressed as H = [-1 2-1;-1 2-1;-1 2-1], the results are as follows:

Three) a question to which it may be thought that, since this operator's processing of pixels is a weighted sum of pixels near the pixel point, what about the edges of an image? Or the 3*3 template, what should I do with the leftmost and topmost pixel? Its top, left no pixel point ah, this involves the expansion of the edge, that is, the IMFilter function inside the boundary_options parameter, matlab expansion way like on the 3 way, copy the boundary, mirror boundary, expand by the period, such as replication boundary, That is, the boundary point is copied to the boundary, add 100*100 image, use 3*3 template, then copy the boundary image is 102*102 image, which leads to imfilter the third parameter size_options, This parameter determines whether the output image is the size of the original image or the expansion of the boundary, in general, the default output of the original image size, that is, the 102*102 image in the output will remove the added boundary, re-become 100*100 image. Of course, the problem of dealing with the boundary, you can also program to expand, generate a larger image to store it, and then copy the boundary points past. IV) application extensions for IMFilter

The

Application of the IMFilter function is much more than filtering, in fact, the construction of H can quickly achieve a lot of complex operations, especially for the entire image (and will involve boundary problems).
A simple example, assuming now an image, we want to extract the eigenvalues of each pixel in the image, which includes four features (grayscale value, pixel lateral gradient, pixel vertical gradient, field variance in pixels), each feature is calculated as follows:

What do I do when I need to ask for these four features of all pixels? A method directly according to the formula to solve, programming to achieve these four features is also very simple, and then for each pixel point to a for loop. This brings two questions: (1) What about your border point? There is always a direction to the top and bottom of the boundary point, there is no pixel point, this time you have to expand the boundary to continue. (2) We know that for the cycle time-consuming, for the MATLAB Matrix Lab, the overall operation of the matrix is the best, fast, the most taboo is to use a lot of matlab in the For loop, use up to slow you (some problems).
Here we use the filter function IMFilter to decompose the solving process of the above four features, which makes the calculation faster and simpler. Feature A1 not to say, is the original image. The most troublesome feature A2,A2 is the domain class variance, first to solve the 3*3 field mean value, that is, all the pixels in this field sum and then average, which requires a mean template:

model = 1/9*[1 1 1;1 1 1;1 1 1];F2_mean = imfilter(img,model,‘replicate‘);

Boundaries are expanded in a way that is duplicated, with the default generation size as large as the original image. Then the mean value and the field of the pixel minus the sum and then the mean, then the field of pixels must be adjusted to the center point position, which requires 8 templates, the corresponding program:

Model_1 = [-1 0 0;0 0 0;0 0 0]; F2_1 = IMFilter (Img,model_1,' Replicate '); model_2 = [0-1 0;0 0 0;0 0 0]; F2_2 = IMFilter (img,model_2,' Replicate '); model_3 = [0 0-1;0 0 0;0 0 0]; F2_3 = IMFilter (Img,model_3,' Replicate '); model_4 = [0 0 0;-1 0 0;0 0 0]; F2_4 = IMFilter (Img,model_4,' Replicate '); model_5 = [0 0 0;0 0-1;0 0 0]; F2_5 = IMFilter (Img,model_5,' Replicate '); model_6 = [0 0 0;0 0 0;-1 0 0]; F2_6 = IMFilter (Img,model_6,' Replicate '); model_7 = [0 0 0;0 0 0;0-1 0]; f2_7 = IMFilter (img,model_7,' Replicate '); model_8 = [0 0 0;0 0 0;0 0-1]; F2_8 = IMFilter (Img,model_8,' Replicate '); F2=0.125* (ABS (f2_mean+f2_1) +abs (f2_mean+f2_2) +abs (f2_mean+f2_3) +abs (f2_mean+f2_4) +...ABS (F2_mean+f2_5) +abs (f2_mean+f2_6) +abs (f2_mean+f2_7) +abs (f2_mean+f2_8));

The resulting F2 is the A2 feature of the entire image, you can see no loop, do not have to design the boundary expansion, speed is particularly fast.
followed by A3 and A4 features, these two features are also relatively simple, as follows:

0.5*(absabs0.5*(absabs(img + F2_7));

At this point, the pixels of these four features are almost, you can see the IMFilter function is very powerful, especially for all pixels of the overall operation of the time (and the image is generally the overall operation), but the usual use of edge extraction, filtering and other original, for other problems, As long as can be decomposed into simple addition and problems, can be easily solved with imfilter.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Application and extension of image filter function IMFilter function

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.