gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:10:05 28 4
gpt4 key购买 nike

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

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

由于这些有用的评论,我(希望)改进了代码,现在可以处理灰度图像,并且还设置了更类似于非 OpenCV 爱好者可能期望的 RGB 顺序 - 即 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;
}
}
}

关于c++ - OpenCV 中是否有类似 MATLAB 的 'impixelinfo()' 的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297686/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com