-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.cpp
More file actions
103 lines (83 loc) · 2.36 KB
/
view.cpp
File metadata and controls
103 lines (83 loc) · 2.36 KB
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
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
void show_mat(const cv::Mat &image, std::string const &win_name);
cv::Mat &invert_mat(cv::Mat &mat);
cv::Mat& invert_mat_pointer(cv::Mat &mat);
int main(int argc, char **argv) {
if (argc != 3) {
printf("usage: Opencv_Test <Image_Path> <Output_Path>\n");
return -1;
}
Mat image;
image = imread(argv[1], 1);
if (!image.data) {
printf("No image data (warning: OpenCV recognize files by extensions)\n");
return -1;
}
show_mat(image, "Input");
image = invert_mat_pointer(image);
show_mat(image, "Output");
imwrite(argv[2], image);
return 0;
}
cv::Mat& invert_mat(cv::Mat &mat) {
// accept only char type matrices
CV_Assert(mat.depth() == CV_8U);
const int channels = mat.channels();
switch (channels) {
case 1: {
// gray scale image
MatIterator_<uchar> it, end;
for (it = mat.begin<uchar>(), end = mat.end<uchar>(); it != end; ++it)
*it = ~*it;
break;
}
case 3: {
// RGB image
MatIterator_<Vec3b> it, end;
for (it = mat.begin<Vec3b>(), end = mat.end<Vec3b>(); it != end; ++it) {
(*it)[0] = ~(*it)[0];
(*it)[1] = ~(*it)[1];
(*it)[2] = ~(*it)[2];
}
}
}
return mat;
}
cv::Mat& invert_mat_pointer(cv::Mat &mat) {
int channels = mat.channels();
int nRows = mat.rows;
int nCols = mat.cols;
int i, j;
switch (channels) {
case 1: {
// gray scale image
uchar *p;
for (i = 0; i < nRows; ++i) {
p = mat.ptr<uchar>(i);
for (j = 0; j < nCols; ++j) {
p[j] = ~p[j];
}
}
}
case 3: {
// RGB image
Vec3b *p;
for (i = 0; i < nRows; ++i) {
p = mat.ptr<Vec3b>(i);
for (j = 0; j < nCols; ++j) {
p[j][0] = ~p[j][0];
p[j][1] = ~p[j][1];
p[j][2] = ~p[j][2];
}
}
}
}
return mat;
}
void show_mat(const cv::Mat &image, std::string const &win_name) {
namedWindow(win_name, WINDOW_AUTOSIZE);
imshow(win_name, image);
waitKey(0);
}