1、介紹
實(shí)現(xiàn)獲取鼠標(biāo)點(diǎn)擊處的圖像的坐標(biāo)和像素值,灰度圖顯示其灰度值,RGB圖顯示rgb的值帮哈。
OpenCV獲取灰度值及彩色像素值的方法:
//灰度圖像:
image.at<uchar>(j, i) //j為行數(shù),i為列數(shù)
//BGR彩色圖像
image.at<Vec3b>(j, i)[0] //B分量
image.at<Vec3b>(j, i)[1] //G分量
image.at<Vec3b>(j, i)[2] //R分量
這里要通過鼠標(biāo)點(diǎn)擊事件來獲取鼠標(biāo)點(diǎn)擊的位置和狀態(tài)锰镀,選擇OpenCV的setMouseCallback回調(diào)函數(shù)實(shí)現(xiàn)娘侍。
2、效果展示
PixelPos_Mouse.PNG
3泳炉、代碼實(shí)現(xiàn)
onMouse.h
#ifndef ONMOUSE_H
#define 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); //evnet:鼠標(biāo)事件類型 x,y:鼠標(biāo)坐標(biāo) flags:鼠標(biāo)哪個(gè)鍵
void onMouse(int event, int x, int y, int flags, void* param) //evnet:鼠標(biāo)事件類型 x,y:鼠標(biāo)坐標(biāo) flags:鼠標(biāo)哪個(gè)鍵
{
Mat* im = reinterpret_cast<Mat*>(param);
switch (event) {
case EVENT_LBUTTONDOWN:
//顯示圖像像素值
if (static_cast<int>(im->channels()) == 1)
{
//若圖像為單通道圖像憾筏,則顯示鼠標(biāo)點(diǎn)擊的坐標(biāo)以及灰度值
switch (im->type())
{
case 0:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<uchar>(Point(x, y))) << endl; break;
case 1:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<char>(Point(x, y))) << endl; break;
case 2:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<ushort>(Point(x, y))) << endl; break;
case 3:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<short>(Point(x, y))) << endl; break;
case 4:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<int>(Point(x, y))) << endl; break;
case 5:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<float>(Point(x, y))) << endl; break;
case 6:
cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<double>(Point(x, y))) << endl; break;
}
}
else
{
//若圖像為彩色圖像,則顯示鼠標(biāo)點(diǎn)擊坐標(biāo)以及對應(yīng)的B, G, R值
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;
}
}
#endif // ONMOUSE_H
頭文件onMouse.h主要實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊事件的回調(diào)花鹅,輸出點(diǎn)擊時(shí)鼠標(biāo)的坐標(biāo)和對應(yīng)的像素值氧腰。
main.cpp
Mat image1 = imread("lena.png");
if(image1.empty()){
qDebug()<<"讀取圖像錯(cuò)誤";
}
imshow("image1",image1);
setMouseCallback("image1", onMouse, reinterpret_cast<void*>(&image1));
這里我用的Qt來實(shí)現(xiàn)的,要在pro文件里添加OpenCV庫的引用
win32:CONFIG(release, debug|release): LIBS += -LD:/opencv/build/x64/vc15/lib/ -lopencv_world455
else:win32:CONFIG(debug, debug|release): LIBS += -LD:/opencv/build/x64/vc15/lib/ -lopencv_world455d
else:unix: LIBS += -LD:/opencv/build/x64/vc15/lib/ -lopencv_world455
INCLUDEPATH += D:/opencv/build/include
DEPENDPATH += D:/opencv/build/include
4刨肃、源碼展示
本小例程的代碼放到我的開源gitte項(xiàng)目里古拴,歡迎一起學(xué)習(xí),也希望能收獲你的小星星之景。
項(xiàng)目源碼PixelPos_Mouse