点击获取图片像素值

点击获取图片像素值

想看一张图片某个像素的像素值.印象里只有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})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值