C++ Programming: Image Blur Code
Another block of code that simulates blurring an image. This reads the position of x and y of a pixel and the surrounding values and applies a simple algorithm to find the average of all values and reapply the new value resulting in a blurry effect.
void blur(int img[][MAX_COL], int h, int w) {
//for loop to cycle through the image positions
for ( int x = 0; x < h; x++ ) {
for ( int y = 0; y < w; y++ ) {
//formula to calculate the values of the surrounding pixels
int sum = 2 * ( img[x][y] + //center center
img[x-1][y-1] + //top left
img[x-1][y] + //top center
img[x-1][y+1] + //top right
img[x][y-1] + // center left
img[x][y+1] + //center right
img[x+1][y-1] + //bottom left
img[x+1][y] + //bottom center
img[x+1][y+1] //bottom right
);
// position modifier with the new value
img[x][y] = sum/14;
}
}
}
If there is a better way to accomplish this, please leave a comment. I am always open to learning new ways to code.