1
votes

I'm implementing a blur and color quantization function. I have a separate blur function that I call into this function to create the blur effect.

Edit I think I got it working now, but not completely sure yet if it's the right algorithm.

This is the goal: Blur then quantize the image into a fixed number of levels as specified by a parameter. E.g. if the level parameter is 10, then each color channel should be quantized into 10 values. Calculate b = 255/levels. Then take a color channel value x and do xt = x / b, then xf = xt * b. Do this for each pixel and each color channel, so that the image will have only levels**3 possible color values.

// BLUR QUANTIZE FILTER
int blurQuantize( cv::Mat &src, cv::Mat &dst, int levels ) {
    dst.create(src.size(), src.type());
    
    blur5x5(src, dst);

    int i, j, c;
    
    int b = 255 / levels;

        for(i=0; i<dst.rows; i++) {
          // loop over all rows except first and last
          for(j=0; j<dst.cols; j++) {
            // apply the filter and write the result to a destination image
            for(c=0; c<3; c++) {
              dst.at<cv::Vec3b>(i, j)[c] = dst.at<cv::Vec3b>(i, j)[c] / b * b + b / 2; 
            }
          }
        }
    return 0;
}