作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在 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);
}
}
由于这些有用的评论,我(希望)改进了代码,现在可以处理灰度图像,并且还设置了更类似于非 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/
我正在 OpenCV 中搜索类似于 MATLAB 中的 impixelinfo() 的函数。 impixelinfo() 告诉你 像素(x, y)和的位置 光标悬停在图像中的像素强度, 喜欢: imp
我是一名优秀的程序员,十分优秀!