-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorphology.c
More file actions
168 lines (143 loc) · 5.49 KB
/
Copy pathmorphology.c
File metadata and controls
168 lines (143 loc) · 5.49 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
struct pixel
{
uint8_t red;
uint8_t green;
uint8_t blue;
};
uint16_t get_total_pixel_value(struct pixel pix){
return (pix.red + pix.green + pix.blue);
}
void set_pixel_value(struct pixel *pix, uint8_t red, uint8_t green, uint8_t blue){
pix->red = red;
pix->green = green;
pix->blue = blue;
}
void set_random_rgb(struct pixel *pix){
pix->red = ((rand()% 255 + rand()%75 ) % 255);
pix->green = ((rand()% 255 + rand()%75 ) % 255);
pix->blue = ((rand()% 255 + rand()%75 ) % 255);
}
void copy_pixel_value(struct pixel *pix1, struct pixel pix2){
// printf("pix2 : %d ,%d , %d", pix2->blue, pix2->green, pix2->red);
pix1->red = pix2.red;
pix1->green = pix2.green;
pix1->blue = pix2.blue;
}
int compare_two_pixels(struct pixel pix1, struct pixel pix2){
if( pix1.red == pix2.red && pix1.green == pix2.green && pix1.blue == pix2.blue){
return 1;
}
return 0;
}
int not_background(struct pixel pix){
uint16_t background_value = 0; // red + green + blue = 255 + 255 + 255 = 765
if(get_total_pixel_value(pix) != background_value){
return 1;
}
return 0;
}
int check_part(struct pixel *pixes, int *structing_image, int structing_image_height, int structing_image_width){
int total=0;
int multipy_result=0;
for(int h=0; h<structing_image_height; h++){
for(int w=0; w<structing_image_width; w++){
multipy_result = get_total_pixel_value(pixes[h*5+w]) * structing_image[h*structing_image_width+w];
if(multipy_result != 0){
total +=1;
}
}
}
return total;
}
// void erision(struct pixel *pixes, int *structing_image, int height, int width, int structing_img_height, int structing_img_width){
// int total;
// for(int h=1; h<height-1; h++){
// for(int w=1; w<width-1; w++){
// if(not_background(pixes[h*width+w])){
// total=0;
// for(int s_h=0; s_h<structing_img_height; s_h++){
// for(int s_w=0; s_w<structing_img_width; s_w++){
// int multiply_result=0;
// int starting_height = h-1;
// int starting_widht = w-1;
// multiply_result = get_total_pixel_value( pixes[ (starting_height+s_h)*width+(starting_widht+s_w)] ) * structing_image[s_h*structing_img_width+s_w];
// // printf("%d ", multiply_result);
// if(multiply_result!=0){
// total+=1;
// }
// }
// // printf("\n");
// }
// printf("%d\n ", total);
// if(total != 9){
// set_pixel_value( (pixes+h*width+w), 254, 254, 254);
// }
// else{
// set_pixel_value( (pixes+h*width+w), 255, 255, 255);
// }
// }
// }
// }
void erision(struct pixel *pixes, int *structing_image, int height, int width, int structing_img_height, int structing_img_width){
for(int h=1; h<height-1; h++){
for(int w=1; w<width-1; w++){
int result = check_part(pixes+(h-1)*width+(w-1), structing_image, structing_img_height, structing_img_width);
if(result != 9){
set_pixel_value( (pixes+h*width+w), 254, 254, 254);
}
}
}
for(int h=1; h<height-1; h++){
for(int w=1; w<width-1; w++){
if(get_total_pixel_value(pixes[h*width+w]) == 762){
set_pixel_value(pixes+h*width+w, 0 ,0 ,0);
}
}
}
}
void dilation(struct pixel *pixes, int *structing_image, int height, int width, int structing_img_height, int structing_img_width){
struct pixel *new_pixes = (struct pixel *)malloc(sizeof(struct pixel)*height*width);
memset(new_pixes, 0, sizeof(struct pixel)*height*width);
for(int h=1; h<height-1; h++){
for(int w=1; w<width-1; w++){
int result = check_part(pixes+(h-1)*width+(w-1), structing_image, structing_img_height, structing_img_width);
if(result != 0){
set_pixel_value( (new_pixes+h*width+w), 255, 255, 255);
}
}
}
for(int h=0; h<height; h++){
for(int w=0; w<width; w++){
printf("%d\t", get_total_pixel_value(new_pixes[h*width+w]));
}
printf("\n");
}printf("\n\n");
for(int h=1; h<height-1; h++){
for(int w=1; w<width-1; w++){
copy_pixel_value(pixes+h*width+w, new_pixes[h*width+w]);
}
}
free(new_pixes);
}
int main(){
int height = 5;
int width =5;
struct pixel image[] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0},{0,0,0}, {255,255,255}, {0,0,0}, {0,0,0}, {0,0,0},{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0},{0,0,0}, {0,0,0}, {0,0,0}, {255,255,255}, {0,0,0},{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};
int foreground = 1;
int structing_image[] = {foreground, foreground, foreground, foreground, foreground, foreground, foreground, foreground, foreground};
// erision(image, structing_image, height, width, 3, 3);
dilation(image, structing_image, height, width, 3, 3);
for(int h=0; h<height; h++){
for(int w=0; w<width; w++){
printf("%d\t", get_total_pixel_value(image[h*width+w]));
}
printf("\n");
}
return 0;
}