-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code
714 lines (558 loc) · 20.2 KB
/
Code
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
#include <opencv2\opencv.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
using namespace cv;
using namespace std;
static Mat img;
static Point origin;
static Rect selection;
static bool selectionObject = false;
static bool objectCropped = false;
static Mat imgClone;
class image_processing {
public:
Mat image;
//Input image
void input_image() {
int myimg;
string path;
cout << "\n================ Pictures ===================\n\n";
cout << "\t | (1) Nature" << endl << endl;
cout << "\t | (2) gold" << endl << endl;
cout << "\t | (2) gold" << endl << endl;
cin >> myimg;
if (!cin) {
cout << "\nWrong choice! Enter again....\n " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
//continue;
}
switch (myimg) {
case 1:
image = imread("nature.jpg");
break;
case 2:
image = imread("gold.jpg");
break;
case 3:
break;
default:
break;
}
cout << "\n\nloading";
for (int i = 0; i < 4; i++) {
cout << " .";
Sleep(3);
}
cout << "\n\nImage readed successfully..........";
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//Output image
int output_image() {
if (image.empty()) {
cout << "Image file not found....." << endl;
cin.get();
return -1;
}
imshow("my image",image);
waitKey(0);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//case1: Image shear
void forward_shear() {
Mat image2(image.rows, image.cols + image.rows + 1, image.type());
int constant = image.rows;
for (int x = 0; x < image2.rows; x++) {
Vec3b* pixrow = image.ptr<Vec3b>(x);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x);
for (int j = 0; j < image.cols; j++) {
pixsolrow1[j + constant] = pixrow[j];
}
constant--;
}
imshow("shear image", image2);
waitKey(0);
}
void backward_shear() {
Mat image2(image.rows, image.cols + image.rows + 1, image.type());
int constant = 0;
for (int x = 0; x < image2.rows; x++) {
Vec3b* pixrow = image.ptr<Vec3b>(x);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x);
for (int j = 0; j < image.cols; j++) {
pixsolrow1[j + constant] = pixrow[j];
}
constant++;
}
imshow("Backward shear image", image2);
waitKey(0);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Case2: Rotate image
void rotate(Mat img, float b) {
double angle = b;
// // get the center coordinates of the image to create the 2D rotation matrix
Point2f center((img.cols - 1) / 2.0, (img.rows - 1) / 2.0);
// // using getRotationMatrix2D() to get the rotation matrix
Mat rotation_matix = getRotationMatrix2D(center, angle, 1.0);
// // we will save the resulting image in rotated_image matrix
Mat rotated_image;
// rotate the image using warpAffine
warpAffine(img, rotated_image, rotation_matix, img.size());
imshow("Rotated image", rotated_image);
// wait indefinitely, press any key on keyboard to exit
waitKey(0);
// save the rotated image to disk
imwrite("rotated_im.jpg", rotated_image);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Case3 Image Blur
void ImageBlurring() {
Mat image2 = image.clone();
for (int x = 0; x < image.rows - 2; x++) {
Vec3b* pixrow = image.ptr<Vec3b>(x);
Vec3b* pixrow1 = image.ptr<Vec3b>(x + 1);
Vec3b* pixrow2 = image.ptr<Vec3b>(x + 2);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x + 1);
for (int y = 0; y < image.cols - 2; y++) {
pixsolrow1[y + 1] = pixrow[y + 2] * (double)1 / 9 + pixrow[y + 1] * (double)1 / 9 + pixrow[y] * (double)1 / 9 +
pixrow1[y + 2] * (double)1 / 9 + pixrow1[y + 1] * (double)1 / 9 + pixrow1[y] * (double)1 / 9 +
pixrow[y + 2] * (double)1 / 9 + pixrow[y + 1] * (double)1 / 9 + pixrow[y] * (double)1 / 9;
}
}
imshow("brighter", image2);
waitKey(0);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//case4: Image Enhacement
void bright() {
Mat image2;
image2.create(image.rows, image.cols, image.type());
// // | 0,-1, 0 |
// //Kernel = |-1, 5,-1 |
// // | 0,-1, 0 |
for (int i = 1; i < image.rows - 2; i++) {
Vec3b* K_row1 = image.ptr<Vec3b>(i);
Vec3b* K_row2 = image.ptr<Vec3b>(i + 1);
Vec3b* K_row3 = image.ptr<Vec3b>(i + 2);
Vec3b* output = image2.ptr<Vec3b>(i + 1);
for (int j = 1; j < image.cols - 2; j++) {
output[j] = (K_row1[j] * 0 + K_row1[j + 1] * -1 / 5 + K_row1[j + 2] * 0) + //1st row till 3 columns
(K_row2[j] * -1 / 5 + K_row2[j + 1] * 5 / 5 + K_row2[j + 2] * -1 / 5) + //2nd row till 3 columns
(K_row3[j] * 0 + K_row3[j + 1] * -1 / 2 + K_row3[j + 2] * 0); //3rd row till 3 columns
}
}
image2 = image2 + image;
imshow("brighter", image2);
waitKey(0);
}
//
////xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void sharpen2D() {
Mat image2;
image2.create(image.rows, image.cols, image.type());
Mat kernel = (Mat_ < double >(3, 3) << 0, -1, 0,
-1, 5, -1,
0, -1, 0);
//filter the image
filter2D(image, image2, -1, kernel);
imshow("sharpen", image2);
}
////xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
////case5: Image Reflection
void Reflect() {
int col = image.cols-1;
int row = image.rows - 1;
Mat Reflected_image =image.clone();
for (int i = 0; i < image.rows; i++) {
Vec3b* pixel_row = image.ptr <Vec3b>(i);
Vec3b* Reflect_pixels = Reflected_image.ptr<Vec3b>(i);
for (int j = 0; j < image.cols; j++)
Reflect_pixels[j] = pixel_row[col - j];
}
imshow("invert", Reflected_image);
waitKey(0);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//case6: Image Denoising
void Salt_and_Pepper_removal() {
Mat image2 = image.clone();
medianBlur(image, image2, 3);
imshow("salt", image2);
waitKey(0);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
static void onMouse(int event, int x, int y, int flags, void*) {
if (selectionObject) {
selection.x = min(x, origin.x);
selection.y = min(y, origin.y);
selection.width = abs(x - origin.x);
selection.height = abs(y - origin.y);
selection &= Rect(0, 0, img.cols, img.rows);
}
switch (event) {
case EVENT_LBUTTONDOWN:
origin = Point(x, y);
selection = Rect(x, y, 0, 0);
selectionObject = true;
break;
case EVENT_LBUTTONUP:
selectionObject = false;
if (selection.width > 0 && selection.height > 0)
objectCropped = true;
default:
break;
}
img.copyTo(imgClone);
//Draw a Rectangle while cropping image
rectangle(imgClone, selection, Scalar(0, 0, 225), 1, 8, 0);
imshow("image", imgClone);
// selectionObject = false;
}
void crop() {
img = image.clone();
namedWindow("image", WINDOW_AUTOSIZE);
setMouseCallback("image", onMouse);
imshow("image", img);
int d = 1;
while (d == 1) {
if (objectCropped) {
Mat crop = img(selection);
namedWindow("crop", WINDOW_AUTOSIZE);
imshow("crop", crop);
d = 0;
}
waitKey(10);
}
waitKey(0);
}
/// //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// image Blurring [ 3x3 kernel ]
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
// Binary Image 1 using [3x3 kernel]
void SobelOperatorx(Mat& image2) {
for (int x = 0; x < image.rows - 2; x++) {
Vec3b* pixrow = image.ptr<Vec3b>(x);
Vec3b* pixrow1 = image.ptr<Vec3b>(x + 1);
Vec3b* pixrow2 = image.ptr<Vec3b>(x + 2);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x + 1);
for (int y = 0; y < image.cols - 2; y++) {
pixsolrow1[y + 1] = pixrow[y]*(double)1 / 4 + pixrow[y + 1]*(double)0 - pixrow[y + 2] * (double)1 / 4 +
pixrow1[y]*(double)1 / 2 + pixrow1[y + 1]*(double)0 - pixrow1[y + 2] * (double)1 / 2 +
pixrow2[y]*(double)1 / 4 + pixrow2[y + 1]*(double)0 - pixrow2[y + 2] * (double)1 / 4;
}
}
image2 *= 2;
}
void SobelOperatory( Mat& image2) {
for (int x = 0; x < image.rows - 2; x++) {
Vec3b* pixrow = image.ptr<Vec3b>(x);
Vec3b* pixrow1 = image.ptr<Vec3b>(x + 1);
Vec3b* pixrow2 = image.ptr<Vec3b>(x + 2);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x + 1);
for (int y = 0; y < image.cols - 2; y++) {
pixsolrow1[y + 1] = pixrow[y]*(double)1 / 4 + pixrow[y + 1]*(double)1 / 2 + pixrow[y + 2] * (double)1 / 4 +
pixrow1[y]*(double)0 + pixrow1[y + 1]*(double)0 + pixrow1[y + 2] * (double)0 -
pixrow2[y]*(double)1 / 4 - pixrow2[y + 1]*(double)1 / 2 - pixrow2[y + 2] * (double)1 / 4;
}
}
image2 *= 2;
}
//
// // Binary Image 2 with [ 3x3 kernel ]
//
// void BinaryImageLite(Mat& image, Mat& image2) {
// for (int x = 0; x < image.rows - 2; x++) {
//
// Vec3b* pixrow = image.ptr<Vec3b>(x);
// Vec3b* pixrow1 = image.ptr<Vec3b>(x + 1);
// Vec3b* pixrow2 = image.ptr<Vec3b>(x + 2);
//
//
// Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x + 1);
//
// // Binary Image 2
//
// for (int y = 0; y < image.cols - 2; y++) {
// pixsolrow1[y + 1] = pixrow[y] * (double)1 / 4 + pixrow[y + 1] * (double)1 / 4 + pixrow[y + 2] * (double)1 / 4 +
// pixrow1[y] * (double)0 + pixrow1[y + 1] * (double)0 - pixrow1[y + 2] * (double)0 -
// pixrow2[y] * (double)1 / 4 - pixrow2[y + 1] * (double)1 / 4 - pixrow2[y + 2] * (double)1 / 4;
// }
//
// }
//
// }
//
// //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//
void rotations() {
Mat image2=image.clone();
cv::transpose(image,image2);
imshow("leftrotate",image2);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxx Filters xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//
void BlackBoard() {
Mat image2 = image.clone();
Mat image3 = image.clone();
Mat image4 = image.clone();
SobelOperatorx(image2);
SobelOperatory(image3);
multiply(image2, image2, image4); // sqr img2
multiply(image3, image3, image2); // sqr img3
add(image2, image, image3);
abs(image3);
sqrt(image3, image2);
cvtColor(image2, image3, COLOR_BGR2GRAY);
image2 = image3;
image2 = image2 / 1.25;
imshow("Black Board", image2);
waitKey(0);
}
//
//
// // Wall Paint [ 3x3 kernel ]
void WallPaint() {
Mat image2 = image.clone();
for (int x = 0; x < image.rows - 2; x++) {
Vec3b* pixrow = image2.ptr<Vec3b>(x);
Vec3b* pixrow1 = image2.ptr<Vec3b>(x + 1);
Vec3b* pixrow2 = image2.ptr<Vec3b>(x + 2);
Vec3b* pixsolrow1 = image2.ptr<Vec3b>(x + 1);
for (int y = 0; y < image.cols - 2; y++) {
pixsolrow1[y + 1] = -pixrow[y] * 1 + pixrow[y + 1] * 0 + pixrow[y + 2] * 1 -
pixrow1[y] * 1 + pixrow1[y + 1] * 0 + pixrow1[y + 2] * 1 -
pixrow2[y] * 1 + pixrow2[y + 1] * 0 + pixrow2[y + 2] * 1;
}
}
imshow("Wall paint", image2);
cv::waitKey(0);
}
//
void Autumn() {
Mat image2 = image.clone();
Mat image3 = image.clone();
// | 0,-1, 0 |
//Kernel = |-1, 5,-1 |
// | 0,-1, 0 |
// GaussianBlur(image2, image3,Size(5, 5) , 1);
for (int i = 0; i < image.rows - 2; i++) {
Vec3b* K_row1 = image2.ptr<Vec3b>(i);
Vec3b* K_row2 = image2.ptr<Vec3b>(i + 1);
Vec3b* K_row3 = image2.ptr<Vec3b>(i + 2);
Vec3b* output = image3.ptr<Vec3b>(i + 1);
for (int j = 0; j < image.cols; j++) {
output[j + 1] = (K_row1[j] * 0 + K_row1[j + 1] * -0.05 + K_row1[j + 2] * 0) + //1st row till 3 columns
(K_row2[j] * -0.05 + K_row2[j + 1] * 0.7 + K_row2[j + 2] * -0.05) + //2nd row till 3 columns
(K_row3[j] * 0 + K_row3[j + 1] * -0.05 + K_row3[j + 2] * 0); //3rd row till 3 columns
}
}
image3*= 2.25;
imshow("Autumn", image3);
cv::waitKey(0);
}
//
//
//
};
// Driver code
int main(int argc, char** argv)
{
Mat back;
image_processing obj;
Mat image2 = obj.image.clone();
int choice,choice1;
bool breaker = true;
bool loop_break = true;
while (loop_break) {
cout << "======== Functions Of Program ========\n " << endl;
cout << "\t | (1) Input an Image" << endl << endl;
cout << "\t | (2) Display original Image" << endl << endl;
cout << "\t | (3) Image Shear" << endl << endl;
cout << "\t | (4) Image Rotation" << endl << endl;
cout << "\t | (5) Image Blurring" << endl << endl;
cout << "\t | (6) Image Enhacement" << endl << endl;
cout << "\t | (7) Image Reflection" << endl << endl;
cout << "\t | (8) Image Denoising" << endl << endl;
cout << "\t | (9) Image Cropping" << endl << endl;
cout << "\t | (10) Image Filtering" << endl << endl;
cout << "\t | (11) Exit";
cout << "\n\nEnter Your Choice : ";
cin >> choice;
if (!cin) {
cout << "\nWrong choice! Enter again..... \n" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
system("pause");
system("cls");
continue;
}
switch (choice)
{
//Input
case 1:
obj.input_image();
break;
//Output
case 2:
obj.output_image();
break;
// //shearing
case 3: {
breaker = true;
while (breaker) {
system("cls");
cout << "======== Functionalities Of Program ========\n " << endl;
cout << "\t 1) Forward Shear\n\n\t 2) Backward Shear\n\n\t 3) Back" << endl;
cout << "\n\nEnter Your Choice : ";
cin >> choice1;
if (!cin) {
cout << "\nWrong choice! Enter again....\n " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
system("pause");
system("cls");
continue;
}
switch (choice1)
{
case 1:
obj.forward_shear();
break;
case 2:
obj.backward_shear();
break;
case 3:
breaker = false;
break;
default:
break;
}
cout << endl << endl;
system("pause");
system("cls");
}
}
break;
//rotation
case 4:
float degree;
cout << "Enter the Degree of Rotation you want: ";
cin >> degree;
obj.rotate(obj.image, degree);
break;
// //Blurring
case 5:
obj.ImageBlurring();
break;
// //Enhancement
case 6:
breaker = true;
while (breaker) {
system("cls");
cout << "======== Functionalities Of Program ========\n " << endl;
cout << "\t 1) Sharpness\n\n\t 2) Brightness\n\n\t 3) Back" << endl;
cout << "\n\nEnter Your Choice : ";
cin >> choice1;
if (!cin) {
cout << "\nWrong choice! Enter again.....\n" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
system("pause");
system("cls");
continue;
}
switch (choice1)
{
case 1:
obj.sharpen2D();
break;
case 2:
obj.bright();
break;
case 3:
breaker = false;
break;
default:
break;
}
cout << endl << endl;
system("pause");
system("cls");
}
break;
//Reflection
case 7:
obj.Reflect();
break;
// //Denoising
case 8:
obj.Salt_and_Pepper_removal();
break;
//Negative
case 9:
obj.crop();
//obj.negative();
break;
case 10:
breaker = true;
while (breaker) {
system("cls");
cout << "======== Image Filtering ========\n " << endl;
cout << "\t 1) Autumn\n\n\t 2) Black Board\n\n\t 2) Wall paint\n\n\t 4) Back" << endl;
cout << "\n\nEnter Your Choice : ";
cin >> choice1;
if (!cin) {
cout << "\nWrong choice! Enter again....\n " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
system("pause");
system("cls");
continue;
}
switch (choice1)
{
case 1:
obj.Autumn();
break;
case 2:
break;
case 3:
obj.WallPaint();
break;
case 4:
breaker = false;
break;
default:
break;
}
cout << endl << endl;
system("pause");
system("cls");
}
break;
//Exit
case 11:
loop_break = false;
break;
default:
break;
//waitKey(0);
}
cout << endl << endl;
system("pause");
system("cls");
}
return 0;
}