-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
238 lines (205 loc) · 8.96 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
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
cv::Mat& scanImageAndInvertColors(cv::Mat& I) {
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols;
if (I.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int i,j;
for (i = 0; i < nRows; ++i) {
for (j = 0; j < nCols; ++j) {
cv::Vec3b& pixel = I.at<cv::Vec3b>(i, j);
for (int c = 0; c < channels; ++c) {
if (pixel[c] >= 255)
pixel[c] = 0; // To prevent overflow, reset to 0 if greater than 255
else
pixel[c] = 255 - pixel[c]; // Subtract 255 from each channel
}
}
}
return I;
}
cv::Mat& scanImageAndTurnBlackAndWhite(cv::Mat& I) {
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols;
if (I.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int i,j;
for (i = 0; i < nRows; ++i) {
for (j = 0; j < nCols; ++j) {
cv::Vec3b& pixel = I.at<cv::Vec3b>(i, j);
float average = (pixel[0] + pixel[1] + pixel[2])/3.0;
if (average > 128) {
pixel[0] = 255;
pixel[1] = 255;
pixel[2] = 255;
} else {
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
}
}
}
return I;
}
cv::Vec3b sharpMaskAverage(cv::Mat I, int currX, int currY, float kernel[3][3]) {
cv::Vec3b newPixel;
float averageR = (I.at<cv::Vec3b>(currX-1, currY-1)[0]*kernel[0][0] +
I.at<cv::Vec3b>(currX, currY-1)[0]*kernel[0][1] +
I.at<cv::Vec3b>(currX, currY-1)[0]*kernel[0][2] +
I.at<cv::Vec3b>(currX-1, currY)[0]*kernel[1][0] +
I.at<cv::Vec3b>(currX, currY)[0]*kernel[1][1] +
I.at<cv::Vec3b>(currX+1, currY)[0]*kernel[1][2] +
I.at<cv::Vec3b>(currX-1, currY+1)[0]*kernel[2][0] +
I.at<cv::Vec3b>(currX, currY+1)[0]*kernel[2][1] +
I.at<cv::Vec3b>(currX+1, currY+1)[0]*kernel[2][2]);
float averageG = (I.at<cv::Vec3b>(currX-1, currY-1)[1]*kernel[0][0] +
I.at<cv::Vec3b>(currX, currY-1)[1]*kernel[0][1] +
I.at<cv::Vec3b>(currX, currY-1)[1]*kernel[0][2] +
I.at<cv::Vec3b>(currX-1, currY)[1]*kernel[1][0] +
I.at<cv::Vec3b>(currX, currY)[1]*kernel[1][1] +
I.at<cv::Vec3b>(currX+1, currY)[1]*kernel[1][2] +
I.at<cv::Vec3b>(currX-1, currY+1)[1]*kernel[2][0] +
I.at<cv::Vec3b>(currX, currY+1)[1]*kernel[2][1] +
I.at<cv::Vec3b>(currX+1, currY+1)[1]*kernel[2][2]);
float averageB = (I.at<cv::Vec3b>(currX-1, currY-1)[2]*kernel[0][0] +
I.at<cv::Vec3b>(currX, currY-1)[2]*kernel[0][1] +
I.at<cv::Vec3b>(currX, currY-1)[2]*kernel[0][2] +
I.at<cv::Vec3b>(currX-1, currY)[2]*kernel[1][0] +
I.at<cv::Vec3b>(currX, currY)[2]*kernel[1][1] +
I.at<cv::Vec3b>(currX+1, currY)[2]*kernel[1][2] +
I.at<cv::Vec3b>(currX-1, currY+1)[2]*kernel[2][0] +
I.at<cv::Vec3b>(currX, currY+1)[2]*kernel[2][1] +
I.at<cv::Vec3b>(currX+1, currY+1)[2]*kernel[2][2]);
newPixel[0] = averageR;
newPixel[1] = averageG;
newPixel[2] = averageB;
return newPixel;
}
cv::Mat& scanImageAndSharpen(cv::Mat& I) {
float kernel [3][3] = { {0, -1, 0},{-1, 5, -1},{0, -1, 0} };
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols;
if (I.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
int i,j;
for (i = 1; i < nRows-1; ++i) {
for (j = 1; j < nCols-1; ++j) {
cv::Vec3b& pixel = I.at<cv::Vec3b>(i, j);
cv::Mat copy = I.clone();
cv::Vec3b newPixel = sharpMaskAverage(copy, i, j, kernel);
pixel[0] = newPixel[0];
pixel[1] = newPixel[1];
pixel[2] = newPixel[2];
}
}
return I;
}
int main(int argc, //number of strings in argv
char** argv) {
std::string imageName = "";
std::string transformCommand = "";
if (argc == 2) {
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0) {
std::cout << "-------HELP-------\n";
std::cout << "List of arguments: [image path] [flag]\n";
std::cout << "------------------\n";
std::cout << "# image path: path to the image (jpg or png extensions only)\n";
std::cout << "# flags: \n";
std::cout << " -h, -help: show help \n";
std::cout << " -s, -show: show image \n";
std::cout << " -n, -negative: show negative image \n";
std::cout << " -bnw, -blacknwhite: show black and white image \n";
std::cout << " -sharp: sharpen image \n";
} else {
std::cout << "Enter a jpg or png image as first argument followed by flag command. Enter -h or -help to show help.\n";
}
} else if (argc >= 3) {
imageName = argv[1];
if ((imageName.find(".jpg") || imageName.find(".png")) != std::string::npos) {
// Read the image file
cv::Mat image = cv::imread(imageName);
// Check for failure
if (image.empty()) {
std::cout << "Could not open or find the image\n";
std::cin.get(); //wait for any key press
return -1;
}
int argumentCount = 2;
std::vector<std::string> windowsOpened;
int coordinateX = 0;
int coordinateY = 0;
while (argumentCount < argc) {
if (strcmp(argv[argumentCount], "-s") == 0 || strcmp(argv[argumentCount], "-show") == 0) {
std::string showWindow = "original";
cv::namedWindow(showWindow);
imshow(showWindow, image);
cv::moveWindow(showWindow, coordinateX, coordinateY);
coordinateX += 500;
coordinateY += 200;
windowsOpened.push_back(showWindow);
} else if (strcmp(argv[argumentCount], "-n") == 0 || strcmp(argv[argumentCount], "-negative") == 0) {
//Negative image
cv::Mat negativeImage = image.clone();
scanImageAndInvertColors(negativeImage);
std::string negativeWindow = "negative";
cv::namedWindow(negativeWindow);
imshow(negativeWindow, negativeImage);
cv::moveWindow(negativeWindow, coordinateX, coordinateY);
coordinateX += 500;
coordinateY += 200;
windowsOpened.push_back(negativeWindow);
} else if (strcmp(argv[argumentCount], "-bnw") == 0 || strcmp(argv[argumentCount], "-blacknwhite") == 0) {
//Black and white image
cv::Mat blackAndWhiteImage = image.clone();
scanImageAndTurnBlackAndWhite(blackAndWhiteImage);
std::string blackAndWhiteWindow = "black and white";
cv::namedWindow(blackAndWhiteWindow);
imshow(blackAndWhiteWindow, blackAndWhiteImage);
cv::moveWindow(blackAndWhiteWindow, coordinateX, coordinateY);
coordinateX += 500;
coordinateY += 200;
windowsOpened.push_back(blackAndWhiteWindow);
} else if (strcmp(argv[argumentCount], "-sharp") == 0) {
//Sharp image
cv::Mat sharpImage = image.clone();
scanImageAndSharpen(sharpImage);
std::string sharpWindow = "sharp";
cv::namedWindow(sharpWindow);
imshow(sharpWindow, sharpImage);
cv::moveWindow(sharpWindow, coordinateX, coordinateY);
coordinateX += 500;
coordinateY += 200;
windowsOpened.push_back(sharpWindow);
}
argumentCount++;
}
cv::waitKey(0);
for (int i=0;i<windowsOpened.size(); ++i) {
cv::destroyWindow(windowsOpened[i]);
}
} else {
std::cout << "Enter a jpg or png image as first argument followed by flag command. Enter -h or -help to show help.\n";
}
} else {
std::cout << "Enter command followed by -h or -help to show help\n";
return -1;
}
return 0;
}