前言
c++ opencv 数字图像处理
在matlab中进行图像处理时,在图像窗口可以点击图像像素查看对应的像素值,比较方便。在c++ opencv中需要自己编程达到对应效果,在图像处理之前可以创建对应的函数文件来辅助进行图像处理。
一、创建鼠标操作函数的头文件:onMouse.h
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void onMouse(int event, int x, int y, int flags, void* param);
二、创建鼠标操作函数的源文件:onMouse.cpp
#include "onMouse.h"
void onMouse(int event, int x, int y, int flags, void* param)
{
Mat* im = reinterpret_cast<Mat*>(param);
switch (event) {
case EVENT_LBUTTONDOWN:
if (static_cast<int>(im->channels()) == 1)
{
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<uchar>(Point(x, y))) << endl;
}
else
{
cout << "at (" << x << ", " << y << ")"
<< " B value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[0])
<< " G value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[1])
<< " R value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[2])
<< endl;
}
break;
}
}
三、示例
#include<iostream>
#include<opencv2/opencv.hpp>
#include "onMouse.h"
using namespace cv;
using namespace std;
int main()
{
Mat image1 = imread("lena.png");
if (image1.empty())
{
cout << "读取错误" << endl;
return -1;
}
imshow("image1",image1);
setMouseCallback("image1", onMouse, reinterpret_cast<void*>(&image1));
waitKey(0);
return 0;
}
|