在给定图像上移动滤镜/遮罩(无功能) [英] Moving Filter/Mask Across Given Image (No Function)

查看:87
本文介绍了在给定图像上移动滤镜/遮罩(无功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建一个填充图像和滤镜/蒙版的程序.我遇到麻烦的地方实际上是试图在不使用函数的情况下将滤镜移到图像的每一位.

这是我到目前为止所拥有的.

  L = 256;灰度等级节省滤镜尺寸和图像尺寸的尺寸[FilterX,FilterY] = size(Filter);[ImageX,ImageY] = size(Image);%填充图片pad1 = FilterY;PAD =(pad1-1);带有零的新填充图像MaskX = ImageX + PAD;MaskY = ImageY + PAD;遮罩=零(MaskX,MaskY);图像2 = Mask(1+(Pad/2):MaskX-(Pad/2),1+(Pad/2):MaskY-(Pad/2)); 

解决方案

通过卷积过滤图像而不使用 conv2()函数(移动过滤器)

填充图像:

填充图像可以通过沿图像两侧连接零来实现.每种尺寸所需的填充量取决于过滤器/内核的尺寸.下图总结了所需的填充量.请注意,最好的方法是确保过滤器的尺寸(高度和宽度)为奇数.如果滤波器没有奇异的尺寸,最好在滤波器上加上零,我将把它留给读者做为下一个任务.有关填充的更多详细信息,请参见以下问题的答案:

  Image = imread("Pikachu.png");Test_Filter = [-1 0 1;-2 0 2;-1 0 1];%抓取滤镜和图像的尺寸%[Filter_Height,Filter_Width] = size(Test_Filter);[Image_Height,Image_Width] = size(Image);%评估所需的填充厚度%Side_Padding_Size = floor(Filter_Width/2);Top_And_Bottom_Padding_Size = floor(Filter_Height/2);%创建零块以与图像相辅相成%Side_Padding =零(Image_Height,Side_Padding_Size);Top_And_Bottom_Padding =零(Top_And_Bottom_Padding_Size,Image_Width + 2 * Side_Padding_Size);通过级联%填充图像的侧面Padded_Image = [Side_Padding图片Side_Padding];Padded_Image = [Top_And_Bottom_Padding;Padded_Image; Top_And_Bottom_Padding];子图(1,2,1);imshow(图片);子图(1,2,2);imshow(Padded_Image); 

将过滤器应用于图片:

要将滤镜应用于图像,必须设置遍历的界限/范围.由变量 Row Column 递增的外部for循环指示左上角像素,以在给定时间开始扫描与滤镜重叠的区域.下图显示了由滤镜大小设置的界限.下图中的红色方块表示for循环(过滤器的左上像素)可以通过的最大范围.您可能还希望通过旋转滤镜内核并应用相同的方法来添加垂直和水平滤镜.在进行水平和垂直滤波之后,您可能希望获得结果的大小.以下示例仅在一个方向上进行过滤.描述卷积和伪实现的完整PDF:

 %计算遍历图像的边界%[Papped_Image_Height,Padded_Image_Width] = size(Papped_Image);Row_Limit = Papped_Image_Height-Filter_Height +1;Column_Limit =填充图像宽度-Filter_Width + 1;Filtered_Image =零(Image_Height,Image_Width);对于行= 1:Row_Limit对于Column = 1:Column_Limit%抓取重叠滤镜的像素%重叠= Papped_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;结尾结尾子图(1,2,1);imshow(图片);标题(原始图像");子图(1,2,2);imshow(uint8(Filtered_Image));title(使用Sobel边缘过滤器过滤"); 

使用MATLAB R2019b进行运行

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)); 

解决方案

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

这篇关于在给定图像上移动滤镜/遮罩(无功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆