forked from oreillymedia/Learning-OpenCV-3_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_02-09.cpp
76 lines (52 loc) · 1.8 KB
/
example_02-09.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
// Example 2-9. Getting and setting pixels in Example 2-8
#include <opencv2/opencv.hpp>
void help(char** argv ) {
std::cout << "\n"
<< "\nExample 2-9. Getting and setting pixels in Example 2-8"
<< "\nCall:\n"
<< argv[0] <<" <path/image>\n"
<< "For example:\n"
<< argv[0] << " ../fruits.jpg\n"
<< std::endl;
}
int main( int argc, char** argv ) {
if (argc != 2) {
help(argv);
return 0;
}
cv::Mat img_rgb, img_gry, img_cny, img_pyr, img_pyr2;
cv::namedWindow( "Example Gray", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example Canny", cv::WINDOW_AUTOSIZE );
img_rgb = cv::imread( argv[1] );
cv::cvtColor( img_rgb, img_gry, cv::COLOR_BGR2GRAY);
cv::pyrDown( img_gry, img_pyr );
cv::pyrDown( img_pyr, img_pyr2 );
cv::Canny( img_pyr2, img_cny, 10, 100, 3, true );
// ----------------------------------------------------
// Start new code for example 2-9
//
int x = 16, y = 32;
cv::Vec3b intensity = img_rgb.at< cv::Vec3b >(y, x);
// ( Note: We could write img_rgb.at< cv::Vec3b >(x,y)[0] )
//
uchar blue = intensity[0];
uchar green = intensity[1];
uchar red = intensity[2];
std::cout << "At (x,y) = (" << x << ", " << y <<
"): (blue, green, red) = (" <<
(unsigned int) blue <<
", " << (unsigned int)green << ", " <<
(unsigned int) red << ")" << std::endl;
std::cout << "Gray pixel there is: " <<
(unsigned int) img_gry.at<uchar>(y, x) << std::endl;
x /= 4; y /= 4;
std::cout << "Pyramid2 pixel there is: " <<
(unsigned int)img_pyr2.at<uchar>(y, x) << std::endl;
img_cny.at<uchar>(x, y) = 128; // Set the Canny pixel there to 128
//
// End new code for example 2-9
// ----------------------------------------------------
cv::imshow( "Example Gray", img_gry );
cv::imshow( "Example Canny", img_cny );
cv::waitKey(0);
}