Personal notes when learning OpenCV, for the convenience of netizens and myself to check, the following records are now made:
Go directly to the code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
// Declare the method to be called in advance
void onMouse(int event, int x, int y, int flgs, void* param);
int main(int atgc, char *argv[]){
cv::Mat image; // 创建一个空图像
std::cout << "This image is " << image.rows << " x " << image.cols << std::endl;
image = cv::imread("E:/earth.jpg"); // 读取图像
if (image.empty()){
cout << "read image error!" << endl;
return 0;
}
// Example of flipping an image
Mat flipImage;
flip(image, flipImage, 1); // positive number means horizontal, 0 means vertical, negative number means horizontal and vertical
// File read and write operations
//imwrite("E:/flipImage.jpg", flipImage);
// Read an image and convert it to grayscale
Mat grayImage = imread("E:/earth.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat threeChannel = imread("E:/earth.jpg", CV_LOAD_IMAGE_COLOR);
cout << " All channels :" << threeChannel.channels() << endl;
// draw graphics or write text on the image
circle(image,
Point(122, 110), // center of circle
65, // radius
0, // color
3); // thickness
putText(image,
"This is a picture",
Point(40, 40),
cv::FONT_HERSHEY_PLAIN, // font type
2.0, // font size
255, // font color
2 // text thickness
);
//// Image display
cvNamedWindow("pic", CV_WINDOW_AUTOSIZE);
imshow("pic", image);
cvNamedWindow("pic1", CV_WINDOW_AUTOSIZE);
imshow("pic1", flipImage);
// Mouse click time discrimination
setMouseCallback("pic", onMouse, reinterpret_cast<void*>(&image));
waitKey(0);
return 0;
}
// Method implementation
void onMouse(int event, int x, int y, int flgs, void* param){
Mat *im = reinterpret_cast<Mat*>(param);
switch (event)
{
case CV_EVENT_LBUTTONDOWN: // The trigger callback function of the left mouse button press time
cout <<"at (" << x << "," << y <<") value is:" << static_cast< int>(im->at<uchar>(Point(x, y))) << endl;
break;
}
}
Reference: OpenCV computer vision programming strategy