c-图像卷积,错误生成滤波图像



我改编了Rosetta code中的代码,但我得到的只是白色或黄色的图像,而不是过滤后的图像。

我使用的图像是lena.ppm.

main:有一个过滤器的例子和一些参数,在网站上

double sobel_emboss_kernel[3*3] = {
-1., -2., -1.,
0.,  0.,  0.,
1.,  2.,  1.,
};
const double filter_params[2*4] = {
1.0, 0.0,
1.0, 0.0,
1.0, 0.5,
9.0, 0.0
};

ImageRGB *imagergb;
ImageRGB *rgb_filtered;
imagergb = readRGB("lena.ppm");
rgb_filtered=filter_rgb(imagergb, sobel_emboss_kernel, 1, filter_params[2], filter_params[3]);
writeRGB("resultado_rgb.ppm",imagergb);

滤波器函数和像素函数(我的结构正确实现(:

ImageRGB *filter_rgb(ImageRGB *img, double *K, int Ks, double divisor, double offset){
ImageRGB *img_filtered;
unsigned int ix, iy, l;
int kx, ky;
double cp[3];
int pixel_red,pixel_green,pixel_blue;
img_filtered = (ImageRGB*)malloc(sizeof(ImageRGB));
img_filtered->width = img->width;
img_filtered->height = img->height;
img_filtered->pixels = (colorPixel*)malloc(img->width * img->height * sizeof(colorPixel));
for(ix=0; ix < img->width; ix++) {
for(iy=0; iy < img->height; iy++) {
cp[0] = cp[1] = cp[2] = 0.0;
for(kx=-Ks; kx <= Ks; kx++) {
for(ky=-Ks; ky <= Ks; ky++) {
pixel_red,pixel_green,pixel_blue=getPixelRGB(img,ix+kx,iy+ky);
cp[0] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * (double)pixel_red + offset;
cp[1] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * (double)pixel_green + offset;
cp[2] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * (double)pixel_blue + offset;
}
}
for(l=0; l<3; l++)
cp[l] = (cp[l]>255.0) ? 255.0 : ((cp[l]<0.0) ? 0.0 : cp[l]) ;
put_pixel_unsafe(img_filtered, ix, iy, (int)cp[0], (int)cp[1], (int)cp[2]);
}
}
return img_filtered;
}


void *put_pixel_unsafe(ImageRGB *img,int x,int y,int red,int green,int blue){
unsigned int ofs;
ofs = (y * img->width) + x;
img->pixels[ofs].red = red;
img->pixels[ofs].green = green;
img->pixels[ofs].blue = blue;
}

int getPixelRGB(ImageRGB *imageRGB,int x, int y){
if (x < 0 || x > imageRGB->width || y < 0 || y > imageRGB->height){
return 0;
}
unsigned int position = (y * imageRGB->width) + x;
return imageRGB->pixels[position].red,imageRGB->pixels[position].green,imageRGB->pixels[position].blue;
}

我做错了什么?

问题出在您的行中:

pixel_red,pixel_green,pixel_blue=getPixelRGB(img,ix+kx,iy+ky);

这并不是你想的那样!它只是将getPixelRGB返回的值分配给单个变量pixel_blue(其他两个保持不变(。

根据我所能收集到的,您需要将相应的RGB值分配给该行中的三个变量中的每一个。这样做的一种方法(尽管不是必然是最好或最有效的(是为每个颜色通道声明单独的getPixelX函数:

int getPixelR(ImageRGB *imageRGB,int x, int y){ // Red channel
if (x < 0 || x > imageRGB->width || y < 0 || y > imageRGB->height) return 0;
unsigned int position = (y * imageRGB->width) + x;
return imageRGB->pixels[position].red;
}
int getPixelG(ImageRGB *imageRGB,int x, int y){ // Red channel
if (x < 0 || x > imageRGB->width || y < 0 || y > imageRGB->height) return 0;
unsigned int position = (y * imageRGB->width) + x;
return imageRGB->pixels[position].green;
}
int getPixelB(ImageRGB *imageRGB,int x, int y){ // Red channel
if (x < 0 || x > imageRGB->width || y < 0 || y > imageRGB->height) return 0;
unsigned int position = (y * imageRGB->width) + x;
return imageRGB->pixels[position].blue;
}

然后,您将分别为每个值调用这些函数中的每一个:

pixel_red = getPixelR(img,ix+kx,iy+ky);
pixel_green = getPixelG(img,ix+kx,iy+ky);
pixel_blue = getPixelB(img,ix+kx,iy+ky);

最新更新