我为困惑的标题致歉。基本上,我想在应用颜色图之前使用鼠标光标显示图像的像素值。可能吗?我知道如何从here获取(x,y)坐标
这是我的代码:
cv::Mat falseColorsMap;
void onMouse( int event, int x, int y, int, void* );
void showImage( cv::Mat frameMat){
double min;
double max;
cv::minMaxLoc(frameMat, &min, &max);
std::cout << min << "," << max << "\n";
cv::Mat adjMap;
frameMat.convertTo(adjMap,CV_8UC1, 255 / (max-min), -min); // expand your range to 0..255. Similar to histEq();
applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_JET);
namedWindow( "Out", CV_WINDOW_NORMAL );// Create a window for display.
resizeWindow("Out", 800, 600);
setMouseCallback( "Out", onMouse, 0 );
cv::imshow("Out", falseColorsMap);
waitKey(0);
}
// Function onMouse displays cursor values
void onMouse( int event, int x, int y, int, void* )
{
if ( event != CV_EVENT_LBUTTONDOWN )
return;
Point pt = Point(x,y);
std::cout<<"("<<pt.x<<", "<<pt.y<<") ...... "<<(falseColorsMap.at<int>(y,x)) << '\n';
}
使用鼠标光标时如何获取frameMat的像素值?谢谢你的帮助!
最佳答案
请查看以下更改,以显示颜色图转换之前的原始帧像素值。在此添加了基于颜色通道的条件。如果是三通道(BGR)图像,则将显示3通道像素值;如果是单通道图像,则还将显示像素值:
cv::Mat falseColorsMap;
cv::Mat framesMap;
void onMouse( int event, int x, int y, int, void* );
void showImage( cv::Mat frameMat){
double min;
double max;
cv::minMaxLoc(frameMat, &min, &max);
std::cout << min << "," << max << "\n";
framesMap = frameMat.clone();
cv::Mat adjMap;
frameMat.convertTo(adjMap,CV_8UC1, 255 / (max-min), -min); // expand your range to 0..255. Similar to histEq();
applyColorMap(adjMap, falseColorsMap, cv::COLORMAP_JET);
namedWindow( "Out", CV_WINDOW_NORMAL );// Create a window for display.
resizeWindow("Out", 800, 600);
setMouseCallback( "Out", onMouse, 0 );
cv::imshow("Out", falseColorsMap);
waitKey(0);
}
// Function onMouse displays cursor values
void onMouse( int event, int x, int y, int, void* )
{
if ( event != CV_EVENT_LBUTTONDOWN )
return;
Point pt = Point(x,y);
if(framesMap.channels()==3)
{
std::cout<<"("<<pt.x<<", "<<pt.y<<") ...... [blue] "<<framesMap.at<Vec3b>(y,x)[0] << " [green] "<<framesMap.at<Vec3b>(y,x)[1] << " [red] "<<framesMap.at<Vec3b>(y,x)[2]<<'\n';
}
else
{
std::cout<<"("<<pt.x<<", "<<pt.y<<") ...... "<<framesMap.at<uchar>(y,x) <<'\n';
}
}