-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBMP_ImageFilter_helper.c
150 lines (146 loc) · 5.27 KB
/
BMP_ImageFilter_helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "helpers.h"
#include<math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
float avg_color;
int color;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
avg_color = (float)(image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed)
/ 3; //Calculates value of average RGB intensity
color = round(avg_color);
image[i][j].rgbtBlue = color; //Assigns grayscale value to Red channel values
image[i][j].rgbtGreen = color; //Assigns grayscale value to Green channel value
image[i][j].rgbtRed = color; //Assigns grayscale value to Blue channel values
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp_image;
for (int i = 0; i < height; i++)
{
for (int j = 0, k = width - 1; j < width / 2; j++, k--) // Reverses pixels in each row to get reflected image
{
temp_image = image[i][j];
image[i][j] = image[i][k];
image[i][k] = temp_image;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image_copy[height][width];
int c;
float sum_Blue, sum_Green, sum_Red;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
c = 0;
sum_Blue = 0;
sum_Green = 0;
sum_Red = 0;
for (int a = i - 1; a < i + 2; a++)
{
for (int b = j - 1; b < j + 2; b++)
{
if ((a >= 0) && (a < height) && (b >= 0) && (b < width))
{
sum_Blue += image[a][b].rgbtBlue;
sum_Green += image[a][b].rgbtGreen;
sum_Red += image[a][b].rgbtRed;
c++;
}
}
}
image_copy[i][j].rgbtBlue = round(sum_Blue / (float)c); // Calculates average value of RGB for neighbouring pixels in a 3x3 box
image_copy[i][j].rgbtGreen = round(sum_Green / (float)c);
image_copy[i][j].rgbtRed = round(sum_Red / (float)c);
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = image_copy[i][j]; // Assigns modified values to original image
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image_copy[height][width];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}, Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}}; // Declares kernel matrices
float gx_Blue, gx_Green, gx_Red, gy_Blue, gy_Green, gy_Red, Blue, Red, Green;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
gx_Blue = 0;
gx_Green = 0;
gx_Red = 0;
gy_Blue = 0;
gy_Green = 0;
gy_Red = 0;
for (int a = i - 1, m = 0; a < i + 2; a++, m++)
{
for (int b = j - 1, n = 0; b < j + 2; b++, n++)
{
if ((a >= 0) && (a < height) && (b >= 0) && (b < width))
{
gx_Blue += Gx[m][n] * image[a][b].rgbtBlue;
gx_Green += Gx[m][n] * image[a][b].rgbtGreen;
gx_Red += Gx[m][n] * image[a][b].rgbtRed;
gy_Blue += Gy[m][n] * image[a][b].rgbtBlue;
gy_Green += Gy[m][n] * image[a][b].rgbtGreen;
gy_Red += Gy[m][n] * image[a][b].rgbtRed;
}
Blue = round(pow((pow(gx_Blue, 2) + pow(gy_Blue, 2)), 0.5));
Green = round(pow((pow(gx_Green, 2) + pow(gy_Green, 2)), 0.5));
Red = round(pow((pow(gx_Red, 2) + pow(gy_Red, 2)), 0.5));
if (Blue > 255) // Checks if calculated value is greater than 255
{
image_copy[i][j].rgbtBlue = 255;
}
else // Else assigns calculated value
{
image_copy[i][j].rgbtBlue = Blue;
}
if (Green > 255) // Checks if calculated value is greater than 255
{
image_copy[i][j].rgbtGreen = 255;
}
else // Else assigns calculated value
{
image_copy[i][j].rgbtGreen = Green;
}
if (Red > 255) // Checks if calculated value is greater than 255
{
image_copy[i][j].rgbtRed = 255;
}
else // Else assigns calculated value
{
image_copy[i][j].rgbtRed = Red;
}
}
}
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = image_copy[i][j]; // Assigns modified values to original image
}
}
return;
}