-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab06_2.cpp
90 lines (72 loc) · 2.77 KB
/
Lab06_2.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
//
// Lab06_2.cpp
// Lab06
//
//
#include <opencv2/opencv.hpp>
const std::string imageName = "unnamed.jpg";
const std::string windowName_0 = "Source Image";
const std::string windowName_1 = "Processed Image";
double getUserEntry (std::string message, int min, int max);
int main( /** No argument is needed */ ) {
// read the image from the project directory
cv::Mat image = imread(imageName, cv::IMREAD_COLOR);
// Check if could find the image
if( image.empty() ) {
std::cout << "Could not read the image" << std::endl ;
return 1;
}
std::string menu = "0 Exit\n1 Rotate\n2 Resize\n3 Perspective\n";
cv::namedWindow(windowName_0);
cv::namedWindow(windowName_1);
cv::moveWindow(windowName_1, 200, 100);
cv::imshow(windowName_0, image);
cv::waitKey(33);
cv::Mat processed_image;
int choice = (int) getUserEntry("Enter a choice\n" + menu, 0, 3);
while (choice != 0) {
if (choice == 1) {
double angle = getUserEntry("Enter an angle to rotate.", -360, 360);
cv::Point2f centre = cv::Point2f(image.cols/2.0, image.rows/2.0);
cv::Mat rMat = cv::getRotationMatrix2D(centre, angle, 1.0);
cv::warpAffine(image, processed_image, rMat, image.size());
cv::imshow(windowName_1, processed_image);
cv::waitKey(33);
}
else if (choice == 2) {
double fX = getUserEntry("Enter resizing factor for X: ", 0, INT_MAX);
double fY = getUserEntry("Enter resizing factor for Y: ", 0, INT_MAX);
cv::resize(image, processed_image, cv::Size(), fX, fY);
cv::imshow(windowName_1, processed_image);
cv::waitKey(33);
}
else if (choice == 3) {
cv::Matx<double,3,3> homographyMat = {0.4,-0.4,190, 0.15,0.4,100, 0,0,1};
cv::warpPerspective(image, processed_image, homographyMat, image.size());
cv::imshow(windowName_1, processed_image);
cv::waitKey(33);
}
choice = (int) getUserEntry("\nEnter a choice\n" + menu, 0, 3);
}
cv::destroyAllWindows();
return 0;
}
double getUserEntry (std::string message, int min, int max) {
double* answer = nullptr;
std::cout << message << std::endl;
while ( answer == nullptr ) {
answer = new double;
std::cout << "Your entry: ";
std::cin >> *answer;
if (!std::cin.fail() && *answer >= min && *answer <= max) /** do nothing */;
else {
std::cin.clear();
std::cout << "Please try again.\n" << std::endl;
delete answer;
answer = nullptr;
}
/** flush the buffer */
std::cin.ignore(INT_MAX, '\n');
}
return *answer;
}