[]
I have recently began to dive in the world of image processing in C and one of the things I have been trying to do is to create a horizontal blur.
The program is designed so that the user inputs an image and then choose a blur amount(which is the amount of pixels to the right of the current given pixel they want to blur). This works by looking at a current pixel's RGB values, then halving it to make up 50% of its new weight. The rest of the 50% then lies on the blur amount, in which the program takes that many pixels sums them up and averages so it makes up the remaining 50%. There are also edge cases where you have to divide by a smaller number because you can't add a pixel's RGB value when it is off of the image. When comparing to the correct image, the autotest function shows that I am mostly correct but I am about ~1-2% off. I look at the pixels that are wrong and most of them just seem like dots, so I'm guessing I'm not handling the edge cases correctly? I have tried almost everything and nothing seems to fix it, so some input would be great. I have attached my code below. I have also attached an image showing the difference between my code and the "correct" one, with black pixels indicating it's the correct pixel.
void MotionBlur(int BlurAmount, unsigned char R[WIDTH][HEIGHT],unsigned char G[WIDTH][HEIGHT],unsigned char B[WIDTH][HEIGHT]) {
double r, g, b;
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
r = R[x][y]/2.0;
g = G[x][y]/2.0;
b = B[x][y]/2.0;
if(x+BlurAmount<WIDTH) {
for(int i = 1; i <= BlurAmount; i++) {
r = r + ((0.5*R[x+i][y])/(1.0*BlurAmount));
g = g + ((0.5*G[x+i][y])/(BlurAmount*1.0));
b = b + ((0.5*B[x+i][y])/(BlurAmount*1.0));
}
} else {
for(int j = 1; j <= WIDTH-x-1; j++) {
r = r + ((0.5*R[x+j][y])/(WIDTH-x-1.0));
g = g + ((0.5*G[x+j][y])/(WIDTH-x-1.0));
b = b + ((0.5*B[x+j][y])/(WIDTH-x-1.0));
}
}
R[x][y] = r;
G[x][y] = g;
B[x][y] = b;
}
}
}
I tried changing the stop condition on all of my for loops. For example, i
changed from <= BlurAmount
to < BlurAmount
and it made it worse. Same thing with dividing by width-x
instead of width-x-1
. Also, I'm pretty sure the r
, g
, and b
pixels have to be float
/double
because integers only made it less accurate as well.