Jan
Jan

Reputation: 1084

simple blur of RGB raw image

I'm trying to make a simple blur effect in c. I have an image loaded in an 512*512 array of RGB pixels and I'm using 3x3 kernel to blur that image

here is the kernel

float matrix[9] = {1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f};

and here is the code that is doing the bluring

void useBlur(){

  for(int i = 0; i < ARRAY_SIZE; i++){
      float r = 0;
      float g = 0;
      float b = 0;
      int m, n;

      for(int y = -1,  m = 0; y <= 1; y++, m++){
          for(int z = -1,  n = 0; z <= 1; z++, n++){
              r += (float)orig_image[i + 512 * y + z].r * (float)matrix[m*3+n];
              g += (float)orig_image[i + 512 * y + z].g * (float)matrix[m*3+n];
              b += (float)orig_image[i + 512 * y + z].b * (float)matrix[m*3+n];
          }

      }

      image[i].r = r;
      image[i].g = g;
      image[i].b = b;

  }
}

I'm not sure what is wrong with that code, but it is producing result:

enter image description here

Any ideas why the colors are wrong? And how to fix it?

EDIT: fixed matrix[7] from 9.0/9.0 to 1.0/9.0 and uploaded new image

Upvotes: 3

Views: 4390

Answers (1)

Hammer
Hammer

Reputation: 10329

I tried your code with some changes, the images I have are BGRA and I am using opencv for my image container but the structure of the code is the same and it works perfectly.

    for (int i = 2*image.cols; i < image.rows*image.cols-2*image.cols; i++)
    {
        float r = 0;
        float g = 0;
        float b = 0;

        for (int y = -1; y <=1; y++)
        {
            for (int z = -1; z <=1; z++)
            {
                b += (float)image.at<cv::Vec4b>(i + image.cols*y+z)(0)*(1.0/9);
                g += (float)image.at<cv::Vec4b>(i + image.cols*y+z)(1)*(1.0/9);
                r += (float)image.at<cv::Vec4b>(i + image.cols*y+z)(2)*(1.0/9);
            }
        }
        image.at<cv::Vec4b>(i)(0) = b;
        image.at<cv::Vec4b>(i)(1) = g;
        image.at<cv::Vec4b>(i)(2) = r;
    }

I would assume that means the problem is either not in the code you posted, or somehow the opencv cv::Mat class is handling something your image container is not. The obvious candidate for that would be that you seem to be implicitly converting from float to uchar at the end of your code. Could you test running this loop over your image but without modifying it? It could look like this, or any number of other ways. This is ugly, but a quick conversion.

void useBlur(){

for(int i = 0; i < ARRAY_SIZE; i++){
  float r = 0;
  float g = 0;
  float b = 0;
  int m, n;

  for(int y = -1,  m = 0; y <= 1; y++, m++){
      for(int z = -1,  n = 0; z <= 1; z++, n++){
          if(y == 0 && z == 0)
          {
            r += (float)orig_image[i + 512 * y + z].r * (float)matrix[m*3+n]*9;
            g += (float)orig_image[i + 512 * y + z].g * (float)matrix[m*3+n]*9;
            b += (float)orig_image[i + 512 * y + z].b * (float)matrix[m*3+n]*9;
          }
      }

  }

  image[i].r = r;
  image[i].g = g;
  image[i].b = b;

 }
 }

Theoretically nothing should change about the image when you do that, so if the image is changing it is because of some conversion error. I admit the theory is unlikely but the structure of your code seems sound so I don't know what else to suggest.

Upvotes: 3

Related Questions