加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:05:36 所属栏目:百科 来源:网络整理
导读:我在OpenCV中搜索的函数类似于MATLAB中的impixelinfo(). impixelinfo()向您展示 像素(x,y)和的位置 光标悬停在图像中的像素强度, 喜欢: impixelinfo() in matlab shows you this OpenCV中是否有任何实现?有没有人创建它的个人版本? 解决方法 你可以这样做
我在OpenCV中搜索的函数类似于MATLAB中的impixelinfo().

impixelinfo()向您展示

>像素(x,y)和的位置
>光标悬停在图像中的像素强度,

喜欢:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读