图像卷积 2d 中的八度参数错误
Octave Arguments Error in Image Convolution 2d
我需要编写一个二维卷积函数,它 return 与 conv2 的结果相同。
我找到了 conv2 的替代方法,但 return 比 conv2 的结果多 2,它会导致错误。
这里是我找到的卷积函数:
function [ conv_res ] = convolve_im( im,filter )
[rows,cols] = size(im);
new_img = zeros(rows+2,cols+2);
new_img = cast(new_img, class(im));
new_img(2:end-1,2:end-1) = im;
conv_res = zeros(size(new_img));
conv_res = cast(conv_res, class(im));
for i=2:1:rows+1
for j=2:1:cols+1
value=0;
for g=-1:1:1
for l=-1:1:1
value=value+new_img(i+g,j+l) * filter(g+2,l+2);
end
end
conv_res(i,j)=value;
end
end
conv_res = conv_res(2:end-1,2:end-1);
end
这就是我将结果与 conv2 进行比较的方式:
img = imread('puppy.jpeg');
conv_ok =1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0];
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'valid');
check = abs(ground_res) - abs(conv_res); % Line 24
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end
这是我遇到的运行时错误:
第 24 行的参数不一致(op1 是 211x234,op2 是 213x236)
我该如何解决这个错误?谢谢
编辑:将'valid'更改为'same'后,不再报错而是显示'Convolution part is wrong for 3x3!!!'
编辑后的测试函数如下:
img = imread('puppy.jpeg');
conv_ok =1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0]; %laplace filter 3x3
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'same');
check = abs(ground_res) - abs(conv_res);
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end
您需要将 conv2
操作的标志从 'valid'
更改为 'same'
。当您将地面实况和卷积图像一起减去时,valid
的卷积结果较小,因为您想要输出一个图像,其中过滤结果来自完全包含在图像中的内核。使用 'same'
肯定会像您提供的自定义代码 () 中所做的那样对边框进行零填充,并确保输出大小与输入大小相同。事实上,自定义代码专门做了 'same'
填充。
此外,您正在与 3 x 3 内核进行卷积,因此最终输出将删除 2 行和 2 列,以确保您返回的结果有效或内核完全位于图像内部。
此外,您必须更改图像的数据类型。当前您的图像是无符号 8 位整数。因为您使用的是边缘检测,所以任何负的输出值都将被截断为 0,因此您肯定不会得到正确的结果。因此,在过滤图像之前,请先将图像转换为 double
。你可以通过转换为 double
来做到这一点。我选择使用这个而不是 im2double
因为你想检查图像的相等性并且当你的图像像素不是小数时这样做更好。
因此您需要进行以下更改:
img = imread('puppy.jpeg');
img = double(img); % Change
conv_ok = 1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0]; %laplace filter 3x3
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'same'); % Change
check = abs(ground_res) - abs(conv_res);
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end
我需要编写一个二维卷积函数,它 return 与 conv2 的结果相同。
我找到了 conv2 的替代方法,但 return 比 conv2 的结果多 2,它会导致错误。
这里是我找到的卷积函数
function [ conv_res ] = convolve_im( im,filter )
[rows,cols] = size(im);
new_img = zeros(rows+2,cols+2);
new_img = cast(new_img, class(im));
new_img(2:end-1,2:end-1) = im;
conv_res = zeros(size(new_img));
conv_res = cast(conv_res, class(im));
for i=2:1:rows+1
for j=2:1:cols+1
value=0;
for g=-1:1:1
for l=-1:1:1
value=value+new_img(i+g,j+l) * filter(g+2,l+2);
end
end
conv_res(i,j)=value;
end
end
conv_res = conv_res(2:end-1,2:end-1);
end
这就是我将结果与 conv2 进行比较的方式:
img = imread('puppy.jpeg');
conv_ok =1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0];
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'valid');
check = abs(ground_res) - abs(conv_res); % Line 24
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end
这是我遇到的运行时错误:
第 24 行的参数不一致(op1 是 211x234,op2 是 213x236)
我该如何解决这个错误?谢谢
编辑:将'valid'更改为'same'后,不再报错而是显示'Convolution part is wrong for 3x3!!!'
编辑后的测试函数如下:
img = imread('puppy.jpeg');
conv_ok =1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0]; %laplace filter 3x3
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'same');
check = abs(ground_res) - abs(conv_res);
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end
您需要将 conv2
操作的标志从 'valid'
更改为 'same'
。当您将地面实况和卷积图像一起减去时,valid
的卷积结果较小,因为您想要输出一个图像,其中过滤结果来自完全包含在图像中的内核。使用 'same'
肯定会像您提供的自定义代码 ('same'
填充。
此外,您正在与 3 x 3 内核进行卷积,因此最终输出将删除 2 行和 2 列,以确保您返回的结果有效或内核完全位于图像内部。
此外,您必须更改图像的数据类型。当前您的图像是无符号 8 位整数。因为您使用的是边缘检测,所以任何负的输出值都将被截断为 0,因此您肯定不会得到正确的结果。因此,在过滤图像之前,请先将图像转换为 double
。你可以通过转换为 double
来做到这一点。我选择使用这个而不是 im2double
因为你想检查图像的相等性并且当你的图像像素不是小数时这样做更好。
因此您需要进行以下更改:
img = imread('puppy.jpeg');
img = double(img); % Change
conv_ok = 1;
test_filter=[0 -1 0; -1 4 -1; 0 -1 0]; %laplace filter 3x3
conv_res = convolve_im(img, test_filter);
ground_res = conv2(img,test_filter, 'same'); % Change
check = abs(ground_res) - abs(conv_res);
if sum(abs(check(:,:))) ==0
disp('Convolution for 3x3 works fine.');
else
conv_ok = 0;
disp('Convolution part is wrong for 3x3!!!');
end