Reputation:
I adapted the code from Rosetta Code, but i'm getting just white or yellow images, instead of a filtered image.
The image i'm using is lena.ppm.
main: with one filter example and some parameters, that were on the site
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);
filter function and pixel functions (my struct is corretly implemented):
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;
}
What im doing wrong?
Upvotes: 0
Views: 33
Reputation: 51894
The problem is in your line:
pixel_red,pixel_green,pixel_blue=getPixelRGB(img,ix+kx,iy+ky);
This does not do what you may think! It simply assigns the value returned by getPixelRGB
to a single variable, pixel_blue
(the other two are left unchanged).
Form what I can gather, you need to assign the respective R
, G
and B
values to each of the three variables in that line. One way of doing this (though not necessarily the best or most efficient) is to declare separate getPixelX
functions for each colour channel:
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;
}
You would then call each of these functions separately for each value:
pixel_red = getPixelR(img,ix+kx,iy+ky);
pixel_green = getPixelG(img,ix+kx,iy+ky);
pixel_blue = getPixelB(img,ix+kx,iy+ky);
Upvotes: 1