-
Notifications
You must be signed in to change notification settings - Fork 3
/
EdgeDetectionfinalparallel.c
354 lines (297 loc) · 10.4 KB
/
EdgeDetectionfinalparallel.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Basic Image Processing Library using OpenMp
Task : Edge Detection using Sobel operator implemented in parallely using OpenMp
Team Trident
Rushikesh Korde 2017BTECS00053
Saurabh Zade 2017BTECS00077
Rahul Sonone 2017BTECS00103
*/
#include <stdlib.h>
#include <stdio.h>
#include <png.h>
#include<omp.h>
#include<string.h>
void edgeDetection(char *filename,char *outputFileName)
{
int width,height,dimensions;
png_byte color_type;
png_byte bit_depth;
png_bytep *row_pointers = NULL;
fflush(stdin);
FILE *fp = fopen(filename, "rb");
if(fp==NULL){
return ;
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png) {
abort();
}
png_infop info = png_create_info_struct(png);
if(!info){
abort();
}
if(setjmp(png_jmpbuf(png))){
abort();
}
png_init_io(png, fp);
png_read_info(png, info);
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
color_type = png_get_color_type(png, info);
bit_depth = png_get_bit_depth(png, info);
// Read any color_type into 8bit depth, RGBA format.
// See http://www.libpng.org/pub/png/libpng-manual.txt
dimensions = height;
if(bit_depth == 16)
png_set_strip_16(png);
if(color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png);
// PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png);
if(png_get_valid(png, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png);
// These color_type don't have an alpha channel then fill it with 0xff.
if(color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_PALETTE)
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
if(color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
png_read_update_info(png, info);
//if (row_pointers) abort();
row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
for(int y = 0; y < height; y++) {
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info));
}
png_read_image(png, row_pointers);
fclose(fp);
png_destroy_read_struct(&png, &info, NULL);
int maskMatrix[3][3]={-1,-2,-1,0,0,0,1,2,1};
int averageR,averageG,averageB;
int size = height * width;
int *maskedImageR = (int*)malloc(size*sizeof(int));
int *maskedImageG = (int*)malloc(size*sizeof(int));
int *maskedImageB = (int*)malloc(size*sizeof(int));
for(int i=0;i<size;i++){
maskedImageR[i]=0;
maskedImageG[i]=0;
maskedImageR[i]=0;
}
//maskR function begins
int val=0,maskDimensions=3;
for(int i=0 ; i < height; i++)
{
for(int j=0 ; j < width ; j++)
{
averageR = 0;
averageG = 0;
averageB = 0;
for(int itImageX = i-1, itMaskX = 0; itImageX < (i+maskDimensions) && itMaskX < maskDimensions ;itImageX++,itMaskX++)
{
if(itImageX!=-1)
{
png_bytep row = row_pointers[itImageX];
for(int itImageY = j-1, itMaskY =0; itImageY < (j+maskDimensions) && itMaskY < maskDimensions; itImageY++,itMaskY++)
{
png_bytep px = &(row[itImageY * 4]);
if(itImageX >= 0 && itImageX < height && itImageY >= 0 && itImageY < width){
averageR += (px[0] * maskMatrix[itMaskX][itMaskY]);
averageG += (px[1] * maskMatrix[itMaskX][itMaskY]);
averageB += (px[2] * maskMatrix[itMaskX][itMaskY]);
}
}
}
}
if(averageR < 0)
averageR = 0;
else if(averageR > 255)
averageR = 255;
maskedImageR[i*width + j]+= averageR/9;
if(averageG < 0)
averageG = 0;
else if(averageG > 255)
averageG = 255;
maskedImageG[i*width + j] += averageG/9;
if(averageB < 0)
averageB = 0;
else if(averageB > 255)
averageB = 255;
maskedImageB[i*width + j] += averageB/9;
}
}
val=0;
maskMatrix[0][0] = -1;
maskMatrix[0][1] = 0;
maskMatrix[0][0] = 1;
maskMatrix[1][0] = -2;
maskMatrix[1][1] = 0;
maskMatrix[1][2] = 2;
maskMatrix[2][0] = -1;
maskMatrix[2][1] = 0;
maskMatrix[2][2] = 1;
for(int i=0 ; i < height; i++)
{
for(int j=0 ; j < width ; j++)
{
averageR = 0;
averageG = 0;
averageB = 0;
for(int itImageX = i-1, itMaskX = 0; itImageX < (i+maskDimensions) && itMaskX < maskDimensions ;itImageX++,itMaskX++)
{
if(itImageX!=-1)
{
png_bytep row = row_pointers[itImageX];
for(int itImageY = j-1, itMaskY =0; itImageY < (j+maskDimensions) && itMaskY < maskDimensions; itImageY++,itMaskY++)
{
png_bytep px = &(row[itImageY * 4]);
if(itImageX >= 0 && itImageX < height && itImageY >= 0 && itImageY < width){
averageR += (px[0] * maskMatrix[itMaskX][itMaskY]);
averageG += (px[1] * maskMatrix[itMaskX][itMaskY]);
averageB += (px[2] * maskMatrix[itMaskX][itMaskY]);
}
}
}
}
if(averageR < 0)
averageR = 0;
else if(averageR > 255)
averageR = 255;
maskedImageR[i*width + j]+= averageR/9;
if(averageG < 0)
averageG = 0;
else if(averageG > 255)
averageG = 255;
maskedImageG[i*width + j] += averageG/9;
if(averageB < 0)
averageB = 0;
else if(averageB > 255)
averageB = 255;
maskedImageB[i*width + j] += averageB/9;
}
}
val=0;
//sobel edge operators
maskMatrix[0][0] = -1;
maskMatrix[0][1] = -1;
maskMatrix[0][0] = -1;
maskMatrix[1][0] = -1;
maskMatrix[1][1] = 8;
maskMatrix[1][2] = -1;
maskMatrix[2][0] = -1;
maskMatrix[2][1] = -1;
maskMatrix[2][2] = -1;
for(int i=0 ; i < height; i++){
for(int j=0 ; j < width ; j++){
averageR = 0;
averageG = 0;
averageB = 0;
for(int itImageX = i-1, itMaskX = 0; itImageX < (i+maskDimensions) && itMaskX < maskDimensions ;itImageX++,itMaskX++)
{
if(itImageX!=-1)
{
png_bytep row = row_pointers[itImageX];
for(int itImageY = j-1, itMaskY =0; itImageY < (j+maskDimensions) && itMaskY < maskDimensions; itImageY++,itMaskY++){
png_bytep px = &(row[itImageY * 4]);
if(itImageX >= 0 && itImageX < height && itImageY >= 0 && itImageY < width){
averageR += (px[0] * maskMatrix[itMaskX][itMaskY]);
averageG += (px[1] * maskMatrix[itMaskX][itMaskY]);
averageB += (px[2] * maskMatrix[itMaskX][itMaskY]);
}
}
}
}
if(averageR < 0)
averageR = 0;
else if(averageR > 255)
averageR = 255;
maskedImageR[i*width + j]+= averageR/9;
if(averageG < 0)
averageG = 0;
else if(averageG > 255)
averageG = 255;
maskedImageG[i*width + j] += averageG/9;
if(averageB < 0)
averageB = 0;
else if(averageB > 255)
averageB = 255;
maskedImageB[i*width + j] += averageB/9;
}
}
int x=0;
for(int i=0;i<height;i++)
{
png_bytep row = row_pointers[i];
for(int j=0;j<width;j++)
{
png_bytep px = &(row[j * 4]);
px[0] = maskedImageR[x];
px[1] = maskedImageG[x];
px[2] = maskedImageB[x];
x++;
}
}
fp = fopen(outputFileName, "wb");
if(!fp) abort();
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) abort();
info = png_create_info_struct(png);
if (!info) abort();
if (setjmp(png_jmpbuf(png))) abort();
png_init_io(png, fp);
// Output is 8bit depth, RGBA format.
png_set_IHDR(
png,
info,
width, height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png, info);
png_write_image(png, row_pointers);
png_write_end(png, NULL);
fclose(fp);
png_destroy_write_struct(&png, &info);
}
int main() {
double startTime = omp_get_wtime();
#pragma omp parallel for
for(int i=1;i<=800;i++)
{
char str[25]="cat (";
char out[25]="out (";
int a=i;
int tmp=i;
int cnt=0;
while(tmp)
{
tmp=tmp/10;
cnt++;
}
int j=cnt-1;
char pok[25]=").png";
char lok[25];
while(a)
{
int k=a%10;
lok[j]=(char)('0'+k);
a=a/10;
j--;
}
lok[cnt]='\0';
strcat(str,lok);
strcat(str,pok);
strcat(out,lok);
strcat(out,pok);
char* s=out;
char* p=str;
edgeDetection(p,s);
}
double endTime = omp_get_wtime();
printf("\nThe time required is %lf\n",endTime - startTime);
return 0;
}