簡體   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