是否有一个功能类似MATLAB的“impixelinfo()”提供的OpenCV?

问题描述:

我在寻找在OpenCV的功能类似于在MATLAB impixelinfo()是否有一个功能类似MATLAB的“impixelinfo()”提供的OpenCV?

impixelinfo()显示你

  1. 像素的(x, y)
  2. 光标悬停在图像中的像素强度,

    喜欢的位置:

impixelinfo() in matlab shows you this

有OpenCV中已经是这个任何实现?有没有人有创建它的个人版本?

你可以做这样的事情:

#include <opencv2/opencv.hpp> 
#include <iostream> 

using namespace std; 
using namespace cv; 

Mat img; 

void 
CallBackFunc(int event,int x,int y,int flags,void* userdata) 
{ 
    if(event==EVENT_MOUSEMOVE){ 
     cout << "Pixel (" << x << ", " << y << "): " << img.at<Vec3b>(y,x) << endl; 
    } 
} 

int main() 
{ 
    // Read image from file 
    img=imread("demo.jpg"); 

    // Check it loaded 
    if(img.empty()) 
    { 
     cout << "Error loading the image" << endl; 
     exit(1); 
    } 

    //Create a window 
    namedWindow("ImageDisplay",1); 

    // Register a mouse callback 
    setMouseCallback("ImageDisplay",CallBackFunc,nullptr); 

    // Main loop 
    while(true){ 
     imshow("ImageDisplay",img); 
     waitKey(50); 
    } 
} 

enter image description here

作为的有益意见的结果,我(希望)提高了代码,现在处理灰度图像,并且还设置RGB排序更类似于非OpenCV爱好者可能期望的 - 即RGB而不是BGR。更新后的函数如下所示:

void 
CallBackFunc(int event,int x,int y,int flags,void* userdata) 
{ 
    if(event==EVENT_MOUSEMOVE){ 
     // Test if greyscale or color 
     if(img.channels()==1){ 
     cout << "Grey Pixel (" << x << ", " << y << "): " << (int)img.at<uchar>(y,x) << endl; 
     } else { 
     cout << "RGB Pixel (" << x << ", " << y << "): " << (int)img.at<Vec3b>(y,x)[2] << "/" << (int)img.at<Vec3b>(y,x)[1] << "/" << (int)img.at<Vec3b>(y,x)[0] << endl; 
     } 
    } 
} 
+0

这是超爽! –

+1

我可能应该检查图像有3个通道,并略微不同地处理灰度图像。也许以后... –

+0

这看起来不错! TNX。如果我想看到从灰度图像的亮度值更改:img.at (Y,X),以img.at (Y,X)......但为什么然后我看到奇怪的字符,而不是从0-255的整数?? – jok23