二维卷积在C语言中的实现
这是我用C编写的2D卷积函数:二维卷积在C语言中的实现,c,matlab,image-processing,implementation,convolution,C,Matlab,Image Processing,Implementation,Convolution,这是我用C编写的2D卷积函数: typedef struct PGMImage{ int w; int h; int* data; }GrayImage; GrayImage Convolution2D(GrayImage image,GrayImage kernel){ int aH,aW,bW,bH,r,c,x,y,xx,yy,X,Y; int temp = 0; GrayImage conv; CreateGrayImage(&am
typedef struct PGMImage{
int w;
int h;
int* data;
}GrayImage;
GrayImage Convolution2D(GrayImage image,GrayImage kernel){
int aH,aW,bW,bH,r,c,x,y,xx,yy,X,Y;
int temp = 0;
GrayImage conv;
CreateGrayImage(&conv,image.w,image.h);
aH = image.h;
aW = image.w;
bH = kernel.h;
bW = kernel.w;
if(aW < bW || aH < bH){
fprintf(stderr,"Image cannot have smaller dimensions than the blur kernel");
}
for(r = aH-1;r >= 0;r--){
for(c = aW-1;c >= 0;c--){
temp = 0;
for(y = bH-1;y >= 0;y--){
yy = bH - y -1;
for(x = bW-1;x >= 0;x--){
xx = bW - x - 1;
X = c + (x - (bW/2));
Y = r + (y - (bH/2));
if(X >= 0 && X < aW && Y >= 0 && Y < aH){
temp += ((kernel.data[(yy*bW)+xx])*(image.data[(Y*aW)+X]));
}
}
}
conv.data[(r*aW)+c] = temp;
}
}
return conv;
}
但是Matlab的conv2给了我
46596 46618 46627
46073 46400 46149
45951 46226 46153
对于相同的像素(行:239-241,列:316:318)
这是我用来比较数值的Matlab代码:
pgm_img = imread('path\to\lena512.pgm');
kernel = imread('path\to\test_kernel.pgm');
sz_img = size(pgm_img);
sz_ker = size(kernel);
conv = conv2(double(pgm_img),double(kernel),'same');
pgm_img = padarray(pgm_img,floor(0.5*sz_ker),'both');
convolve = zeros(sz_img);
for i=floor(0.5*sz_ker(1))+1:floor(0.5*sz_ker(1))+sz_img(1)
for j=floor(0.5*sz_ker(2))+1:floor(0.5*sz_ker(2))+sz_img(2)
startX = j - floor(sz_ker(2)/2);
startY = i - floor(sz_ker(1)/2);
endX = j + floor(sz_ker(2)/2);
endY = i + floor(sz_ker(1)/2);
block = pgm_img(startY:endY,startX:endX);
prod = double(block).*double(kernel);
convolve(i-floor(0.5*sz_ker(1)),j-floor(0.5*sz_ker(2))) = sum(sum(prod));
end
end
disp(conv(239:241,316:318));
disp(convolve(239:241,316:318));
一个明显的区别是c代码使用int,而matlab代码使用double。将c代码更改为使用double,然后查看结果是否仍然不同 一个明显的区别是,c代码使用int,而matlab代码使用double。将c代码更改为使用double,然后查看结果是否仍然不同 我为简单的2D浮点数组图像创建了图像卷积库 该函数支持任意内核,并通过MATLAB实现进行了验证 因此,您所需要的只是使用生成的内核调用它 您可以在MATLAB中使用它生成的DLL,并看到它产生与MATLAB的图像卷积函数相同的结果
.我为简单的2D浮点数组图像创建了图像卷积库 该函数支持任意内核,并通过MATLAB实现进行了验证 因此,您所需要的只是使用生成的内核调用它 您可以在MATLAB中使用它生成的DLL,并看到它产生与MATLAB的图像卷积函数相同的结果
.请提供一份报告,以便我们复制您的问题。你使用什么图像和内核,从哪里得到错误的值?我们都有专有图像的问题。你能在一个普通的股票图像上运行你的程序并给我们指出它吗?说真的,试试一些小的例子。有这么大的数据,我永远不会发现错误。请给我们一个答案,让我们重现你的问题。你使用什么图像和内核,从哪里得到错误的值?我们都有专有图像的问题。你能在一个普通的股票图像上运行你的程序并给我们指出它吗?说真的,试试一些小的例子。有了这样大的数据,我永远也不会发现错误。
pgm_img = imread('path\to\lena512.pgm');
kernel = imread('path\to\test_kernel.pgm');
sz_img = size(pgm_img);
sz_ker = size(kernel);
conv = conv2(double(pgm_img),double(kernel),'same');
pgm_img = padarray(pgm_img,floor(0.5*sz_ker),'both');
convolve = zeros(sz_img);
for i=floor(0.5*sz_ker(1))+1:floor(0.5*sz_ker(1))+sz_img(1)
for j=floor(0.5*sz_ker(2))+1:floor(0.5*sz_ker(2))+sz_img(2)
startX = j - floor(sz_ker(2)/2);
startY = i - floor(sz_ker(1)/2);
endX = j + floor(sz_ker(2)/2);
endY = i + floor(sz_ker(1)/2);
block = pgm_img(startY:endY,startX:endX);
prod = double(block).*double(kernel);
convolve(i-floor(0.5*sz_ker(1)),j-floor(0.5*sz_ker(2))) = sum(sum(prod));
end
end
disp(conv(239:241,316:318));
disp(convolve(239:241,316:318));