通过如下代码可以获取图片某个位置rgb值,前提需要安装opencv

//rgbfetcher.cpp
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char **argv)  {
    if (argc != 4) {
        printf("Usage: rgbfetcher file x y\n");
        return -1;
    }

    // read image
    char *file = argv[1];
    Mat img = imread(file);
    if (img.empty())  {
        printf("Failed to open %s\n", file);
        return -1;
    }

    // read pixel
    int r, g, b;
    int x, y;

    x = atoi(argv[2]);
    y = atoi(argv[3]);

    b=img.at<Vec3b>(y,x)[0];
    g=img.at<Vec3b>(y,x)[1];
    r=img.at<Vec3b>(y,x)[2];

    printf("%02x%02x%02x\n", r, g, b);
    return 0;
}

编译

g++ rgbfetcher.cpp -o rgbfetcher -lopencv_core -lopencv_highgui -lopencv_imgcodecs

使用

smou:opencv $ ./rgbfetcher screen.png 1 1
0f4778
smou:opencv $ ./rgbfetcher screen.png 1 10
0e4473
smou:opencv $ ./rgbfetcher screen.png 100 100
00beaa