I have wrong output values :(
:( edges correctly filters middle pixel
expected "210 150 60\n", not "210 32 203\n"
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "213 228 166\n"
:( edges correctly filters pixel in corner
expected "76 117 255\n", not "76 117 162\n"
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "76 117 162\n21..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "76 117 162\n21..."
Some of them are correct (color channels), but others are not, I'm not sure why.
code:
#include "helpers.h"
#include <math.h>
RGBTRIPLE calculate_sum_of_squered_Gx_and_GY(RGBTRIPLE Gx_sum, RGBTRIPLE Gy_sum, int cap_value);
RGBTRIPLE trim_channel_to_max_value(RGBTRIPLE Gxsq_Gysq, int cap_value);
RGBTRIPLE pass_value_to_temp_image_px(RGBTRIPLE temp_image_px, RGBTRIPLE value);
RGBTRIPLE refresh_values_to_0();
RGBTRIPLE count_G_sum(int h_px, int w_px, int im_height, int im_width, RGBTRIPLE image[im_height][im_width], int rows, int cols, int G_Matrix[rows][cols]);
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
const int HEX_W = 3;
const int HEX_H = 3;
int Gx_Matrix[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy_Matrix[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
RGBTRIPLE Gx_sum, Gy_sum, Gxsq_Gysq;
const int cap_value = 255;
RGBTRIPLE temp_image[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// take values from all sticking nearby channels to Gx && Gy
Gx_sum = count_G_sum(i, j, height, width, image, 3, 3, Gx_Matrix);
Gy_sum = count_G_sum(i, j, height, width, image, 3, 3, Gy_Matrix);
Gxsq_Gysq = calculate_sum_of_squered_Gx_and_GY(Gx_sum, Gy_sum, cap_value);
temp_image[i][j] = pass_value_to_temp_image_px(temp_image[i][j], Gxsq_Gysq);
Gxsq_Gysq = refresh_values_to_0();
Gy_sum = refresh_values_to_0();
Gx_sum = refresh_values_to_0();
}
}
// pass channels values from temp image to original image
for (int m = 0; m < height; m++)
{
for (int o = 0; o < width; o++)
{
image[m][o].rgbtRed = temp_image[m][o].rgbtRed;
image[m][o].rgbtGreen = temp_image[m][o].rgbtGreen;
image[m][o].rgbtBlue = temp_image[m][o].rgbtBlue;
}
}
}
RGBTRIPLE count_G_sum(int h_px, int w_px, int im_height, int im_width, RGBTRIPLE image[im_height][im_width], int rows, int cols, int G_Matrix[rows][cols])
{
RGBTRIPLE G_sum;
G_sum.rgbtRed = 0;
G_sum.rgbtGreen = 0;
G_sum.rgbtBlue = 0;
for (int h = 0; h < 3; h++)
{
if (h_px - 1 + h >= 0 && h_px - 1 + h < im_height)
{
for (int w = 0; w < 3; w++)
{
if (w_px - 1 + w >= 0 && w_px - 1 + w < im_width)
{
G_sum.rgbtRed += image[h_px - 1 + h][w_px - 1 + w].rgbtRed * G_Matrix[h][w];
G_sum.rgbtGreen += image[h_px - 1 + h][w_px - 1 + w].rgbtGreen * G_Matrix[h][w];
G_sum.rgbtBlue += image[h_px - 1 + h][w_px - 1 + w].rgbtBlue * G_Matrix[h][w];
}
}
}
}
return G_sum;
}
RGBTRIPLE calculate_sum_of_squered_Gx_and_GY(RGBTRIPLE Gx_sum, RGBTRIPLE Gy_sum, int cap_value)
{
RGBTRIPLE Gxsq_Gysq;
// calculating squared sum of matrixes
Gxsq_Gysq.rgbtRed = round( sqrtf( pow(Gx_sum.rgbtRed, 2) + pow(Gy_sum.rgbtRed, 2)));
Gxsq_Gysq.rgbtGreen = round( sqrtf( pow(Gx_sum.rgbtGreen, 2) + pow(Gy_sum.rgbtGreen, 2)));
Gxsq_Gysq.rgbtBlue = round( sqrtf( pow(Gx_sum.rgbtBlue, 2) + pow(Gy_sum.rgbtBlue, 2)));
Gxsq_Gysq = trim_channel_to_max_value(Gxsq_Gysq, cap_value);
return Gxsq_Gysq;
}
RGBTRIPLE trim_channel_to_max_value(RGBTRIPLE Gxsq_Gysq, int cap_value)
{
if (Gxsq_Gysq.rgbtRed > cap_value)
{
Gxsq_Gysq.rgbtRed = cap_value;
}
if (Gxsq_Gysq.rgbtGreen > cap_value)
{
Gxsq_Gysq.rgbtGreen = cap_value;
}
if (Gxsq_Gysq.rgbtBlue > cap_value)
{
Gxsq_Gysq.rgbtBlue = cap_value;
}
return Gxsq_Gysq;
}
RGBTRIPLE pass_value_to_temp_image_px(RGBTRIPLE temp_image_px, RGBTRIPLE value)
{
temp_image_px.rgbtRed = value.rgbtRed;
temp_image_px.rgbtGreen = value.rgbtGreen;
temp_image_px.rgbtBlue = value.rgbtBlue;
return temp_image_px;
}
RGBTRIPLE refresh_values_to_0()
{
RGBTRIPLE target_file;
target_file.rgbtRed = 0;
target_file.rgbtGreen = 0;
target_file.rgbtBlue = 0;
return target_file;
}
Also fact that I can't get output image for some weird reason doesn't help either.
Thanks in advance for any help.