-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1_DemosaicMHP.cpp
More file actions
247 lines (221 loc) · 10.6 KB
/
Copy pathProblem1_DemosaicMHP.cpp
File metadata and controls
247 lines (221 loc) · 10.6 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
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
/**Main.cpp
Input arguments program.cpp inputImage.raw outputImage.raw bytesperpixel width height
Author : Taruna Agrawal
Date: 20th september 2015
email:tagrawal@usc.edu
ID: 7650184685
*/
#include "image_proc.cpp"
//Global variables
Image inImage, outImage;
/*Function implements demosaicing using MHC algoritm*/
bool Image::demosaic_MHP()
{
int newwidth = width+4;
int newheight =height+4;
unsigned char* temp;
unsigned char* newdata;
temp = new unsigned char[width*height*3]; //to store final RGB image
newdata = new unsigned char[(width+4)*(height+4)]; //to store expanded input image with first two rows/column reflected
float* floatPixel;
floatPixel = new float[(width+4)*(height+4)]; //to store expanded input image with first two rows/column reflected
/*copy input image to newdata - reflection of pixels -column wise*/
for (int i=0; i < height; i++)
{
newdata[(i+2)*newwidth] = data[(i*width)]; //1st column reflection
newdata[((i+2)*newwidth) +1] = data[(i*width)+1]; //2nd column reflection
for(int j=0; j < width; j++)
{
newdata[((i+2)*newwidth) +(j+2)] = data[(i*width) + j]; //copy original pixel value
}
newdata[(i+3)*newwidth -1] = data[((i+1)*width)-1]; //2nd column reflection
newdata[(i+3)*newwidth -2] = data[((i+1)*width)-2]; // last column reflection
}
/*copy input image to newdata - reflection of pixels -row wise*/
for (int a=0; a < newwidth; a++)
{
newdata[a] = newdata[2*newwidth +a]; //1st row
newdata[newwidth + a] = newdata[3*newwidth +a]; //2nd row
newdata[newwidth*(newheight-2) + a] = newdata[(newheight-4)*newwidth +a]; //2nd last row
newdata[newwidth*(newheight-1) + a] = newdata[(newheight-3)*newwidth +a]; //last row
}
// Demosaicing -MHC
/*Normalize pixel values*/
for (int y=0; y < newwidth*newheight; y++)
{
// int pix = (y*(newwidth*3)) + (x*3);
floatPixel[y] = (float)newdata[y]/(float)255;
}
/*MHC based on 8 filters*/
for (int y=0; y < height; y++)
{
for (int x=0; x < width; x++)
{
int pixel = (y*(width*3)) + (x*3);
float val[3];
/*if even row the R G R G pattern*/
if ((y % 2) == 0)
{
/*even pixel is R*/
if ((x % 2) == 0)
{
val[0] = (float)data[(y*width) + x]/(float)255; //Red
/*G at R locations*/
val[1] = (0.125)*((2*((floatPixel[((y+1)*newwidth) + (x+2)] +
floatPixel[((y+3)*newwidth + (x+2))]) +
(floatPixel[((y+2)*newwidth) + (x+1)] +
floatPixel[((y+2)*(newwidth) + (x+3))]))) -
(1*(((floatPixel[(y*newwidth) + (x+2)] +
floatPixel[((y+4)*newwidth + (x+2))]) +
(floatPixel[((y+2)*newwidth) + x] +
floatPixel[((y+2)*(newwidth) + (x+4))]))))+
(4*floatPixel[((y+2)*(newwidth) + (x+2))]) ) ; //fill Green
/*B at red in R row, R column*/
val[2] = (0.125)*((2*((floatPixel[((y+1)*newwidth) + (x+1)] +
floatPixel[((y+1)*(newwidth) + (x+3))]) +
(floatPixel[((y+3)*newwidth) + (x+1)] +
floatPixel[((y+3)*(newwidth) + (x+3))]))) -
((1.5)*(floatPixel[(y*newwidth) + (x+2)] +
floatPixel[((y+4)*newwidth + (x+2))] +
floatPixel[((y+2)*newwidth) + x] +
floatPixel[((y+2)*(newwidth) + (x+4))])) +
(6*floatPixel[((y+2)*(newwidth) + (x+2))])) ; //fill blue*/
}
/*odd pixel is G*/
else
{
/*R at green in R row, B column*/
val[0] = (0.125)* ((4*(floatPixel[((y+2)*newwidth) + (x+1)] +
floatPixel[((y+2)*(newwidth) + (x+3))])) -
1*((floatPixel[(y+2)*(newwidth) + (x)])+ (floatPixel[(y+2)*(newwidth) +(x+4)]) +
(floatPixel[(y+1)*newwidth + (x+1)]) + (floatPixel[(y+3)*newwidth + (x+1)]) +
(floatPixel[(y+1)*newwidth + (x+3)]) + (floatPixel[(y+3)*newwidth + (x+3)])) +
(0.5)*(floatPixel[(y*newwidth) + (x+2)] + floatPixel[((y+4)*newwidth) + (x+2)]) +
5*floatPixel[((y+2)*newwidth) + (x+2)]); //fill red
val[1] = (float)data[(y*width) + x]/(float)255; //Green
/*Blue at green in R row, B column*/
val[2] = (0.125)* ((4*(floatPixel[((y+1)*newwidth) + (x+2)] +
floatPixel[((y+3)*(newwidth) + (x+2))])) +
((0.5)*((floatPixel[(y+2)*(newwidth) + (x)])+ (floatPixel[(y+2)*(newwidth) +(x+4)]))) -
1*((floatPixel[(y+1)*newwidth + (x+1)]) + (floatPixel[(y+3)*newwidth + (x+1)]) +
(floatPixel[(y+1)*newwidth + (x+3)]) + (floatPixel[(y+3)*newwidth + (x+3)])) -
((1)*(floatPixel[(y*newwidth) + (x+2)] + floatPixel[((y+4)*newwidth) + (x+2)])) +
(5*floatPixel[((y+2)*newwidth) + (x+2)])); //fill blue*/
}
}
/*odd row B G B G B G pattern*/
else
{ /* Green pixel*/
if ((x % 2) == 0)
{
/*R at green in B row, R column*/
val[0] = (0.125)* ((4*(floatPixel[((y+1)*newwidth) + (x+2)] +
floatPixel[((y+3)*(newwidth) + (x+2))])) +
(0.5)*((floatPixel[(y+2)*(newwidth) + x])+ (floatPixel[(y+2)*(newwidth) +(x+4)])) -
1*((floatPixel[(y+1)*newwidth + (x+1)]) + (floatPixel[(y+3)*newwidth + (x+1)]) +
(floatPixel[(y+1)*newwidth + (x+3)]) + (floatPixel[(y+3)*newwidth + (x+3)])) -
1*(floatPixel[(y*newwidth) + (x+2)] + floatPixel[((y+4)*newwidth) + (x+2)]) +
(5*floatPixel[((y+2)*newwidth) + (x+2)])); //fill red
val[1] = (float)data[(y*width) + x]/(float)255; //Green
/*B at green in B row, R column*/
val[2] = (0.125)* ((4*(floatPixel[((y+2)*newwidth) + (x+1)] +
floatPixel[((y+2)*(newwidth) + (x+3))])) -
1*((floatPixel[(y+2)*(newwidth) + (x)])+ (floatPixel[(y+2)*(newwidth) +(x+4)]) +
(floatPixel[(y+1)*newwidth + (x+1)]) + (floatPixel[(y+3)*newwidth + (x+1)]) +
(floatPixel[(y+1)*newwidth + (x+3)]) + (floatPixel[(y+3)*newwidth + (x+3)])) +
((0.5)*(floatPixel[(y*newwidth) + (x+2)] + floatPixel[((y+4)*newwidth) + (x+2)])) +
(5*floatPixel[((y+2)*newwidth) + (x+2)])); //fill blue*/
}
/*Blue pixel*/
else
{
/*R at blue in B row, B column*/
val[0] = (0.125)*((2*((floatPixel[((y+1)*newwidth) + (x+1)] +
floatPixel[((y+1)*(newwidth) + (x+3))]) +
(floatPixel[((y+3)*newwidth) + (x+1)] +
floatPixel[((y+3)*(newwidth) + (x+3))]))) -
((1.5)*(((floatPixel[(y*newwidth) + (x+2)] +
floatPixel[((y+4)*newwidth + (x+2))]) +
(floatPixel[((y+2)*newwidth) + x] +
floatPixel[((y+2)*(newwidth) + (x+4))])))) +
(6*floatPixel[((y+2)*(newwidth) + (x+2))])) ; //fill Red
/*G at B locations*/
val[1] = (0.125)*((2*((floatPixel[((y+1)*newwidth) + (x+2)] +
floatPixel[((y+3)*newwidth + (x+2))]) +
(floatPixel[((y+2)*newwidth) + (x+1)] +
floatPixel[((y+2)*(newwidth) + (x+3))]))) -
(1*(((floatPixel[(y*newwidth) + (x+2)] +
floatPixel[((y+4)*newwidth + (x+2))]) +
(floatPixel[((y+2)*newwidth) + x] +
floatPixel[((y+2)*(newwidth) + (x+4))]))))+
(4*floatPixel[((y+2)*(newwidth) + (x+2))]) ) ; //fill Green
/*Blue pixel*/
val[2] = (float)data[(y*width) + x]/(float)255; //Blue
}
}
for (int iVal= 0; iVal<3; iVal++)
{
if (val[iVal] < 0)
val[iVal] = 0;
else if(val[iVal] > 1)
val[iVal] = 1;
}
temp[pixel] = val[0]*255;
temp[pixel+1] = val[1]*255;
temp[pixel+2] = val[2]*255;
} //end for loop width
} // end for loop height
data = new unsigned char[width*height*3];
for (int i =0; i < height*width; i++)
{
data[3*i] = temp[3*i];
data[3*i+1] = temp[3*i+1];
data[3*i+2] = temp[3*i+2];
}
delete temp;
delete newdata;
return true;
}
int main(int argc, char* argv[])
{
int BytesPerPixel = 1;
int Width = 424;
int Height = 636;
int outWidth, outHeight;
if (argc < 5)
{
cout << "Error - Incorrect Parameter Usage:" << endl;
cout << "program_name input_image.raw output_image.raw BytesPerPixel width height" << endl;
return 0;
}
BytesPerPixel = atoi(argv[3]);
inImage.setNumbytes(BytesPerPixel);
// Check if size is specified
if (argc >= 5){
Width = atoi(argv[4]);
Height = atoi(argv[5]);
inImage.setWidth(Width);
inImage.setHeight(Height);
}
//Read Image
if (!inImage.readImage(argv[1]))
{
cout <<"Could not Read Image"<< endl;
}
else
{
cout << "MHP Demosaicing" <<endl;
outImage = inImage;
if (!outImage.demosaic_MHP())
{
cout << "Error:Could not demosaic Image"<<endl;
}
}
outImage.setNumbytes(3); //RGB
//write final image to file
if (!outImage.writeImage(argv[2]))
{
cout <<"Could not Write Image"<< endl;
}
return 0;
}