Moving Filter/Mask Across Given Image (No Function)

这一生的挚爱 提交于 2021-02-10 17:55:38

问题


I am struggling attempting to create a program that pads an image and filter/mask. Where I am having trouble is actually attempting to move this filter over each bit of the image without using a function to do so.

Here is what I have so far.

L=256; %Gray Levels

%Saving Dimensions of both Filter and Image Sizes
[FilterX , FilterY] = size(Filter);
[ImageX , ImageY]= size(Image);

% Padding Image
pad1 = FilterY; 
PAD = (pad1-1);

%New Padded Image With Zeros
MaskX = ImageX + PAD;
MaskY = ImageY + PAD;
Mask = zeros(MaskX,MaskY);

Image2 = Mask(1+(Pad/2) : MaskX-(Pad/2) , 1+(Pad/2) : MaskY-(Pad/2)); 

回答1:


Filtering Image by Convolution Without Using conv2() Function (Moving Filter)

Padding the Image:

Padding the image can be achieved by concatenating zeros along the sides of the image. The amount of padding required on each size is dependent on the size of the filter/kernel. The diagram below summarizes the amount of padding required. Note that it may be best practice to ensure that the filter has dimensions (height and width) that are odd in size. If the filter does not have odd dimensions it might be a good idea to pad the filter with zeros which I will leave as a further task for the reader to implement. For more details about padding see the answer to this question: Rotating image with trigonometric functions and image padding. A great animated GIF (not created by me) that visualizes the convolution process well: Animated image convolution process.

Image = imread("Pikachu.png");
Test_Filter = [-1 0 1; -2 0 2; -1 0 1];

%Grabbing the dimensions of the filter and image%
[Filter_Height,Filter_Width] = size(Test_Filter);
[Image_Height,Image_Width] = size(Image);

%Evaluating the padding thickness required%
Side_Padding_Size = floor(Filter_Width/2);
Top_And_Bottom_Padding_Size = floor(Filter_Height/2);

%Creating the blocks of zeros to concantenate to the image%
Side_Padding = zeros(Image_Height,Side_Padding_Size);
Top_And_Bottom_Padding = zeros(Top_And_Bottom_Padding_Size,Image_Width+2*Side_Padding_Size);

%Padding the sides of the image by concatenating%
Padded_Image = [Side_Padding Image Side_Padding];
Padded_Image = [Top_And_Bottom_Padding; Padded_Image ;Top_And_Bottom_Padding];

subplot(1,2,1); imshow(Image);
subplot(1,2,2); imshow(Padded_Image);

Applying Filter to Image:

To apply the filter to the image the limits/bounds of traversing must be set. The outer for-loops incremented by variables Row and Column indicate the top-left corner pixel to start scanning the region overlapping the filter at a given time. The diagram below indicates the bounds which are set by the filter size. The red squares in the diagram below indicate the maximum bounds that the for-loops (top-left pixel of the filter) can traverse through. You may also wish to add vertical and horizontal filtering by rotating the filter kernel and applying the same method. You may wish to take the magnitude of the results after doing the horizontal and vertical filtering. The example below only does filtering in one direction. Full PDF describing convolution and pseudo implementation: GitHub PDF: Image Convolution

%Calculating bounds of traversing through the image%
[Padded_Image_Height,Padded_Image_Width] = size(Padded_Image);
Row_Limit = Padded_Image_Height - Filter_Height + 1;
Column_Limit = Padded_Image_Width - Filter_Width + 1; 


Filtered_Image = zeros(Image_Height,Image_Width);

for Row = 1: Row_Limit
   for Column = 1: Column_Limit 
    
  
%Grabbing pixels overlapping filter%       
Overlap = Padded_Image(Row:Row+(Filter_Height-1),Column:Column+(Filter_Width-1));
Filtered_Portion = double(Test_Filter).*double(Overlap);   
Filtered_Portion = sum(Filtered_Portion,'all');
Filtered_Image(Row,Column) = Filtered_Portion; 
    
    
   end
end


subplot(1,2,1); imshow(Image);
title("Original Image");

subplot(1,2,2); imshow(uint8(Filtered_Image));
title("Filtered with Sobel Edge Filter");

Ran using MATLAB R2019b



来源:https://stackoverflow.com/questions/64743475/moving-filter-mask-across-given-image-no-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!