博客 / 詳情

返回

[OpenCV學習筆記]獲取鼠標處圖像的座標和像素值

1、介紹

實現獲取鼠標點擊處的圖像的座標和像素值,灰度圖顯示其灰度值,RGB圖顯示rgb的值。
OpenCV獲取灰度值及彩色像素值的方法:

//灰度圖像:
image.at<uchar>(j, i) //j為行數,i為列數
//BGR彩色圖像
image.at<Vec3b>(j, i)[0] //B分量
image.at<Vec3b>(j, i)[1] //G分量
image.at<Vec3b>(j, i)[2] //R分量

這裏要通過鼠標點擊事件來獲取鼠標點擊的位置和狀態,選擇OpenCV的setMouseCallback回調函數實現。

2、效果展示

PixelPos_Mouse.PNG
# 3、代碼實現 #

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:鼠標事件類型 x,y:鼠標座標 flags:鼠標哪個鍵

void onMouse(int event, int x, int y, int flags, void* param)  //evnet:鼠標事件類型 x,y:鼠標座標 flags:鼠標哪個鍵
{
    Mat* im = reinterpret_cast<Mat*>(param);
    switch (event) {

    case EVENT_LBUTTONDOWN:
        //顯示圖像像素值

        if (static_cast<int>(im->channels()) == 1)
        {
            //若圖像為單通道圖像,則顯示鼠標點擊的座標以及灰度值
            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
        {
            //若圖像為彩色圖像,則顯示鼠標點擊座標以及對應的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主要實現鼠標點擊事件的回調,輸出點擊時鼠標的座標和對應的像素值。

main.cpp
    Mat image1 = imread("lena.png");
    if(image1.empty()){
        qDebug()<<"讀取圖像錯誤";
    }
    imshow("image1",image1);

    setMouseCallback("image1", onMouse, reinterpret_cast<void*>(&image1));

這裏我用的Qt來實現的,要在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項目裏,歡迎一起學習,也希望能收穫你的小星星。
項目源碼PixelPos_Mouse

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.