-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2_oilpainting.cpp
More file actions
380 lines (341 loc) · 11.6 KB
/
Copy pathProblem2_oilpainting.cpp
File metadata and controls
380 lines (341 loc) · 11.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**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;
/*Code to implement oil painting with 64 colors per pixel*/
bool Image::oil_painting()
{
double hist_R[256] = {0} ,hist_G[256] = {0},hist_B[256] = {0};
double cd_histR[256] = {0} ,cd_histG[256] = {0},cd_histB[256] = {0};
int r = 1;
int g = 1;
int b = 1;
int binR[5] = {0};
int binG[5] = {0};
int binB[5] = {0};
unsigned int cumR =0;
unsigned int cumG =0;
unsigned int cumB =0;
/*Count number of pixels at each intensity location*/
for (int i = 0; i <height*width; i++)
{
hist_R[data[3*i]]++;
hist_G[data[3*i+1]]++;
hist_B[data[3*i+2]]++;
}
for (int i = 0; i < 256; i++)
{
cumR = cumR + hist_R[i];
cumG = cumG + hist_G[i];
cumB = cumB + hist_B[i];
cd_histR[i] = cumR;
cd_histG[i] = cumG;
cd_histB[i] = cumB;
/*Calculate threshold values- Choose threshold(x axis) where 25%, 50%, 75% 100%
pixels lie(y axis)*/
//Red
if (cd_histR[i] > (0.25*r*height*width))
{
//store y axis intensity value as threshold
binR[r] = i-1;
r = r+1;
}
else if (cd_histR[i] == (0.25*r*height*width))
{
//store y axis intensity value as threshold
binR[r] = i;
r = r+1;
}
//Green
if (cd_histG[i] > (0.25*g*height*width))
{
//store y axis intensity value as threshold
binG[g] = i-1;
g = g+1;
}
else if (cd_histG[i] == (0.25*g*height*width))
{
//store y axis intensity value as threshold
binG[g] = i;
g = g+1;
}
//Blue
if (cd_histB[i] > (0.25*b*height*width))
{
//store y axis intensity value as threshold
binB[b] = i-1;
b = b+1;
}
else if (cd_histB[i] == (0.25*b*height*width))
{
//store y axis intensity value as threshold
binB[b] = i;
b = b+1;
}
}
/*Calculating mean of each bin mean= (freq*value)/freq*/
int meanR[4]= {0}, meanG[4]={0}, meanB[4]={0};
for(int j =1; j<5; j++)
{
int freqR=0, freqG=0, freqB=0;
for (int k = binR[j-1]; k <= binR[j]; k++)
{
meanR[j-1] = meanR[j-1] + (hist_R[k+1]*(k+1));
freqR= freqR + hist_R[k+1];
}
meanR[j-1] = (float)meanR[j-1]/(float)freqR;
for (int k = binG[j-1]; k <= binG[j]; k++)
{
meanG[j-1] = meanG[j-1] + (hist_G[k+1]*(k+1));
freqG= freqG + hist_G[k+1];
}
meanG[j-1] = (float)meanG[j-1]/(float)freqG;
for (int k = binB[j-1]; k <= binB[j]; k++)
{
meanB[j-1] = meanB[j-1] + (hist_B[k+1]*(k+1));
freqB= freqB + hist_B[k+1];
}
meanB[j-1] = (float)meanB[j-1]/(float)freqB;
}
/*Check each pixel for the bin in which they fall
replace pixel with bin value*/
for (int i = 0; i < height*width; i++)
{
for (int l=0; l<4; l++)
{
if (data[3*i] >= binR[l] && data[3*i] <= binR[l+1])
{
//data[3*i] = (float)(binR[l]+binR[l+1])/(float)2;
data[3*i] = meanR[l];
break;
}
}
for (int l=0; l<4; l++)
{
if (data[3*i +1] >= binG[l] && data[3*i +1] <= binG[l+1])
{
//data[3*i+1] = (float)(binG[l]+binG[l+1])/(float)2;
data[3*i +1] = meanG[l];
break;
}
}
for (int l=0; l<4; l++)
{
if (data[3*i+2] >= binB[l] && data[3*i +2] <= binB[l+1])
{
//data[3*i+1] = (float)(binB[l]+binB[l+1])/(float)2;
data[3*i+2] = meanB[l];
break;
}
}
}
int N;
cout << "Enter the neighbourhood value N"<<endl;
cin >> N;
if (N < 0 || N >width || N >height || (N%2 ==0))
{
cout << "Error: Enter correct value of N--odd, postive";
return false;
}
/*Extend image by one row/column pixel from all sides*/
int extension = floor(N/2); //number of rows/columns to extend
int newwidth = width+(2*extension);
int newheight =height+(2*extension);
unsigned char* newdata;
newdata = new unsigned char[newwidth*newheight*3]; //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++)
{
for(int j=0; j < width; j++)
{
newdata[3*(((i+extension)*newwidth) +(j+extension))] = data[3*((i*width) + j)]; //copy original R pixel value
newdata[3*(((i+extension)*newwidth) +(j+extension)) +1] = data[3*((i*width) + j) +1]; //copy original G pixel value
newdata[3*(((i+extension)*newwidth) +(j+extension)) +2] = data[3*((i*width) + j) +2]; //copy original B pixel value
}
for (int c = 0; c <extension; c++)
{
newdata[3*((i+extension)*newwidth + c)] = data[3*((i*width) + c)]; //cth column R reflection
newdata[3*((i+extension)*newwidth + c)+1] = data[3*((i*width) + c)+1]; //cth column G reflection
newdata[3*((i+extension)*newwidth + c)+2] = data[3*((i*width) + c)+2]; //cth column B reflection
newdata[3*((i+(extension+1))*newwidth - (c+1))] = data[3*(((i+1)*width)-(c+1))]; //last cth R column reflection
newdata[3*((i+(extension+1))*newwidth - (c+1)) +1] = data[3*(((i+1)*width)-(c+1))+1]; //last G cth column reflection
newdata[3*((i+(extension+1))*newwidth - (c+1))+2] = data[3*(((i+1)*width)-(c+1))+2]; //last B cth column reflection
}
}
/*copy input image to newdata - reflection of pixels -row wise*/
for (int a=0; a < newwidth; a++)
{
for (int r = 0; r< extension; r++)
{
newdata[3*(r*newwidth + a)] = newdata[3*((extension+r)*newwidth +a)]; //copy rth row
newdata[3*(r*newwidth + a)+1] = newdata[3*((extension+r)*newwidth +a)+1]; //copy rth row
newdata[3*(r*newwidth + a)+2] = newdata[3*((extension+r)*newwidth +a)+2]; //copy rth row
newdata[3*((newwidth*(newheight-(r+1))) + a)] = newdata[3*((newheight-(extension+r+1))*newwidth +a)]; //copy last rth row
newdata[3*((newwidth*(newheight-(r+1))) + a) +1] = newdata[3*((newheight-(extension+r+1))*newwidth +a)+1]; //copy last rth row
newdata[3*((newwidth*(newheight-(r+1))) + a)+2] = newdata[3*((newheight-(extension+r+1))*newwidth +a)+2]; //copy last rth row
}
}
/*Oil painting Effect*/
unsigned char* modidata;
modidata = new unsigned char[newwidth*newheight*3]; //to store expanded input image with first two rows/column reflected
/*Store the RGB value together in one pixel 8*3=24bit
first 8 bits are R , second 8 bits G, last 8 bits as blue*/
int* combImage;
combImage = new int[newwidth*newheight]; //to store RGB value combined into one pixel
for (int i =0; i < newheight; i++)
{
for (int j =0; j < newwidth; j++)
{
int pixVal = (i*(newwidth*3)) + (j*3);
combImage[(i*newwidth) +j] = (int)newdata[pixVal+2];
combImage[(i*newwidth) +j] = combImage[(i*newwidth) +j] <<8;
combImage[(i*newwidth) +j] += (int)newdata[pixVal+1];
combImage[(i*newwidth) +j] = combImage[(i*newwidth) +j] <<8;
combImage[(i*newwidth) +j] += (int)newdata[pixVal];
}
}
for (int i = extension; i < height+extension; i++)
{
for (int j = extension; j < width+extension; j++)
{
int pixelcount[N][N];
int c=0, d=0, m=0, n=0;
int blkrow, blkcol;
for (int m = 0; m <N; m++)
{
for (int n =0; n < N; n++)
{
pixelcount[m][n] = 0;
}
}
for (blkrow = i-extension; blkrow <= i+extension; blkrow++)
{
for (blkcol = j-extension; blkcol <= j+extension; blkcol++)
{
int blkpixel = (blkrow*newwidth) + blkcol;
for (m = 0; m <N; m++)
{
for (n =0; n < N; n++)
{
int X = i-extension + m;
int Y = j-extension + n;
int combpix = X*newwidth +Y;
if (combImage[blkpixel] == combImage[combpix])
pixelcount[c][d]++; //0-8
}
}
d++;
} //end for loop blk width
c++;
d=0;
} //end for loop blk height
/*Find bin which has maximum number of pixels*/
int max = pixelcount[0][0];
int indexX = 0;
int indexY = 0;
for (int iY=0; iY <N; iY++)
{
for (int iX=0; iX <N; iX++)
{
if (pixelcount[iY][iX] > max)
{
max = pixelcount[iY][iX];
indexY = iY; //stores value that occurs maximum in N*N neighbourhood
indexX = iX;
}
}
}
int parseY = blkrow + indexY-N;
int parseX = blkcol + indexX-N;
int parsePix = parseY*newwidth*3 + (3*parseX);
/*Fill the final color*/
modidata[(3*i*newwidth) + 3*j] = newdata[parsePix];
modidata[(3*i*newwidth) +(3*j) +1] = newdata[parsePix + 1];
modidata[(3*i*newwidth)+ (3*j) +2] = newdata[parsePix +2];
} //end for loop newwidth
} //end for loop new height
for (int i = extension; i < height+extension; i++)
{
for (int j = extension; j < width+extension; j++)
{
int pixel = 3*(i-extension)*width + 3*(j-extension);
int pixelres = 3*i*newwidth +(3*j);
data[pixel] = modidata[pixelres];
data[pixel+1] = modidata[pixelres+1];
data[pixel+2] = modidata[pixelres+2];
}
}
delete newdata;
delete modidata;
delete combImage;
return true;
}
int main(int argc, char* argv[])
{
int BytesPerPixel = 1;
int Width = 424;
int Height = 636;
int outWidth, outHeight;
int ExtensionVal;
if (argc < 5)
{
cout << "Error - Incorrect Parameter Usage:" << endl;
cout << "program_name input_image.raw output_image.raw BytesPerPixel width height" << endl;
return 0;
}
else
{
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 << "Oil painting- problem 2" <<endl;
outImage = inImage;
#if 0
/*Quantize the image with 64 colors*/
if (!outImage.Quantization())
{
cout << "Error:Could not Quantize Image"<<endl;
}
/*Image extension*/
cout << "Enter the image extension value" <<endl;
cin >> ExtensionVal;
if (!outImage.image_extension(ExtensionVal))
{
cout << "Error:Could not Extend Image"<<endl;
}
#endif
if (!outImage.oil_painting())
{
cout << "Error:Could not Quantize Image"<<endl;
}
}
// outImage.setWidth(Width+4);
//outImage.setHeight(Height+4);
//outImage.setNumbytes(3); //RGB
//write final image to file
if (!outImage.writeImage(argv[2]))
{
cout <<"Could not Write Image"<< endl;
}
return 0;
}