-
Notifications
You must be signed in to change notification settings - Fork 160
/
main.cpp
136 lines (114 loc) · 3.35 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
#include <cstdio>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
string infile = "input/sky.jpg";
string outfile = "sky2.jpg";
double d;
int i2, nRows, nCols, channels;
Mat I, I1, I2, I3, I4, I5;
//create
I = Mat(2,2, CV_8UC3, Scalar(0,127,255));
//2x2 image, r=0, g=127 b=255
//CV_8UC3
//each item (red, green or blue) 8 bits long
//U: unsigned
//C3: 3 channels
I1 = Mat(2,2, CV_8UC3, Scalar::all(0));
//all black
I2 = Mat(I);
//copy constructor
//data is *not* copied
I3 = I2;
//data is *not* copied
I4 = I1.clone();
//data is copied
I1.copyTo(I4);
//data is copied
cout << "info" << endl;
I = Mat(2,2, CV_8UC3, Scalar(0,127,255));
cout << "cout" << endl << I << endl;
cout << "python" << endl << format(I,"python") << endl;
//cout << "cvs" << endl << format(I,"cvs") << endl;
cout << "numpy" << endl << format(I,"numpy") << endl;
cout << "rows" << endl << I.rows << endl;
//2
cout << "cols" << endl << I.cols << endl;
//2
cout << "step" << endl << I.step << endl;
cout << "depth()" << endl << I.depth() << endl;
//CV_8U
cout << "CV_8U" << endl << CV_8U << endl;
//CV_8U
cout << "channels()" << endl << I.channels() << endl;
//3
cout << "isContinuous()" << endl << I.isContinuous() << endl;
//?
//if true, storage is one big array in memery
//and can therefore be looped with pointers directly
//otherwise, stored rows are continuous in memory
cout << "access elements" << endl;
cout << "row pointers" << endl;
CV_Assert(I.depth() == CV_8U);
channels = I.channels();
nRows = I.rows;
nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
uchar* p;
for(int i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for (int j = 0; j < nCols; ++j)
{
cout << (int)p[j] << " ";
//p[j] = 0; //assigment is ok too
}
cout << endl;
for (int j = 0; j < nCols; j=j+channels)
{
//THERE IS SOMETHING ABOUT COLORS BEING INVERTED as b,g,r and not r,g,b
cout << "b " << (int)p[j] << endl;
cout << "g " << (int)p[j+1] << endl;
cout << "r " << (int)p[j+2] << endl;
//cout << "a " << (int)p[j+4] << endl;
}
cout << endl;
}
cout << "continuous array with pointer" << endl;
if (I.isContinuous()){
uchar* p = I.data;
for( int i =0; i < nCols*nRows; ++i){
cout << (int)*p << " ";
p++;
}
} else {
cout << "not continuous" << endl;
}
//fileio
I1 = imread( infile, 1 );
imwrite( outfile, I1 );
//highgui
namedWindow( "sky", CV_WINDOW_AUTOSIZE );
namedWindow( "gray", CV_WINDOW_AUTOSIZE );
imshow( infile, I1 );
imshow( "gray", I1 );
waitKey(0);
cout << "\nprofiling\n" << endl;
d = (double)getTickCount();
for(int i=0; i<1000; i++)
i2++;
d = ((double)getTickCount() - d)/getTickFrequency();
cout << "seconds " << d << endl;
cout << "error handling" << endl;
CV_Assert(true);
//EXCEPTION:
//CV_Assert(false);
//cvtColor( I1, gray_I1, CV_RGB2GRAY );
return 0;
}