forked from uneupane/guided-filter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
134 lines (107 loc) · 3.37 KB
/
main.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
/* Guided Image Filtering by by Kaiming He, Jian Sun, and Xiaoou Tang, in ECCV 2010 (Oral)
% GUIDEDFILTER O(1) time implementation of guided filter
%
% - guidance image: I (should be a gray-scale/single channel image)
% - filtering input image: p (should be a gray-scale/single channel image)
% - local window radius: r
% - regularization parameter: eps
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadPGM(FILE* , unsigned char** , int* , int* );
void WritePGM(int, int, unsigned char *, unsigned char *, FILE*);
void printMat2(unsigned char* mat, int rows, int cols);
void printMat(float* mat, int rows, int cols);
void guidedFilter(float* guidance, float * src,float * dest,int radius,float eps, int rows, int cols);
main(int argc, char *argv[])
{
int rows, cols, radius;
float eps;
unsigned char *image1;
unsigned char *image2;
unsigned char *output;
float *fimage1;
float *fimage2;
float *distance;
float *filtered_image;
FILE * fp;
int i,j;
if (argc != 6){
printf("Usage: Test <guidance_image_filename><input_filtering_image_filename><radius><eps><output_filename> \n");
printf(" <guidance_image_filename>: PGM file \n");
printf(" <input_filtering_image_filename>: PGM file \n");
printf(" <local window radius>: r\n");
printf(" <regularization parameter>: eps\n");
printf(" <output_filename>: PGM file \n");
exit(0);
}
printf("Reading PGM.... \n");
if ((fp=fopen(argv[1], "rb"))==NULL){
printf("reading error...\n");
exit(0);
}
ReadPGM(fp,&image1,&rows,&cols);
//printMat2(image1,rows,cols);
if ((fp=fopen(argv[2], "rb"))==NULL){
printf("reading error...\n");
exit(0);
}
ReadPGM(fp,&image2,&rows,&cols);
if (argv[3]==NULL){
printf("Please enter the local window radius...\n");
exit(0);
}
else
radius = atoi(argv[3]);
printf("\n Radius = %d\n", radius);
eps = atof(argv[4]);
printf("\n eps = %f\n", eps);
/* you may replace your own applications here */
fimage1 = (float*)malloc(sizeof(float)*rows*cols);
for (j=0; j<rows; j++) {
for (i=0; i<cols; i++){
fimage1[j*cols+i] = (float)image1[j*cols+i]/255;
}
}
fimage2 = (float*)malloc(sizeof(float)*rows*cols);
for (j=0; j<rows; j++) {
for (i=0; i<cols; i++){
fimage2[j*cols+i] = (float)image2[j*cols+i]/255;
}
}
filtered_image = (float*)malloc(sizeof(float)*rows*cols);
printf("begin calculting filtered_output.... \n");
guidedFilter(fimage1, fimage2, filtered_image, radius, eps, rows, cols);
//printf("\nPrinting result\n");
//printMat(filtered_image,rows,cols);
/* end of your application */
output = (unsigned char*)malloc(sizeof(unsigned char)*rows*cols);
for (j=0; j<rows; j++){
for (i=0; i<cols; i++){
output[j*cols+i] = (unsigned char)255*filtered_image[j*cols+i];
}
}
//printf("\nPrinting result\n");
//printMat2(image,rows,cols);
//printf("\nPrinting result\n");
//printMat2(output,rows,cols);
if ((fp=fopen(argv[5], "wb"))==NULL){
printf("reading error...\n");
exit(0);
}
printf("Writing PGM....\n");
WritePGM(rows, cols, image2, output, fp);
printf("\nFreeing memory\n");
free(filtered_image);
free(output);
}
void printMat(float* mat, int rows, int cols) {
int i,j;
for (j=0; j<rows; j++) {
for (i=0; i<cols; i++){
printf("%5f ", mat[j*cols+i]);
}
printf("\n");
}
}