I am trying to write I could that can edit images according to Sobel filter algorithm but it still gives me segmentation error when I try to use edges function but I couldn't know the reason can any one tell me what is the error ?
the target of such project is to turn any picture to be edged by multiplying each pixel colors values with a corresponding matrix but when I do so I got segmentation fault error (core dump) what shall I do ?
#include <stdio.h>
#include <math.h>
#include "helpers.h"
//#include "bmp.h"
//variables for
//int Gx_sum_red, Gx_sum_blue, Gx_sum_green, Gy_sum_red, Gy_sum_blue, Gy_sum_green;
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
//int height_edited = 0;
//int width_edited = 0;
//printf("height is %i, width is %i and RGBTRIPLE IS %i for green, %i for blue and %i for red\n", height, width,
// image[height_edited][width_edited].rgbtBlue, image[height_edited][width_edited].rgbtGreen, image[height_edited][width_edited].rgbtRed);
//RGBTRIPLE temp
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtGreen = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtRed = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
//int height_edited = height / 2;
//int width_edited = width / 2;
//printf("height is %i, width is %i and RGBTRIPLE IS %i for green, %i for blue and %i for red\n", height, width,
// image[height_edited][width_edited].rgbtBlue, image[height_edited][width_edited].rgbtGreen, image[height_edited][width_edited].rgbtRed);
RGBTRIPLE temp;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
temp.rgbtBlue = image[i][j].rgbtBlue;
temp.rgbtGreen = image[i][j].rgbtGreen;
temp.rgbtRed = image[i][j].rgbtRed;
image[i][j].rgbtBlue = image[i][width - j].rgbtBlue;
image[i][j].rgbtGreen = image[i][width - j].rgbtGreen;
image[i][j].rgbtRed = image[i][width - j].rgbtRed;
image[i][width - j].rgbtBlue = temp.rgbtBlue;
image[i][width - j].rgbtGreen = temp.rgbtGreen;
image[i][width - j].rgbtRed = temp.rgbtRed;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0)
{
image[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4;
}
else if (i == 0 && j == width - 1)
{
image[i][j].rgbtBlue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4;
}
else if (i == height - 1 && j == 0)
{
image[i][j].rgbtBlue = (image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed) / 4;
}
else if (i == height - 1 && j == width - 1)
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed) / 4;
}
else if (i == 0 && (j != 0 || j != width - 1))
{
image[i][j].rgbtBlue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
}
else if (i == height - 1 && (j != 0 || j != width - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed) / 6;
}
else if (j == 0 && (i != 0 || i != height - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
}
else if (j == width - 1 && (i != 0 || i != height - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6;
}
else
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9;
}
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int red_avg, blue_avg, green_avg;
int fGx_sum_red, fGx_sum_blue, fGx_sum_green, fGy_sum_red, fGy_sum_blue, fGy_sum_green;
int red_sum, blue_sum, green_sum;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//calculating the Gx sum of red values
if (i == 0 && j == 0)
{
fGx_sum_red = (image[i][j + 1].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i][j + 1].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i][j + 1].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (i == 0 && j == width - 1)
{
fGx_sum_red = (image[i][j - 1].rgbtRed * -2) + (image[i + 1][j - 1].rgbtRed * -1);
fGx_sum_blue = (image[i][j - 1].rgbtBlue * -2) + (image[i + 1][j - 1].rgbtBlue * -1);
fGx_sum_green = (image[i][j - 1].rgbtGreen * -2) + (image[i + 1][j - 1].rgbtGreen * -1);//
fGy_sum_red = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2);//
fGy_sum_blue = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtBlue * 2);//
fGy_sum_green = (image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2);//
}
else if (i == height - 1 && j == 0)
{
fGx_sum_red = (image[i - 1][j + 1].rgbtRed * 1) + (image[i][j + 1].rgbtRed * 2);
fGx_sum_blue = (image[i - 1][j + 1].rgbtBlue * 1) + (image[i][j + 1].rgbtBlue * 2);
fGx_sum_green = (image[i - 1][j + 1].rgbtGreen * 1) + (image[i][j + 1].rgbtGreen * 2);//
fGy_sum_red = (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1);//
fGy_sum_blue = (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1);//
fGy_sum_green = (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1);//
}
else if (i == height - 1 && j == width - 1)
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1)+ (image[i][j - 1].rgbtRed * -2);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i][j - 1].rgbtBlue * -2);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i][j - 1].rgbtGreen * -2);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2);//
}
else if (i == 0 && (j != 0 || j != width - 1))
{
fGx_sum_red = (image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2) +
(image[i + 1][j - 1].rgbtRed * -1) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2) +
(image[i + 1][j - 1].rgbtBlue * -1) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2) +
(image[i + 1][j - 1].rgbtGreen * -1) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (i == height - 1 && (j != 0 || j != width - 1))
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j + 1].rgbtRed * 1) +
(image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j + 1].rgbtBlue * 1) +
(image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j + 1].rgbtGreen * 1) +
(image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1);//
}
else if (j == 0 && (i != 0 || i != height - 1))
{
fGx_sum_red = (image[i - 1][j + 1].rgbtRed * 1) + (image[i][j + 1].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i - 1][j + 1].rgbtBlue * 1) + (image[i][j + 1].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i - 1][j + 1].rgbtGreen * 1) + (image[i][j + 1].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1) +
(image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1) +
(image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1) +
(image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (j == width - 1 && (i != 0 || i != height - 1))
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i][j - 1].rgbtRed * -2) + (image[i + 1][j - 1].rgbtRed * -1);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i][j - 1].rgbtBlue * -2) + (image[i + 1][j - 1].rgbtBlue * -1);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i][j - 1].rgbtGreen * -2) + (image[i + 1][j - 1].rgbtGreen * -1);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) +
(image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) +
(image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) +
(image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2);//
}
else
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j + 1].rgbtRed * 1) +
(image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2) +
(image[i + 1][j - 1].rgbtRed * -1) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j + 1].rgbtBlue * 1) +
(image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2) +
(image[i + 1][j - 1].rgbtBlue * -1) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j + 1].rgbtGreen * 1) +
(image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2) +
(image[i + 1][j - 1].rgbtGreen * -1) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1) +
(image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1) +
(image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1) +
(image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
printf("red_sum is %i and %i, blue_sum is %i and %i and green_sum is %i and %i\n", fGx_sum_red, fGy_sum_red, fGx_sum_blue, fGy_sum_blue,
fGx_sum_green, fGy_sum_green);
//calculating sum of Gx and Gy for each color
red_sum = (fGx_sum_red * fGx_sum_red) + (fGy_sum_red * fGy_sum_red);
blue_sum = (fGx_sum_blue * fGx_sum_blue) + (fGy_sum_blue * fGy_sum_blue);
green_sum = (fGx_sum_green * fGx_sum_green) + (fGy_sum_green * fGy_sum_green);
printf("red_sum is %i, blue_sum is %i and green_sum is %i\n", red_sum, blue_sum, green_sum);
red_avg = sqrt(red_sum);
blue_avg = sqrt(blue_sum);
green_avg = sqrt(green_sum);
printf("red_avg is %i, blue_avg is %i and green_avg is %i\n", red_avg, blue_avg, green_avg);
//updating colors values
image[i][j].rgbtRed = red_avg > 255 ? 255 : red_avg;
image[i][j].rgbtBlue = blue_avg > 255 ? 255 : blue_avg;
image[i][j].rgbtGreen = green_avg > 255 ? 255 : green_avg;
printf("image[i][j].rgbtRed is %i, image[i][j].rgbtBlue is %i and image[i][j].rgbtGreen is %i, i and j are %i and %i \n",
image[i][j].rgbtRed, image[i][j].rgbtBlue, image[i][j].rgbtGreen, i, j);
//continue;
}
}
return;
}
When i == 0 you try to deference image[i - 1][j] in this block of code. This is out of bound access to the image array and trigger a segfault for me:
} else if (i == 0 && (j != 0 || j != width - 1)) {
fGx_sum_red = (image[i - 1][j].rgbtRed * 0) ...
By analogy (in other words I have no idea if that is correct algorithm) replace i - 1 with i in that block.
Related
I'm making a blur filter that takes the RGB values of the surrounding pixels, takes the average of each, and applies it to the pixel. Have been working on it for days and I'm still not sure what the problem is or how to fix it. I did do it the long way and there is probably a shorter faster way but I can't seem to figure it out. Here's the important part of the code:
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int avrgRed;
int avrgBlue;
int avrgGreen;
int maxj = width - 1;
int maxi = height - 1;
if (i == 0) {
if (j == 0) {
avrgRed =
round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) /
4);
avrgBlue =
round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) /
4);
avrgGreen =
round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) /
4);
} else if (j == maxj) {
avrgRed =
round((image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) /
4);
avrgBlue =
round((image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) /
4);
avrgGreen =
round((image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) /
4);
} else {
avrgRed =
round((image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) /
6);
avrgBlue =
round((image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) /
6);
avrgGreen =
round((image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) /
6);
}
} else if (i == maxi) {
if (j == 0)
//{
// avrgRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
// image[i + 1][j].rgbtRed + image[i + 1][j + 1])/4);
//}
{
avrgRed = round((image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j].rgbtRed + image[i][j + 1].rgbtRed) /
4);
avrgBlue =
round((image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) /
4);
avrgGreen =
round((image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) /
4);
} else if (j == maxj) {
avrgRed =
round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) /
4);
avrgBlue =
round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) /
4);
avrgGreen =
round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) /
4);
} else {
avrgRed =
round((image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) /
6);
avrgBlue =
round((image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) /
6);
avrgGreen =
round((image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) /
6);
}
} else {
if (j == 0) {
avrgRed =
round((image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed +
image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) /
6);
avrgBlue =
round((image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue +
image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) /
6);
avrgGreen =
round((image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen +
image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) /
6);
} else if (j == maxj) {
avrgRed =
round((image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) /
6);
avrgBlue =
round((image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) /
6);
avrgGreen =
round((image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) /
6);
} else {
avrgRed = round((image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed +
image[i - 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed +
image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed +
image[i + 1][j + 1].rgbtRed) /
9);
avrgBlue =
round((image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue +
image[i - 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue +
image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue +
image[i + 1][j + 1].rgbtBlue) /
9);
avrgGreen =
round((image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen +
image[i - 1][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen +
image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen +
image[i + 1][j + 1].rgbtGreen) /
9);
}
}
newimageRed[i][j] = avrgRed;
newimageBlue[i][j] = avrgBlue;
newimageGreen[i][j] = avrgGreen;
}
}
Here is a 4x4 image test (the RGB values are written)
Expected result
70 85 95 80 95 105 100 115 125 110 125 135
113 126 136 123 136 145 142 155 163 152 165 173
113 119 136 143 151 164 156 166 171 180 190 194
113 112 132 155 156 171 169 174 177 203 207 209
Actual result
70 85 95 80 95 105 100 115 125 110 125 135
92 104 112 122 135 145 141 154 163 140 147 164
140 147 164 143 150 163 156 165 171 119 121 135
112 111 131 135 89 97 140 139 138 76 138 143
What do you guys think is the problem? or should I rewrite it in another way?
Update:
I did solve the bigger problems, I messed up the + and - placements and mixed them up. Now I just have to fix the 1 problems, which may be because of the round function, have tried a few now but they all end up being off by 1 somewhere. Gonna check if the problem is a float > int conversion problem later.
Find the first value which is incorrect. In your example, it's the first pixel in second row (i.e. first row's results are correct - that's valuable info!).
Expected result: 113
Actual result: 92
Follow the execution path for that pixel. Your code has 9 nearly identical sections; this specific example points you exactly in which section to look.
So in this project , I need to take an image.bmp file and blur it using "box blur".
The instructions for Box Blurring as follows:
For this problem, we’ll use the “box blur,” which works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels.
(example) The new value of each pixel would be the average of the values of all of the pixels that are within 1 row and column of the original pixel (forming a 3x3 box). For example, each of the color values for pixel 6 would be obtained by averaging the original color values of pixels 1, 2, 3, 5, 6, 7, 9, 10, and 11 (note that pixel 6 itself is included in the average). Likewise, the color values for pixel 11 would be be obtained by averaging the color values of pixels 6, 7, 8, 10, 11, 12, 14, 15 and 16.
For a pixel along the edge or corner, like pixel 15, we would still look for all pixels within 1 row and column: in this case, pixels 10, 11, 12, 14, 15, and 16.
Now I know my code is a bit long and amateur,but it is only because I am an amateur but I have come this far without any help:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// iterate through every row
for (int i = 0; i < height; i++)
{
//for every column
for (int j = 0; j < width; j++)
{
int avRed = 0;
int avGreen = 0;
int avBlue = 0;
// check if the current index is an index of an edge
if ((i == 0 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height - 1) && j == 0)
|| (i == height - 1 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height -1) && j == width - 1))
{
// if it is : check if it is a corner
if ((i == 0 && j == 0)
|| (i == 0 && j == width - 1)
|| (i == height - 1 && j == 0)
|| (i == height - 1 && j == width - 1))
{
if (i == 0 && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == 0 && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i +1 ][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if(i == height - 1 && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
avGreen= round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
avRed = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == height - 1 && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
else // if it is not a corner index
{
if (i == 0 && (j >= 1 && j <= width - 2))
{
avRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if ((i >= 1 && i <= height - 2) && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == height - 1 && (j >= 1 && j <= width - 2))
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if ((i >= 1 && i <= height -2) && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
}
else // if it is not an index of an edge at all
{
avRed = round(((image[i - 1][j - 1].rgbtRed) + (image[i - 1][j].rgbtRed) + (image[i - 1][j + 1].rgbtRed) + (image[i][j - 1].rgbtRed) + (image[i][j].rgbtRed) + (image[i][j + 1].rgbtRed) + (image[i + 1][j - 1].rgbtRed) + (image[i + 1][j].rgbtRed) + (image[i + 1][j + 1].rgbtRed)) / 9.0);
avGreen = round(((image[i - 1][j - 1].rgbtGreen) + (image[i - 1][j].rgbtGreen) + (image[i - 1][j + 1].rgbtGreen) + (image[i][j - 1].rgbtGreen) + (image[i][j].rgbtGreen) + (image[i][j + 1].rgbtGreen) + (image[i + 1][j - 1].rgbtGreen) + (image[i + 1][j].rgbtGreen) + (image[i + 1][j + 1].rgbtGreen)) / 9.0);
avBlue = round(((image[i - 1][j - 1].rgbtBlue) + (image[i - 1][j].rgbtBlue) + (image[i - 1][j + 1].rgbtBlue) + (image[i][j - 1].rgbtBlue) + (image[i][j].rgbtBlue) + (image[i][j + 1].rgbtBlue) + (image[i + 1][j - 1].rgbtBlue) + (image[i + 1][j].rgbtBlue) + (image[i + 1][j + 1].rgbtBlue)) / 9.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
}
return;
}
Now at this point, program somewhat blurs the image, but not for the right amount. When I test it via cs50's pre-set test cases, my RGB ratios are correct for corner indexes, but off for all the others. Why that might be happening?
first make shure to copy the orignal image's pixels into another varial of type RGBTRIPLE pixel by pixel using forloop if not the first blured pixel would affect the next pixel to abe blured then do you math.
this works for me!
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
copy[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// check for an edge
if ((i == 0 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height - 1) && j == 0)
|| (i == height - 1 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height -1) && j == width - 1))
{
//check if it is a corner
if ((i == 0 && j == 0)
|| (i == 0 && j == width - 1)
|| (i == height - 1 && j == 0)
|| (i == height - 1 && j == width - 1))
{
if (i == 0 && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i][j + 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i][j + 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i][j + 1].rgbtBlue) / 4.0);
}
if (i == 0 && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i +1 ][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j - 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j - 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j - 1].rgbtBlue) / 4.0);
}
if (i == height - 1 && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j + 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j + 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j + 1].rgbtBlue) / 4.0);
}
if (i == height - 1 && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j - 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j - 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j - 1].rgbtBlue) / 4.0);
}
}
// if not a corner index
else
{
if (i == 0 && (j >= 1 && j <= width - 2))
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
}
if ((i >= 1 && i <= height - 2) && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
}
if (i == height - 1 && (j >= 1 && j <= width - 2))
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i - 1][j - 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue) / 6.0);
}
if ((i >= 1 && i <= height -2) && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i + 1][j - 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue) / 6.0);
}
}
}
// for normal pixel no edge at all
else
{
image[i][j].rgbtRed = (int) round(((copy[i - 1][j - 1].rgbtRed) + (copy[i - 1][j].rgbtRed) + (copy[i - 1][j + 1].rgbtRed) + (copy[i][j - 1].rgbtRed) + (copy[i][j].rgbtRed) + (copy[i][j + 1].rgbtRed) + (copy[i + 1][j - 1].rgbtRed) + (copy[i + 1][j].rgbtRed) + (copy[i + 1][j + 1].rgbtRed)) / 9.0);
image[i][j].rgbtGreen = (int) round(((copy[i - 1][j - 1].rgbtGreen) + (copy[i - 1][j].rgbtGreen) + (copy[i - 1][j + 1].rgbtGreen) + (copy[i][j - 1].rgbtGreen) + (copy[i][j].rgbtGreen) + (copy[i][j + 1].rgbtGreen) + (copy[i + 1][j - 1].rgbtGreen) + (copy[i + 1][j].rgbtGreen) + (copy[i + 1][j + 1].rgbtGreen)) / 9.0);
image[i][j].rgbtBlue = (int) round(((copy[i - 1][j - 1].rgbtBlue) + (copy[i - 1][j].rgbtBlue) + (copy[i - 1][j + 1].rgbtBlue) + (copy[i][j - 1].rgbtBlue) + (copy[i][j].rgbtBlue) + (copy[i][j + 1].rgbtBlue) + (copy[i + 1][j - 1].rgbtBlue) + (copy[i + 1][j].rgbtBlue) + (copy[i + 1][j + 1].rgbtBlue)) / 9.0);
}
}
}
return;
}
I am doing a cs50 assignment filter(blur). i didnt do it really well but it is working. bluring all the pixels except the first row(north) and the final row (south) in the pic. and the corners of the picture are blured but the pixels after the corners the colors are really unusual. i dont know its the conditions or the the color changing that messed this up. so i can use another pair of eyes for finding the problem thank you
[click here to see the issues in detail]
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE changed[height][width];
for(int i=0 ; i<=height-1 ;i++)
{
for(int j=0 ; j<=width-1 ; j++)
{
if(i==0) //main if north corners
{
if (j==0)
{
changed[i][j].rgbtRed =round( (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /4);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /4);
}
else if(j==width-1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /4);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /4);
}
}
else if(i==height-1) //main if south corners
{
if (j==0)
{
changed[i][j].rgbtRed = round( (image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) /4);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /4);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /4);
}
else if(j==width-1)
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed) /4);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen) /4);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue) /4);
}
}
else if(i>0 && i<height-1 && j==0) //main if left side
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /6);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /6);
}
else if(i>0 && i<height-1 && j==width-1) //main if right side
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /6);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /6);
}
else if(i==0 && j>0 && j<width-1) //main if north side
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed + image[i][j+1].rgbtRed) /6);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /6);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /6);
}
else if(i==height-1 && j>0 && j>width-1) //main if south side
{
changed[i][j].rgbtRed = round( (image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) /6);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /6);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /6);
}
else //other pixels in the middle
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed) /9);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen) /9);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue) /9);
}
}
}
for (int i = 0 ;i<=height-1 ; i++)
{
for (int j = 0 ; j<=width-1 ; j++)
{
image[i][j]=changed[i][j];
}
}
return;
}
bluring all the pixels except the first row(north) and the final row (south) in the pic
If you go through the case discriminations with all those ifs and elses, you'll find that the code blocks that should handle the north and south edges between the corners are not entered after the preceding if(i==0) and if(i==height-1); indeed with that many cases that was easy to miss. You could move the code blocks or you could make the whole operation more concise, e. g.:
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
{
int r = 0, g = 0, b = 0, c = 0; // surrounding pixel color sums and count
for (int y = i-1; y <= i+1; ++y) if (0 <= y && y < height)
for (int x = j-1; x <= j+1; ++x) if (0 <= x && x < width)
r += image[y][x].rgbtRed,
g += image[y][x].rgbtGreen,
b += image[y][x].rgbtBlue,
++c;
// now compute the rounded averages
changed[i][j].rgbtRed = (r+c/2)/c;
changed[i][j].rgbtGreen = (g+c/2)/c;
changed[i][j].rgbtBlue = (b+c/2)/c;
}
This is the changed code
RGBTRIPLE changed[height][width];
for (int i = 0 ; i <= height - 1 ;i++)
{
for (int j = 0 ; j <= width - 1 ; j++)
{
//main if north side
if (i == 0 && j > 0 && j < width - 1 )
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / (6.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / (6.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / (6.0));
}
//main if south side
else if (i==height - 1 && j > 0 && j > width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / (6.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / (6.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / (6.0));
}
//main if left side
else if (i > 0 && i < height - 1 && j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);
}
//main if right side
else if (i > 0 && i < height - 1 && j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);
}
//main if north corners
if (i == 0)
{
//north left corner
if (j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);
}
//north left corner
else if (j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);
}
}
//main if south corners
else if (i == height - 1)
{
//south right corner
if (j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
}
//south left corner
else if (j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
}
}
else //other pixels in the middle
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / (9.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / (9.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / (9.0));
}
}
}
//putting the changed color value in original image(applying the blue filter)
for (int i = 0 ; i <= height - 1 ; i++)
{
for (int j = 0 ; j <= width - 1 ; j++)
{
image[i][j] = changed[i][j];
}
}
return;
I wrote a code for the blur function, but it returns a wrong output for reason which is not known for me (it just looks fine for me), here is an example (always same):
:( blur correctly filters middle pixel
expected "127 140 149\n", not "126 140 149\n"
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
and the code is here:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE original[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
original[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue) / 4);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i + 1][j - 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue) / 4);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue) / 6);
}
}
else if (i > 0 && i < height - 1)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j].rgbtRed + original[i + 1][j + 1].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i- 1][j].rgbtGreen + original[i + 1][j + 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i + 1][j + 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 6);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i- 1][j].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue) / 6);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i + 1][j + 1].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j - 1].rgbtRed + original[i - 1][j + 1].rgbtRed + original[i - 1][j].rgbtRed) / 9);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen+ original[i + 1][j].rgbtGreen + original[i - 1][j - 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen + original[i - 1][j].rgbtGreen) / 9);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j - 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue + original[i - 1][j].rgbtBlue) / 9);
}
}
else if (i == height - 1)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 4);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue) / 4);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 6);
}
}
}
}
}
if the members of the structure RGBTRIPLE has integer types, integer division happens in the lines like
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 4);
and the results are truncated to integers before passed to the round() function.
The dividers should be floating-point numbers like 4.0 instead of integers like 4.
If the judge still rejects your answer after applying this change, it should be due to the calculation errors in floating-point numbers. In this case, you should avoid using floating-point number calculation and think how to calculate the desired values using only calculation in integers.
My solution is much short I guess
void blur(int height, int width, RGBTRIPLE image[height][width])
{
//Iterates through all the pixels
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
//Stores average of all the pixels around the certain color
int avgrgb[3] = {0,0,0}, n = 0;
// Matrix “a” and “b” represent the nine pixels address around the pixel of address i and j
int a[3][3] = { {i-1, i-1, i-1}, {i, i, i}, {i+1, i+1,i+1} }, b[3][3] = { {j-1, j, j+1}, {j-1, j, j+1}, {j-1, j, j+1} };
//Iterating through the pixels around the middle pixel
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
// Checking if the index contains any negative integer(negative integer represents a pixel outside the image for corner or edge pixel)
if (a[x][y] >= 0 && a[x][y] >= 0)
{
// Calculates the sum of the valid pixel colours
avgrgb[0] += image[a[x][y]][b[x][y]].rgbtRed;
avgrgb[1] += image[a[x][y]][b[x][y]].rgbtGreen;
avgrgb[2] += image[a[x][y]][b[x][y]].rgbtBlue;
n++;
}
}
}
// Calculates the actual average
avgrgb[0] /= n;
avgrgb[1] /= n;
avgrgb[2] /= n;
// Assigning new pixel values
image[i][j].rgbtRed = avgrgb[0];
image[i][j].rgbtGreen = avgrgb[1];
image[i][j].rgbtBlue = avgrgb[2];
}
}
return;
}
MikeCAT's answer is surely right. Just as an alternative to his suggestion, you could avoid the conversions to and from floating point by adding half the divisor before the integer division, e. g.
image[i][j].rgbtRed = (original[i][j].rgbtRed + … + original[i+1][j+1].rgbtRed + 2) / 4;
…
image[i][j].rgbtRed = (original[i][j].rgbtRed + … + original[i-1][j ].rgbtRed + 4) / 9;
So the problem is called "Filters" PSET4 and I was assigned to code a "blur" function that blurs an image. My code blurs the sample pictures but when I use the in-website tool to check the code it says that the program was "unable to blur a 3x3 and 4x4 pictures correctly". This is the code:
Here's what the tool says:
4x4
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image0[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image0[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);
image[0][0].rgbtRed = red00;
image[0][0].rgbtGreen = green00;
image[0][0].rgbtBlue = blue00;
}
else if (i == 0 && j == width - 1) //topright
{
float red01 = round((float)(image0[0][width - 1].rgbtRed + image0[0][width - 2].rgbtRed + image0[1][width - 2].rgbtRed + image0[1][width - 1].rgbtRed)
/ 4);
float green01 = round((float)(image0[0][width - 1].rgbtGreen + image0[0][width - 2].rgbtGreen + image0[1][width - 2].rgbtGreen + image0[1][width - 1].rgbtGreen)
/ 4);
float blue01 = round(((float)image0[0][width - 1].rgbtBlue + image0[0][width - 2].rgbtBlue + image0[1][width - 2].rgbtBlue + image0[1][width - 1].rgbtBlue)
/ 4);
image[0][width - 1].rgbtRed = red01;
image[0][width - 1].rgbtGreen = green01;
image[0][width - 1].rgbtBlue = blue01;
}
else if (i == height - 1 && j == 0) //bottomleft
{
float red10 = round((float)(image0[height - 1][0].rgbtRed + image0[height - 2][0].rgbtRed + image0[height - 2][1].rgbtRed + image0[height - 1][1].rgbtRed)
/ 4);
float green10 = round((float)(image0[height - 1][0].rgbtGreen + image0[height - 2][0].rgbtGreen + image0[height - 2][1].rgbtGreen + image0[height - 1][1].rgbtGreen)
/ 4);
float blue10 = round((float)(image0[height - 1][0].rgbtBlue + image0[height - 2][0].rgbtBlue + image0[height - 2][1].rgbtBlue + image0[height - 1][1].rgbtBlue)
/ 4);
image[height - 1][0].rgbtRed = red10;
image[height - 1][0].rgbtGreen = green10;
image[height - 1][0].rgbtBlue = blue10;
}
else if (i == height - 1 && j == width - 1) //bottomright
{
float red11 = round((float)(image0[height - 1][width - 1].rgbtRed + image0[height - 2][width - 1].rgbtRed + image0[height - 2][width - 2].rgbtRed
+ image0[height - 1][width - 2].rgbtRed) / 4);
float green11 = round((float)(image0[height - 1][width - 1].rgbtGreen + image0[height - 2][width - 1].rgbtGreen + image0[height - 2][width - 2].rgbtGreen
+ image0[height - 1][width - 2].rgbtGreen) / 4);
float blue11 = round((float)(image0[height - 1][width - 1].rgbtBlue + image0[height - 2][width - 1].rgbtBlue + image0[height - 2][width - 2].rgbtBlue
+ image0[height - 1][width - 2].rgbtBlue) / 4);
image[height - 1][width - 1].rgbtRed = red11;
image[height - 1][width - 1].rgbtGreen = green11;
image[height - 1][width - 1].rgbtBlue = blue11;
}
else if (i == 0 && j != 0 && j != width - 1) //top
{
float redtop = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i + 1][j - 1].rgbtRed
+ image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greentop = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen
+ image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluetop = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue
+ image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redtop;
image[i][j].rgbtGreen = greentop;
image[i][j].rgbtBlue = bluetop;
}
else if (i != 0 && i != height - 1 && j == 0) //left
{
float redleft = round((float)(image0[i][j].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j + 1].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenleft = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j + 1].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueleft = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j + 1].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redleft;
image[i][j].rgbtGreen = greenleft;
image[i][j].rgbtBlue = blueleft;
}
else if (i != 0 && i != height - 1 && j == width - 1) //right
{
float redright = round((float)(image0[i][j].rgbtRed + image[i - 1][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i][j - 1].rgbtRed
+ image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenright = round((float)(image0[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i][j - 1].rgbtGreen
+ image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueright = round((float)(image0[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i][j - 1].rgbtBlue
+ image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redright;
image[i][j].rgbtGreen = greenright;
image[i][j].rgbtBlue = blueright;
}
else if (i == height - 1 && j != 0 && j != width - 1) //bottom
{
float redbottom = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed
+ image0[i - 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greenbottom = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen
+ image0[i - 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluebottom = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue
+ image0[i - 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redbottom;
image[i][j].rgbtGreen = greenbottom;
image[i][j].rgbtBlue = bluebottom;
}
else
{
float red = round((float)(image0[i][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j - 1].rgbtRed + image0[i][j + 1].rgbtRed + image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed)
/ 9);
float green = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j - 1].rgbtGreen + image0[i][j + 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen)
/ 9);
float blue = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j - 1].rgbtBlue + image0[i][j + 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue)
/ 9);
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
}
}
}
return;
}
In this piece of code block, you use image in round function for three times by mistake. It should be image0.
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);