Skip to content

Commit d48c4bd

Browse files
committed
Made function for the sobel algorithm
1 parent d8e3f6c commit d48c4bd

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ check:
77

88
run:
99
convert imgs/img.png imgs/img.rgb
10-
./src/sobel imgs/img.rgb imgs/img_out.gray -i imgs/img_out_h.gray imgs/img_out_v.gray
10+
./src/sobel imgs/img.rgb imgs/img_out.gray -g imgs/img_out_gray.gray -i imgs/img_out_h.gray imgs/img_out_v.gray
1111
convert -size 512x512 -depth 8 imgs/img_out.gray imgs/img_out.png
12+
convert -size 512x512 -depth 8 imgs/img_out_gray.gray imgs/img_out_gray.png
1213
convert -size 512x512 -depth 8 imgs/img_out_h.gray imgs/img_out_h.png
1314
convert -size 512x512 -depth 8 imgs/img_out_v.gray imgs/img_out_v.png
1415

src/main.c

+16-18
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66
#include "file_operations.h"
77

88
int main(int argc, char *argv[]) {
9-
char *file_in, *file_out, *file_out_h, *file_out_v, *file_gray;
10-
int inter_files = 0, gray_file = 0;
11-
byte *rgb, *gray;
12-
int rgb_size, gray_size;
13-
int sobel_h[] = {1, 0, -1, 2, 0, -2, 1, 0, -1},
14-
sobel_v[] = {1, 2, 1, 0, 0, 0, -1, -2, -1};
15-
byte *sobel_h_res, *sobel_v_res;
16-
byte *contour_img;
9+
char *file_in,
10+
*file_out,
11+
*file_out_h,
12+
*file_out_v,
13+
*file_gray;
14+
15+
byte *rgb,
16+
*gray,
17+
*sobel_h_res,
18+
*sobel_v_res,
19+
*contour_img;
20+
21+
int inter_files = 0,
22+
gray_file = 0;
1723

1824
// Get arguments
1925
if(argc < 3) {
@@ -61,29 +67,21 @@ int main(int argc, char *argv[]) {
6167
}
6268

6369
// Read file to rgb and get size
64-
rgb_size = readFile(file_in, &rgb);
70+
int rgb_size = readFile(file_in, &rgb);
6571

66-
// Get gray representation of the image
67-
gray_size = rgbToGray(rgb, &gray, rgb_size);
72+
int gray_size = sobelFilter(rgb, &gray, &sobel_h_res, &sobel_v_res, &contour_img, rgb_size);
6873

6974
// Write gray image
7075
if(gray_file) {
7176
writeFile(file_gray, gray, gray_size);
7277
}
7378

74-
// Make sobel operations
75-
itConv(gray, gray_size, sobel_h, &sobel_h_res);
76-
itConv(gray, gray_size, sobel_v, &sobel_v_res);
77-
7879
// Write image after each sobel operator
7980
if(inter_files) {
8081
writeFile(file_out_h, sobel_h_res, gray_size);
8182
writeFile(file_out_v, sobel_v_res, gray_size);
8283
}
8384

84-
// Calculate contour matrix
85-
contour(sobel_h_res, sobel_v_res, gray_size, &contour_img);
86-
8785
// Write sobel img to a file
8886
writeFile(file_out, contour_img, gray_size);
8987

src/sobel.c

+16
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,20 @@ void contour(byte *sobel_h, byte *sobel_v, int gray_size, byte **contour_img) {
107107
}
108108
}
109109

110+
int sobelFilter(byte *rgb, byte **gray, byte **sobel_h_res, byte **sobel_v_res, byte **contour_img, int rgb_size) {
111+
int sobel_h[] = {1, 0, -1, 2, 0, -2, 1, 0, -1},
112+
sobel_v[] = {1, 2, 1, 0, 0, 0, -1, -2, -1};
113+
114+
// Get gray representation of the image
115+
int gray_size = rgbToGray(rgb, gray, rgb_size);
116+
117+
// Make sobel operations
118+
itConv(*gray, gray_size, sobel_h, sobel_h_res);
119+
itConv(*gray, gray_size, sobel_v, sobel_v_res);
120+
121+
// Calculate contour matrix
122+
contour(*sobel_h_res, *sobel_v_res, gray_size, contour_img);
123+
124+
return gray_size;
125+
}
110126

src/sobel.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ void makeOpMem(byte *buffer, int buffer_size, int cindex, byte *op_mem);
88
int convolution(byte *X, int *Y, int c_size);
99
void itConv(byte *buffer, int buffer_size, int *op, byte **res);
1010
void contour(byte *sobel_h, byte *sobel_v, int gray_size, byte **contour_img);
11+
int sobelFilter(byte *rgb, byte **gray, byte **sobel_h_res, byte **sobel_v_res, byte **contour_img, int rgb_size);
1112

1213
#endif
1314

0 commit comments

Comments
 (0)