繁体   English   中英

在OpenCV中是否有类似MATLAB的'impixelinfo()'功能?

[英]Is there a function similar MATLAB's 'impixelinfo()' available in OpenCV?

我在OpenCV中搜索的函数类似于MATLAB中的impixelinfo()

impixelinfo()向您展示

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

    喜欢:

matlab中的impixelinfo()向您展示了这一点

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);
   }
}

在此输入图像描述

作为有用的评论的结果,我(希望)改进了代码并且现在处理灰度图像,并且还将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;
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM