-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20424051_Lab01.cpp
229 lines (189 loc) · 4.58 KB
/
20424051_Lab01.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
#include "Header.h"
#include "ColorTransformer.h"
#include "Converter.h"
void showInfo() {
cout << "Cu phap su dung <ProgramName.exe> <Command> <InputPath> <CommandArguments>";
}
void rgb2grayAction(String InputPath) {
//doc anh 3 kenh mau
Mat input = imread(InputPath, IMREAD_COLOR);
Mat output;
Converter ct = Converter();
if (ct.Convert(input, output, 0)) {
imshow("Anh ban dau", input);
imshow("Anh bien doi", output);
}
else {
cout << "Khong the chuyen doi sang grayscale";
}
}
void rgb2hsvAction(String InputPath) {
//doc anh 3 kenh mau
Mat input = imread(InputPath, IMREAD_COLOR);
Mat output;
Converter ct = Converter();
if (ct.Convert(input, output, 1)) {
imshow("Anh ban dau", input);
imshow("Anh bien doi", output);
}
else {
cout << "Khong the chuyen doi sang hsv";
}
}
void brightAction(String InputPath, string CommandArguments) {
// lay do sang b
short b = 0;
try {
b = stoi(CommandArguments);
if (to_string(b) != CommandArguments) {
throw exception();
}
}
catch (...) {
cout << "Do sang khong hop le";
return;
}
//doc anh bang bat ky mac sac nao co san
Mat input = imread(InputPath, IMREAD_ANYCOLOR);
Mat output;
ColorTransformer ct = ColorTransformer();
if (ct.ChangeBrighness(input, output, b)) {
imshow("Anh ban dau", input);
imshow("Anh bien doi", output);
}
else {
cout << "Khong the thay doi do sang anh";
}
}
void contrastAction(String InputPath, string CommandArguments) {
// lay do tuong phan c
float c = 0;
try {
c = stof(CommandArguments);
}
catch (...) {
cout << "Do tuong phan khong hop le";
return;
}
//doc anh bang bat ky mac sac nao co san
Mat input = imread(InputPath, IMREAD_ANYCOLOR);
Mat output;
ColorTransformer ct = ColorTransformer();
if (ct.ChangeContrast(input, output, c)) {
imshow("Anh ban dau", input);
imshow("Anh bien doi", output);
}
else {
cout << "Khong the thay doi do tuong phan anh";
}
}
void histAction(String InputPath) {
//doc anh bang bat ky mac sac nao co san
Mat input = imread(InputPath, IMREAD_ANYCOLOR);
Mat output;
ColorTransformer ct = ColorTransformer();
if (ct.CalcHistogram(input, output)) {
cout << "Tinh histogram anh thanh cong";
Mat histImage;
int ret = ct.DrawHistogram(output, histImage);
if (ret) {
imshow("Luoc do", histImage);
}
}
else {
cout << "Khong the tinh histogram anh";
}
}
void equalhistAction(String InputPath) {
//doc anh bang bat ky mac sac nao co san
Mat input = imread(InputPath, IMREAD_ANYCOLOR);
Mat output;
ColorTransformer ct = ColorTransformer();
if (ct.HistogramEqualization(input, output)) {
cout << "Can bang histogram anh thanh cong";
Mat histMatrix, histImage;
ct.CalcHistogram(input, histMatrix);
ct.DrawHistogram(histMatrix, histImage);
imshow("Luoc do goc", histImage);
imshow("Luoc do can bang", output);
}
else {
cout << "Khong the Can bang histogram anh";
}
}
void drawhistAction(String InputPath) {
//doc anh bang bat ky mac sac nao co san
Mat input = imread(InputPath, IMREAD_ANYCOLOR);
Mat histMatrix;
ColorTransformer ct = ColorTransformer();
int ret = ct.CalcHistogram(input, histMatrix);
if (ret) {
Mat histImage;
ret = ct.DrawHistogram(histMatrix, histImage);
if (ret) {
int nChannels = histMatrix.rows;
string title;
if (nChannels > 1) {
cout << "Ve luoc do anh mau";
title = "Histogram anh mau";
}
else {
cout << "Ve luoc do anh xam";
title = "Histogram anh xam";
}
imshow(title, histImage);
return;
}
}
cout << "Khong the ve histogram";
}
bool process(string Command, string InputPath, string CommandArguments) {
if (Command == "--rgb2gray") {
rgb2grayAction(InputPath);
}
else if (Command == "--rgb2hsv") {
rgb2hsvAction(InputPath);
}
else if (Command == "--bright") {
brightAction(InputPath, CommandArguments);
}
else if (Command == "--contrast") {
contrastAction(InputPath, CommandArguments);
}
else if (Command == "--hist") {
histAction(InputPath);
}
else if (Command == "--equalhist") {
equalhistAction(InputPath);
}
else if (Command == "--drawhist") {
drawhistAction(InputPath);
}
else {
return false;
}
return true;
}
int main(int argc, char* argv[]) {
// kiem tra so luong tham so hop le
if (argc > 2) {
string Command, InputPath, CommandArguments = "";
Command = argv[1];
InputPath = argv[2];
// truong hop co tham so cau lenh
if (argc > 3)
CommandArguments = argv[3];
if (!process(Command, InputPath, CommandArguments)) {
cout << "Khong tim thay Command tuong ung";
return 1;
}
}
else
{
cout << "So luong tham so thieu\n";
showInfo();
return 1;
}
waitKey(0);
return 0;
}