Свертка изображения, ошибка при получении пикселя с изображения - PullRequest
0 голосов
/ 25 октября 2019

Я адаптировал код из RosettaCode, и я получаю сообщение об ошибке: «Неверные координаты»

Кажется, ошибка в значении Ks.

Я прохожу иди и получаю пиксельотрицательное значение.

Аргументы функции были на сайте.

Фильтровать и получать функции пикселей:

colorPixel getPixelRGB(ImageRGB *imageRGB,int x, int y){
  if ( (x<0) || (x >= imageRGB->width) || (y<0) || (y >= imageRGB->height) ){
    printf("Invalid coordinates!\n");
    exit(0);
  }
  int position = ((imageRGB->width-1) * y + x)-1;
  return imageRGB->pixels[position];
}



  ImageRGB *img_filtered;
  unsigned int ix, iy, l;
  ix=0;
  iy=0;
  int kx, ky;
  double cp[3];
  colorPixel pixel;

  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));
  int i=0;
  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++) {

          printf("%d",ix+kx);
          pixel=getPixelRGB(img,ix+kx,iy+ky);
          cp[0] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * pixel.red + offset;
          cp[1] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * pixel.green + offset;
          cp[2] += (K[(kx+Ks) + (ky+Ks)*(2*Ks+1)]/divisor) * 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]) ;
      img_filtered->pixels[i].red=cp[0];
      img_filtered->pixels[i].green=cp[1];
      img_filtered->pixels[i].blue=cp[2];

    }
  }
  return img_filtered;
}

Структура изображения rgb:


typedef struct {
     unsigned char red,green,blue;
} colorPixel;

typedef struct {

    int  height;
    int width;
    colorPixel* pixels;

} ImageRGB;

основной и функциональный вызов с параметрами:

int main(void){


  double emboss_kernel[3*3] = {
    -2., -1.,  0.,
    -1.,  1.,  1.,
    0.,  1.,  2.,
  };

  double sharpen_kernel[3*3] = {
    -1.0, -1.0, -1.0,
    -1.0,  9.0, -1.0,
    -1.0, -1.0, -1.0
  };
  double sobel_emboss_kernel[3*3] = {
    -1., -2., -1.,
    0.,  0.,  0.,
    1.,  2.,  1.,
  };
  double box_blur_kernel[3*3] = {
    1.0, 1.0, 1.0,
    1.0, 1.0, 1.0,
    1.0, 1.0, 1.0,
  };

  const double filter_params[2*4] = {
  1.0, 0.0,
  1.0, 0.0,
  1.0, 0.5,
  9.0, 0.0
  };

  double *filters[4] = {
    emboss_kernel, sharpen_kernel, sobel_emboss_kernel, box_blur_kernel
  };

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

return 0;
}

Что мне нужно изменить для работы кода? Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...