MATLAB exercise program (Border Processing for image filtering)

Source: Internet
Author: User

When writing a filter program, we generally use a matrix template to perform convolution with the original image. At this time, when processing the image boundary, we generally choose to ignore the edge, but if the template is relatively large, so the processing effect is not good, the image will be the original image around, the middle is the result of filtering, although it can be solved with imfilter of Matlab, however, it is better to use the filter principle.

There are 16 relationships between the template and the image. I roughly drew the first three small rectangles: the template, the large rectangle is the image, and the last one is the template, small is the image. This is all the relationships between images and template convolution.

It seems that we need to write 16 if judgments. In fact, we only need to judge the relationship between the four boundary of the template and the four boundary of the image during convolution. There are two pairs of relative coordinates. One is the convolution range of the image and the other is the convolution range of the template. Let's talk about how to represent the convolution range of the image. If the current processing point is (I, j), the template size is both 2 * r + 1 (I used symmetric odd-number templates here, it will be difficult to process even template boundary pixels. Here I simply convert the odd number to an even number, which is similar in principle ). The eight boundary can be expressed as follows: 1 indicates the upper edge of the image, M indicates the lower edge of the image, 1 indicates the left edge of the image, N indicates the right edge of the image, I + R indicates the bottom edge of the template, J-r indicates the left edge of the template, and J + R indicates the right edge of the template. Through the combination of these four pairs, there will be 16 relationships. For details, see the following code. It is clearer to see the comments and codes.

Main. m

clear all;close all;clc;r=20;w=fspecial('average',[2*r+1 2*r+1]);img=imread('lena.jpg');img=mat2gray(img);[m n]=size(img);imshow(img);imgn=filterim(img,w);figure;imshow(mat2gray(imgn));imgn=img;for i=r+1:m-r    for j=r+1:n-r                        imgn(i,j)=sum(sum(img(i-r:i+r,j-r:j+r).*w));        endendfigure;imshow(mat2gray(imgn));figure;img=imfilter(img,w);imshow(mat2gray(img))

Filterim. m (main functions ):

Function imgn = filterim (IMG, W) [R] = size (w); [m n] = size (IMG); If Mod (R, 2) = 0 r = R + 1; W = imresize (W, [R]); End imgn = zeros (m, n); r = floor (R/2 ); for I = 1: m for j = 1: N % the image needs to obtain the convolution range of the four boundaries. The template only needs to obtain the top and left sides, because the two convolution ranges of the image and the template are the same. If I-r <1% judge the relationship between the upper edge of the template and the upper edge of the image img_up = 1; % if the height of the current pixel is less than half of the template, then, the upper edge of the image is selected as the upper edge of the convolution image mark_up = r-I + 1; % the upper edge of the template uses the upper edge that interacts with the image else img_up = I-r; % use the height of the current pixel minus half of the template as the upper edge of the convolution image mark_up = 1; % use the top edge of the template as the top edge of the convolution template end if I + r> M % to determine the relationship between the bottom edge of the template and the bottom edge of the image img_down = m; % if the height of the current pixel plus half of the template exceeds the height of the entire image, the lower edge of the convolution image uses the lower edge of the entire image else img_down = I + R; % otherwise, the lower edge of the convolution image uses the height of the current pixel plus half of the template end if J-r <1% to determine the relationship between the left edge of the template and the left edge of the image img_left = 1; % if the width of the current pixel is less than half the width of the template, select the left edge of the image as the left edge of the convolution image mark_left = r-J + 1; % the left edge of the template uses the left edge that overlaps the image else img_left = J-R; % use the width of the current pixel minus half of the template as the left edge mark_left = 1 of the convolution image; % use the leftmost edge of the template as the left edge end of the convolution template if J + r> N % to determine the relationship between the right edge of the template and the right edge of the image img_right = N; % if the width of the current pixel plus the width of the template generally exceeds the width of the entire image, then the right edge of the convolution image uses the right edge of the entire image else img_right = J + R; % otherwise, the right edge of the convolution image uses the width of the current pixel plus half of the template end imgn (I, j) = sum (IMG (img_up: img_down, img_left: img_right ). * w (mark_up: mark_up + (img_down-img_up), mark_left: mark_left + (img_right-img_left); %/(img_down-img_up + 1) * (img_right-img_left + 1 )); % convolution image upper edge: lower edge, left edge: right edge % convolution template upper edge: upper edge + (vertical convolution range), convolution template left edge: left Edge + (horizontal convolution range) end endend

To be honest, I am very entangled in writing this annotation, and I have tried my best to write it clearly, although I still do not feel clearly explained. I really need to exercise my writing and expression skills.

Below is:

Source image

Effect of Border Processing for self-written Filtering

Generally, the boundary processing is not performed.

Result of Matlab function processing

My results are similar to those of Matlab, but the speed seems to be much slower. Functions of MATLAB may be processed using assembler. In short, the algorithm is like this.

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.