-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2215 lines (1953 loc) · 67.8 KB
/
main.cpp
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <set>
#include <list>
#include <map>
#include <typeinfo>
#include <fstream>
using cv::Mat;
using cv::samples::findFile;
using cv::IMREAD_COLOR;
using cv::waitKey;
using std::string;
using std::cout;
using std::cin;
using std::istream;
using std::ostream;
using std::endl;
// block of code to disable opencv warnings
int dummy_error_handler(int status, char const *func_name, char const *err_msg, char const *file_name, int line,
void *userdata) {
//Do nothing -- will suppress console output
return 0; //Return value is not used
}
// sets warning display off
void set_dummy_error_handler() {
cv::redirectError(dummy_error_handler);
}
// sets warning display on
void reset_error_handler() {
cv::redirectError(nullptr);
}
void initOpenCV() {
// for stopping logging info in console
cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT);
// for stopping warnings in console
set_dummy_error_handler();
}
class Interface {
public:
virtual void applyAll() = 0;
virtual void write() const = 0;
virtual istream &read(istream &in) = 0;
virtual ostream &print(ostream &out) const = 0;
virtual void serialize(string) const = 0;
virtual void deserialize(std::ifstream&) = 0;
};
class Image : public Interface {
protected:
// boolean for image finding
bool absolute;
string name, path;
Mat img;
public:
Image(string name = "cat.png", string path = "../Images/", bool absolute = false);
Image(const Image &obj);
Image &operator=(const Image &obj);
// virtual to call derivative destructors
virtual ~Image();
istream &read(istream &in);
ostream &print(ostream &out) const;
friend istream &operator>>(istream &in, Image &obj);
friend ostream &operator<<(ostream &out, const Image &obj);
string extension(string word) const;
string withoutExtension(string word) const;
void scan();
void show() const;
void show(const Mat &img) const;
void write() const;
void saveShow() const;
void applyAll();
string getName() const;
string getPath() const;
bool operator<(const Image& obj) const {
return !(this->name > obj.name);
}
bool operator==(const Image& obj) const {
return this->name == obj.name;
}
virtual void serialize(string) const;
virtual void deserialize(std::ifstream&);
};
void Image::serialize(string fileName) const {
std::ofstream out(fileName, std::ios_base::app);
out<<name<<" "<<path<<" "<<absolute<<" ";
out.close();
}
void Image::deserialize(std::ifstream& in) {
string name,path;
bool absolute;
in>>name>>path>>absolute;
this->name = name;
this->path = path;
this->absolute = absolute;
this->scan();
}
Image::Image(string name, string path, bool absolute) {
this->name = name;
this->path = path;
this->absolute = absolute;
}
Image::Image(const Image &obj) {
this->name = obj.name;
this->path = obj.path;
this->absolute = obj.absolute;
}
Image &Image::operator=(const Image &obj) {
if (this != &obj) {
if (!this->name.empty()) this->name.clear();
this->name = obj.name;
if (!this->path.empty()) this->path.clear();
this->path = obj.path;
this->absolute = obj.absolute;
}
return *this;
}
istream &Image::read(istream &in) {
if (!this->name.empty()) this->name.clear();
if (!this->path.empty()) this->path.clear();
cout << "Enter name: \n";
in >> this->name;
cout << "Do you want to use relative path? (1:yes 0:no)?\n";
int temp;
cin >> temp;
in.get();
if (temp == 0) {
this->absolute = true;
cout << "Enter path to image: \n";
// to get \n from buffer
getline(in, this->path);
// deleting "" from path
if (this->path[0] == '"') this->path.erase(0, 1), this->path.pop_back();
} else this->path = "../Images/";
return in;
}
ostream &Image::print(ostream &out) const {
out << "Name: " << this->name << endl;
if (this->absolute == false) out << "Path to image: " << this->path + this->name << endl;
else out << "Path to image: " << this->path << endl;
return out;
}
istream &operator>>(istream &in, Image &obj) {
return obj.read(in);
}
ostream &operator<<(ostream &out, const Image &obj) {
return obj.print(out);
}
Image::~Image() {
if (!this->name.empty()) this->name.clear();
if (!this->path.empty()) this->path.clear();
this->absolute = false;
}
string Image::extension(string word) const {
return word.substr(word.find('.'), word.length());
}
string Image::withoutExtension(string word) const {
return word.substr(0, word.find('.'));
}
void Image::scan() {
string image_path;
try {
if (this->absolute == false) {
string full_name = this->path + this->name;
// silent mode true to suppress errors
image_path = findFile(full_name, true, true);
} else image_path = findFile(this->path, true, true);
Mat temp = imread(image_path, IMREAD_COLOR);
img.create(temp.rows, temp.cols, temp.type());
cv::resize(img, img, temp.size());
temp.copyTo(img);
}
catch (...) { cout << "~ INVALID PATH\n"; }
// CV_8UC3 = 8 bit unsigned integer with 3 channels (RGB)
}
void Image::show() const {
try {
// Mat img = this->scan();
cv::namedWindow("Image", cv::WINDOW_NORMAL);
// using this function makes the window not have a title bar
// cv::setWindowProperty("Image",cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
double aspect_ratio = static_cast<double>(img.cols) / img.rows;
cv::resizeWindow("Image", static_cast<int>(540 * aspect_ratio), 540);
imshow("Image", img);
// Wait for a keystroke in the window
int k = waitKey(0);
// 27 is ascii code for esc and waitKey returns an int
if (k == 27) {
cv::destroyAllWindows();
return;
}
}
catch (...) { cout << "~ OUTPUT FAILED\n"; }
}
void Image::show(const Mat &img) const {
try {
cv::namedWindow("Image", cv::WINDOW_NORMAL);
// using this function makes the window not have a title bar
// cv::setWindowProperty("Image",cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
double aspect_ratio = static_cast<double>(img.cols) / img.rows;
cv::resizeWindow("Image", static_cast<int>(540 * aspect_ratio), 540);
imshow("Image", img);
// Wait for a keystroke in the window
int k = waitKey(0);
// 27 is ascii code for esc and waitKey returns an int
if (k == 27) {
cv::destroyAllWindows();
return;
}
}
catch (...) { cout << "~ OUTPUT FAILED\n"; }
}
void Image::write() const {
try {
// basically does nothing because there is nothing applied to that image
// Mat img = this->scan();
string full_path = this->path + this->name;
cv::imwrite(full_path, img);
}
catch (...) { cout << "~ WRITING IMAGE FAILED\n"; }
}
void Image::saveShow() const {
cout << "Show image on screen (yes:1 no:0)?\n";
int temp;
cin >> temp;
cin.get();
if (temp == 1) this->show();
cout << "Save image (yes:1 no:0)?\n";
cin >> temp;
cin.get();
if (temp == 1) this->write();
}
void Image::applyAll() {
cout << "~ NOTHING TO APPLY\n";
}
string Image::getName() const {
return name;
}
string Image::getPath() const {
return path;
}
class Effect : virtual public Image {
protected:
int blurAmount;
bool effect, blackWhite, cartoon;
public:
Effect(string name = "cat.png", string path = "../Images/", bool absolute = false, bool effect = false,
int blurAmount = 0, bool blackWhite = false, bool cartoon = false);
Effect(const Effect &obj);
Effect &operator=(const Effect &obj);
// override specifier ensures that the function is virtual and is overriding a virtual function from a base class
virtual ~Effect() override;
istream &read(istream &in);
ostream &print(ostream &out) const;
void blur();
void bw();
void cartoon_effect();
void write() const;
void applyAll();
void setBlurAmount(int blurAmount);
void setBlackWhite(bool blackWhite);
void setCartoon(bool cartoon);
void serialize(string) const;
void deserialize(std::ifstream&);
};
void Effect::serialize(string fileName) const {
this->Image::serialize(fileName);
std::ofstream out(fileName, std::ios_base::app);
out<<effect<<" "<<blurAmount<<" "<<blackWhite<<" "<<cartoon<<" ";
out.close();
}
void Effect::deserialize(std::ifstream& in) {
this->Image::deserialize(in);
bool effect,blackWhite,cartoon;
int blurAmount;
in>>effect>>blurAmount>>blackWhite>>cartoon;
this->effect = effect;
this->blurAmount = blurAmount;
this->blackWhite = blackWhite;
this->cartoon = cartoon;
}
Effect::Effect(string name, string path, bool absolute, bool effect, int blurAmount, bool blackWhite, bool cartoon) :
Image(name, path, absolute) {
this->effect = effect;
this->blurAmount = blurAmount;
this->blackWhite = blackWhite;
this->cartoon = cartoon;
this->scan();
}
Effect::Effect(const Effect &obj) : Image(obj) {
this->effect = obj.effect;
this->blurAmount = obj.blurAmount;
this->blackWhite = obj.blackWhite;
this->cartoon = obj.cartoon;
this->scan();
}
Effect &Effect::operator=(const Effect &obj) {
if (this != &obj) {
Image::operator=(obj);
this->effect = obj.effect;
this->blurAmount = obj.blurAmount;
this->blackWhite = obj.blackWhite;
this->cartoon = obj.cartoon;
this->scan();
}
return *this;
}
istream &Effect::read(istream &in) {
this->Image::read(in);
cout << "Are there effects applied on the image? (yes:1 no:0) \n";
in >> this->effect;
cout << "Do you want to blur the image? (yes:1 no:0)?\n";
int temp;
in >> temp;
in.get();
if (temp == 1) {
cout << "Enter blur amount: \n";
in >> this->blurAmount;
}
cout << "Do you want to apply Black and White effect to the image? (yes:1 no:0)?\n";
in >> this->blackWhite;
cout << "Do you want to apply Cartoon effect to the image? (yes:1 no:0)?\n";
in >> this->cartoon;
this->scan();
return in;
}
ostream &Effect::print(ostream &out) const {
this->Image::print(out);
if (this->effect == true) out << "Has effects applied\n";
else out << "Doesn't have effects applied\n";
out << "Blur amount: " << this->blurAmount << endl;
if (this->blackWhite == true) out << "Has Black and White effect applied\n";
else out << "Doesn't have Black and White effect applied\n";
if (this->cartoon) out << "Has Cartoon effect applied\n";
else out << "Doesn't have Cartoon effect applied\n";
return out;
}
Effect::~Effect() {
this->effect = false;
this->blurAmount = 0;
}
void Effect::write() const {
try {
string full_path = "../Images with Effects/" + this->withoutExtension(this->name) + "_withEffects" +
this->extension(this->name);
cv::imwrite(full_path, this->img);
}
catch (...) { cout << "~ WRITING IMAGE FAILED\n"; }
}
void Effect::blur() {
if (this->blurAmount > 0) {
try {
Mat blurredImage;
// cv::GaussianBlur doesnt work with widths and heigths that are even, or 0,0
if (this->blurAmount % 2 == 0) this->blurAmount += 1;
cv::GaussianBlur(this->img, this->img, cv::Size(this->blurAmount, this->blurAmount), 0);
// blurredImage.copyTo(this->img);
this->effect = true;
}
catch (...) { cout << "~ APPLYING EFFECT FAILED\n"; }
}
}
void Effect::bw() {
if (this->blackWhite == true) {
try {
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
this->effect = true;
}
catch (...) { cout << "~ APPLYING EFFECT FAILED\n"; }
}
}
void Effect::cartoon_effect() {
if (this->cartoon == true) {
try {
Mat gray, tresh, edges;
// to check if the image is already gray
if (img.channels() != 1) cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
else img.copyTo(gray);
// blur image to get a better mask for outlines
cv::medianBlur(gray, gray, 7);
// create outline using a treshold
cv::adaptiveThreshold(gray, tresh, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 21, 7);
// blur initial image with a safer method
cv::bilateralFilter(img, edges, 21, 250, 250);
// combine initial blurred image with the outlines
cv::bitwise_and(edges, edges, img, tresh);
this->effect = true;
}
catch (...) { cout << "~ APPLYING EFFECT FAILED\n"; }
}
}
void Effect::applyAll() {
this->blur();
this->bw();
this->cartoon_effect();
}
void Effect::setBlurAmount(int blurAmount) {
this->blurAmount = blurAmount;
}
void Effect::setBlackWhite(bool blackWhite) {
this->blackWhite = blackWhite;
}
void Effect::setCartoon(bool cartoon) {
this->cartoon = cartoon;
}
class Adjustment : virtual public Image {
protected:
double brightness, contrast;
int hue;
bool adjustment;
public:
Adjustment(string name = "cat.png", string path = "../Images/", bool absolute = false, bool adjustment = false,
double brightness = 0, double contrast = 1, int hue = 0);
Adjustment(const Adjustment &obj);
Adjustment &operator=(const Adjustment &obj);
// override specifier ensures that the function is virtual and is overriding a virtual function from a base class
virtual ~Adjustment() override;
istream &read(istream &in);
ostream &print(ostream &out) const;
void brightness_adjustment();
void contrast_adjustment();
void hue_adjustment();
void write() const;
void applyAll();
void setBrightness(double brightness);
void setContrast(double contrast);
void setHue(int hue);
void serialize(string) const;
void deserialize(std::ifstream&);
};
void Adjustment::serialize(string fileName) const {
this->Image::serialize(fileName);
std::ofstream out(fileName, std::ios_base::app);
out<<adjustment<<" "<<brightness<<" "<<contrast<<" "<<hue<<" ";
out.close();
}
void Adjustment::deserialize(std::ifstream& in) {
this->Image::deserialize(in);
bool adjustment;
double brightness, contrast;
int hue;
in>>adjustment>>brightness>>contrast>>hue;
this->adjustment = adjustment;
this->brightness = brightness;
this->contrast = contrast;
this->hue = hue;
}
Adjustment::Adjustment(string name, string path, bool absolute, bool adjustment,
double brightness, double contrast, int hue) :
Image(name, path, absolute) {
this->adjustment = adjustment;
this->brightness = brightness;
this->contrast = contrast;
this->hue = hue;
this->scan();
}
Adjustment::Adjustment(const Adjustment &obj) : Image(obj) {
this->adjustment = obj.adjustment;
this->brightness = obj.brightness;
this->contrast = obj.contrast;
this->hue = obj.hue;
this->scan();
}
Adjustment &Adjustment::operator=(const Adjustment &obj) {
if (this != &obj) {
Image::operator=(obj);
this->adjustment = obj.adjustment;
this->brightness = obj.brightness;
this->contrast = obj.contrast;
this->hue = obj.hue;
this->scan();
}
return *this;
}
istream &Adjustment::read(istream &in) {
this->Image::read(in);
cout << "Is the image adjusted? (yes:1 no:0)\n";
in >> this->adjustment;
in.get();
cout << "Enter brightness [-100,100]: \n";
in >> this->brightness;
in.get();
cout << "Enter contrast [0,10]: \n\t1 = nothing changes\n\t[0,1) = lower contrast\n\t(1,10] = higher contrast\n";
in >> this->contrast;
in.get();
cout << "Enter hue [0,180]: \n";
in >> this->hue;
in.get();
this->scan();
return in;
}
ostream &Adjustment::print(ostream &out) const {
this->Image::print(out);
if (this->adjustment == true) out << "Has adjustments applied\n";
else out << "Doesn't have adjustments applied\n";
out << "Brightness value: " << this->brightness << endl;
out << "Contrast value: " << this->contrast << endl;
out << "Hue value: " << this->hue << endl;
return out;
}
Adjustment::~Adjustment() {
this->contrast = 0;
this->brightness = 0;
this->hue = 0;
this->adjustment = false;
}
void Adjustment::write() const {
try {
string full_path = "../Images with Adjustments/" + this->withoutExtension(this->name) + "_withAdjustments" +
this->extension(this->name);
cv::imwrite(full_path, this->img);
}
catch (...) { cout << "~ WRITING IMAGE FAILED\n"; }
}
void Adjustment::brightness_adjustment() {
if (this->brightness != 0 && this->brightness >= -100 && this->brightness <= 100) {
try {
// rtype == -1 means same type as source image
// alpha = contrast, beta = brightness
img.convertTo(img, -1, 1, this->brightness);
this->adjustment = true;
}
catch (...) { cout << "~ APPLYING ADJUSTMENT FAILED\n"; }
}
}
void Adjustment::contrast_adjustment() {
if (this->contrast >= 0 && this->contrast <= 10) {
try {
// rtype == -1 means same type as source image
// alpha = contrast, beta = brightness
img.convertTo(img, -1, this->contrast, 0);
this->adjustment = true;
}
catch (...) { cout << "~ APPLYING ADJUSTMENT FAILED\n"; }
}
}
void Adjustment::hue_adjustment() {
if (this->hue != 0 && this->hue >= 0 && this->hue <= 180) {
try {
Mat hsv_img;
// changing color space to HSV (HUE, SATURATION, VALUE)
cv::cvtColor(img, hsv_img, cv::COLOR_BGR2HSV);
for (int i = 0; i < hsv_img.rows; i++)
for (int j = 0; j < hsv_img.cols; j++) {
// to extract hue of the current pixel
int h = hsv_img.at<cv::Vec3b>(i, j)[0];
// adding value of this->hue to h
h = (h + this->hue) % 180;
// changing pixel hue
hsv_img.at<cv::Vec3b>(i, j)[0] = h;
}
// converting back to original color space
cv::cvtColor(hsv_img, img, cv::COLOR_HSV2BGR);
this->adjustment = true;
}
catch (...) { cout << "~ APPLYING ADJUSTMENT FAILED\n"; }
}
}
void Adjustment::applyAll() {
this->brightness_adjustment();
this->contrast_adjustment();
this->hue_adjustment();
}
void Adjustment::setBrightness(double brightness) {
this->brightness = brightness;
}
void Adjustment::setContrast(double contrast) {
this->contrast = contrast;
}
void Adjustment::setHue(int hue) {
this->hue = hue;
}
class Edited : public Effect, public Adjustment {
private:
bool edited;
string date;
public:
Edited(string name = "cat.png", string path = "../Images/", bool absolute = false, bool effect = false,
int blurAmount = 0, bool blackWhite = false, bool cartoon = false,
bool adjustment = false, double brightness = 0, double contrast = 1, int hue = 0, bool edited = false,
string date = "13/06/1826");
Edited(const Edited &obj);
Edited &operator=(const Edited &obj);
~Edited();
istream &read(istream &in);
ostream &print(ostream &out) const;
void write() const;
void applyAll();
void serialize(string) const;
void deserialize(std::ifstream&);
};
void Edited::serialize(string fileName) const {
this->Effect::serialize(fileName);
std::ofstream out(fileName, std::ios_base::app);
out<<adjustment<<" "<<brightness<<" "<<contrast<<" "<<hue<<" "<<edited<<" "<<date;
out.close();
}
void Edited::deserialize(std::ifstream& in) {
this->Effect::deserialize(in);
bool adjustment,edited;
double brightness, contrast;
int hue;
string date;
in>>adjustment>>brightness>>contrast>>hue>>edited>>date;
this->adjustment = adjustment;
this->brightness = brightness;
this->contrast = contrast;
this->hue = hue;
this->edited = edited;
this->date = date;
}
Edited::Edited(string name, string path, bool absolute, bool effect, int blurAmount, bool blackWhite, bool cartoon,
bool adjustment, double brightness, double contrast, int hue, bool edited, string date) : Image(name,path,absolute),
Effect(name,path,absolute,effect,blurAmount,blackWhite,cartoon),
Adjustment(name,path,absolute,adjustment,brightness,contrast,hue) {
this->edited = edited;
this->date = date;
this->scan();
}
Edited::Edited(const Edited &obj) : Image(obj), Effect(obj), Adjustment(obj) {
this->edited = obj.edited;
this->date = obj.date;
this->scan();
}
Edited &Edited::operator=(const Edited &obj) {
if (this != &obj) {
Effect::operator=(obj);
Adjustment::operator=(obj);
this->edited = obj.edited;
this->date = obj.date;
this->scan();
}
return *this;
}
Edited::~Edited() {
this->edited = false;
if (!this->date.empty()) this->date.clear();
}
istream &Edited::read(istream &in) {
this->Effect::read(in);
cout << "Is the image adjusted? (yes:1 no:0)\n";
in >> this->adjustment;
in.get();
cout << "Enter brightness [-100,100]: \n";
in >> this->brightness;
in.get();
cout << "Enter contrast [0,10]: \n\t1 = nothing changes\n\t[0,1) = lower contrast\n\t(1,10] = higher contrast\n";
in >> this->contrast;
in.get();
cout << "Enter hue [0,180]: \n";
in >> this->hue;
in.get();
cout << "Is the image edited (yes:1 no:0)?\n";
in >> this->edited;
in.get();
cout << "Enter date of edited image: \n";
std::getline(in, this->date);
this->scan();
return in;
}
ostream &Edited::print(ostream &out) const {
this->Effect::print(out);
if (this->adjustment == true) out << "Has adjustments applied\n";
else out << "Doesn't have adjustments applied\n";
out << "Brightness value: " << this->brightness << endl;
out << "Contrast value: " << this->contrast << endl;
out << "Hue value: " << this->hue << endl;
if (this->edited == true) out << "Is edited\n";
else out << "Is not edited\n";
out << "Date of completion: " << this->date << endl;
return out;
}
void Edited::write() const {
try {
string full_path =
"../Edited Images/" + this->withoutExtension(this->name) + "_Edited" + this->extension(this->name);
cv::imwrite(full_path, this->img);
}
catch (...) { cout << "~ WRITING IMAGE FAILED\n"; }
}
void Edited::applyAll() {
this->brightness_adjustment();
this->contrast_adjustment();
this->hue_adjustment();
this->blur();
this->bw();
this->cartoon_effect();
}
class Photoshop {
private:
Image *image;
bool favorite, goBack;
public:
Image *getImage() { return this->image; }
Image*& getImageByReference() {return this->image;}
friend istream &operator>>(istream &in, Photoshop &obj);
friend ostream &operator<<(ostream &out, const Photoshop &obj);
bool isGoBack() const;
// methods for template
void scan(){image->scan();}
void write() const {image->write();}
void show() const {image->show();}
void applyAll(){image->applyAll();}
// setters for template
void setBlurAmount(int);
void setBlackWhite(bool);
void setCartoon(bool);
void setBrightness(double);
void setContrast(double);
void setHue(int);
string getType() {
return typeid(*image).name();
}
void serialize(string) const;
void deserialize(std::ifstream&);
bool operator<(const Photoshop& obj) const {
return *(this->image) < *(obj.image);
}
bool operator==(const Photoshop& obj) const {
return *(this->image) == *(obj.image);
}
};
void Photoshop::serialize(string fileName) const {
image->serialize(fileName);
}
void Photoshop::deserialize(std::ifstream& in) {
image->deserialize(in);
}
void Photoshop::setBrightness(double brightness) {
if (typeid(*image) == typeid(Adjustment) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Adjustment&>(*image).setBrightness(brightness);
std::cout << "~ ADJUSTMENT WAS APPLIED SUCCESSFULLY\n";
} else std::cout << "~ OBJECT IS NOT OF TYPE ADJUSTMENT OR EDITING\n";
}
void Photoshop::setContrast(double contrast) {
if (typeid(*image) == typeid(Adjustment) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Adjustment&>(*image).setContrast(contrast);
std::cout << "~ ADJUSTMENT WAS APPLIED SUCCESSFULLY\n";
} else std::cout << "~ OBJECT IS NOT OF TYPE ADJUSTMENT OR EDITING\n";
}
void Photoshop::setHue(int hue) {
if (typeid(*image) == typeid(Adjustment) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Adjustment&>(*image).setHue(hue);
std::cout << "~ ADJUSTMENT WAS APPLIED SUCCESSFULLY\n";
} else std::cout << "~ OBJECT IS NOT OF TYPE ADJUSTMENT OR EDITING\n";
}
void Photoshop::setBlurAmount(int blurAmount) {
if (typeid(*image) == typeid(Effect) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Effect&>(*image).setBlurAmount(blurAmount);
std::cout << "~ EFFECT WAS APPLIED SUCCESSFULLY\n";
} else std::cout << "~ OBJECT IS NOT OF TYPE EFFECT OR EDITING\n";
}
void Photoshop::setBlackWhite(bool blackWhite) {
if (typeid(*image) == typeid(Effect) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Effect&>(*image).setBlackWhite(blackWhite);
std::cout<< "~ EFFECT WAS APPLIED SUCCESSFULLY\n";
}
else std::cout << "~ OBJECT IS NOT OF TYPE EFFECT OR EDITING\n";
}
void Photoshop::setCartoon(bool cartoon) {
if (typeid(*image) == typeid(Effect) || typeid(*image) == typeid(Edited)) {
dynamic_cast<Effect&>(*image).setCartoon(cartoon);
std::cout<< "~ EFFECT WAS APPLIED SUCCESSFULLY\n";
}
else std::cout << "~ OBJECT IS NOT OF TYPE EFFECT OR EDITING\n";
}
istream &operator>>(istream &in, Photoshop &obj) {
obj.goBack = false;
for (int i = 0; i < 10; i++) cout << "-";
cout << " CREATE ";
for (int i = 0; i < 10; i++) cout << "-";
cout << endl;
cout << "1. Effects\n";
cout << "2. Adjustments\n";
cout << "3. Editing\n";
cout << "0. Go back\n";
int temp;
cin >> temp;
if(std::cin.fail()) {
std::cout<<"~ INVALID INPUT\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return in;
}
cin.get();
switch (temp) {
case 0: {
obj.goBack = true;
break;
}
case 1: {
obj.image = new Effect();
break;
}
case 2: {
obj.image = new Adjustment();
break;
}
case 3: {
obj.image = new Edited();
break;
}
default:
cout << "~ INVALID OPTION\n";
}
if (obj.goBack == false) {
in >> *obj.image;
cout << "Is this a favorite image (yes:1 no:0)?\n";
in >> obj.favorite;
}
return in;
}
ostream &operator<<(ostream &out, const Photoshop &obj) {
out << *obj.image;
if (obj.favorite == true) out << "Is a favorite image\n";
else out << "Is not a favorite image\n";
return out;
}
bool Photoshop::isGoBack() const {
return this->goBack;
}
class Video {
private:
static int counter;
const int id;
string name;
double fps;
int blurAmount, hue;
bool blackWhite, cartoon;
double brightness, contrast;
cv::VideoCapture capture;
std::vector<Mat> sequence;
public:
Video(const string &name = "", double fps = 0.0, int blurAmount = 0, bool blackWhite = false,
bool cartoon = false, double brightness = 0, double contrast = 1, int hue = 0);
Video(const Video &obj);
~Video();
Video &operator=(const Video &obj);
friend istream &operator>>(istream &in, Video &obj);
friend ostream &operator<<(ostream &out, const Video &obj);
bool operator<(const Video& obj) const {
return this->id <= obj.id;
}
bool operator==(const Video& obj) const {