简体   繁体   English

在Matlab中对图像进行模糊处理-图像尺寸错误

[英]De-blur an image in matlab - Error in image dimensions

I am trying to do deblurr an image in matlab. 我正在尝试在Matlab中对图像进行去模糊处理。 this is my code 这是我的代码

im = im2double(imread('C:\Users\adhil\Desktop\matlab pics\test.JPG'));
figure, imshow (G1); 
% FFT for B1
G_1 = fftshift(G1); 
G_1 = fft2(G_1); 
G_1 = ifftshift(G_1);

h_1 = fspecial( 'gaussian', [130 221] , 1.0 );
% Fourier Transform of 2D Gaussian 
H_1 = fftshift(h_1);
H_1 = fft2(H_1); 
H_1 = ifftshift(H_1); 

% Apply the filter for Image G_1

display(size(G_1));
display(size(H_1));
F_1a = G_1 ./ H_1; 
F_1a = ifftshift (F_1a); 
F_1a = ifft2 (F_1a); 
F_1a = fftshift (F_1a); 
figure, imshow (F_1a);

However i'm getting the following error 但是我收到以下错误

Error using ./ 使用./时出错
Matrix dimensions must agree. 矩阵尺寸必须一致。

Error in deblur (line 18) 去模糊错误(第18行)
F_1a = G_1 ./ H_1; F_1a = G_1 ./ H_1;

I've noticed that the array dimensions of my image is 我注意到我图像的阵列尺寸是

display(size(G_1));
ans = 130   221    3
display(size(H_1));
ans = 130   221

However, 然而,

h_1 = fspecial( 'gaussian', [130 221  3] , 1.0 );

doesn't take in a 3 dimensional array, please advise 不包含3维数组,请告知

You are basically trying to deblur a colour image but the process you speak of (deconvolution) assumes a grayscale image. 您基本上是在尝试对彩色图像进行去模糊处理,但是您所说的过程(去卷积)假定为灰度图像。 You are getting a dimensions mismatch because doing ./ assumes that the matrices you are dividing with are the same dimensions because this is an element-wise operator. 由于执行./假定要与之相除的矩阵是相同的维,因此您将得到一个维不匹配,因为这是一个逐元素运算符。

BTW, your beginning code of using fftshift / ifftshift / fft2 for multi-channel images has undefined behaviour for colour images. 顺便说一句,对于多通道图像使用fftshift / ifftshift / fft2起始代码对彩色图像有未定义的行为。 Specifically, for fftshift/ifftshift , the swapping of the quadrants is done in all dimensions and that will provide behaviour that is undesired when it comes to colour images. 具体来说,对于fftshift/ifftshift ,象限的交换在所有维度上进行,这将提供彩色图像所不希望的行为。 Specifically, you probably want to do this for each colour plane separately, and so you'd want to apply a fftshift/ifftshift to each colour plane by itself. 具体来说,您可能想分别对每个颜色平面执行此操作,因此您想单独对每个颜色平面应用fftshift/ifftshift

Therefore, one way to simultaneously solve your problem and get rid of this undefined behaviour would be to deblur each channel individually then merge the results. 因此,同时解决您的问题并摆脱这种不确定行为的一种方法是分别对每个通道进行模糊处理,然后合并结果。 Therefore, wrap all of this in one for loop and define an empty output image to place the results of each channel in their respective positions. 因此,将所有这些包装在一个for循环中,并定义一个空的输出图像,以将每个通道的结果放置在它们各自的位置。 Also, your code currently wouldn't run. 另外,您的代码目前无法运行。 You define the input image to be im , yet you are using a variable G1 ... which isn't defined. 您将输入图像定义为im ,但使用的变量G1 ...未定义。 I'm going to assume that this im is actually G1 . 我将假定此im实际上是G1 The changes I've made to your code have the %// Change comments dispersed throughout: 我对您的代码所做的更改将%// Change注释分散在以下内容中:

G1 = im2double(imread('C:\Users\adhil\Desktop\matlab pics\test.JPG')); %// Change
figure, imshow (G1);

h_1 = fspecial( 'gaussian', [130 221] , 1.0 ); %// Change - leave outside loop - never changes
% Fourier Transform of 2D Gaussian 
H_1 = fftshift(h_1);
H_1 = fft2(H_1); 
H_1 = ifftshift(H_1); 

%// Change
F_1a = zeros(size(G1));

for idx = 1 : size(G1, 3) %// Change
    % FFT for B1        
    G_1 = fftshift(G1(:,:,idx)); %// Change
    G_1 = fft2(G_1); 
    G_1 = ifftshift(G_1);

    % Apply the filter for Image G_1
    %// Change
    tmp = G_1 ./ H_1; 
    tmp = ifftshift(tmp); 
    tmp = ifft2(tmp); 
    tmp = fftshift(tmp); 
    F_1a(:,:,idx) = tmp;
end

figure, imshow (F_1a);

Take note that I'm applying a for loop and applying the deblur to each channel individually. 请注意,我正在应用for循环并将去模糊分别应用于每个通道。 I've also taken the Gaussian blur outside of the loop as you only need to take the Fourier Transform once. 我也将高斯模糊应用于循环之外,因为您只需要进行一次傅立叶变换。 Once I'm done, I show the results. 完成后,我将显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM