点击获取图片像素值
想看一张图片某个像素的像素值.印象里只有matlab的image viewer有这个功能.
其他软件都没有.不如自己写一个
支持jpg,png,tif
main.cpp
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
void on_mouse(int EVENT, int x, int y, int flags, void* userdata);
Mat hh;
int main(int argc,char** argv)
{
namedWindow("aa");
Mat src;
src = imread(argv[1],cv::IMREAD_ANYDEPTH);
hh = src;
//cvtColor(src, src, COLOR_RGB2GRAY);
setMouseCallback("aa", on_mouse,NULL);
//以40ms刷新显示
while (1)
{
imshow("aa", src);
waitKey(40);
}
return 0;
}
void on_mouse(int EVENT, int x, int y, int flags, void* userdata)
{
// Mat hh;
// hh = *(Mat*)userdata;
Point p(x, y);
switch (EVENT)
{
case EVENT_LBUTTONDOWN:
{
if(hh.type() == CV_8UC3)
{
printf("b=%d\t", hh.at<Vec3b>(p)[0]);
printf("g=%d\t", hh.at<Vec3b>(p)[1]);
printf("r=%d\n", hh.at<Vec3b>(p)[2]);
}
else if(hh.type() == CV_16UC1)
std::cout<< hh.at<unsigned short>(p) <<std::endl;
else if(hh.type() == CV_32FC1)
std::cout<< hh.at<float>(p) <<std::endl;
circle(hh, p, 2, Scalar(255),3);
}
break;
}
}
CMakeLists.txt
project(main)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
include_directories(${PROJECT_SOURCE_DIR})
message(${PROJECT_SOURCE_DIR})
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
## Declare the executable target built from your sources
add_executable(main ${SRC_FILES})
# Link your application with OpenCV libraries
target_link_libraries(main ${OpenCV_LIBS})
1059

被折叠的 条评论
为什么被折叠?



