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;
}
CodePudding user response:
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.