要获取屏幕内指定位置的 RGB 值,可以使用 C++ 中的图形处理库,如 OpenCV。以下是一个简单的示例代码:

#include <opencv2/opencv.hpp>

int main() {
    // 获取屏幕截图
    cv::Mat screenshot = cv::imread('screenshot.png');

    // 指定位置的坐标
    int x = 100;
    int y = 200;

    // 获取 RGB 值
    cv::Vec3b pixel = screenshot.at<cv::Vec3b>(y, x);
    int r = pixel[2];
    int g = pixel[1];
    int b = pixel[0];

    // 打印 RGB 值
    std::cout << 'Red: ' << r << std::endl;
    std::cout << 'Green: ' << g << std::endl;
    std::cout << 'Blue: ' << b << std::endl;

    return 0;
}

在该示例中,我们首先使用 cv::imread() 函数读取屏幕截图,并将其存储在 cv::Mat 对象 screenshot 中。然后,我们指定要获取 RGB 值的位置的坐标 (x, y)。接下来,我们使用 screenshot.at<cv::Vec3b>(y, x) 获取该位置的像素值,其类型为 cv::Vec3b,其中包含了红、绿、蓝三个通道的值。最后,我们将红、绿、蓝三个通道的值分别存储在 rgb 变量中,并打印出来。

请注意,这个示例假设你已经有了一张屏幕截图的图片文件(例如 screenshot.png),你需要将其路径传递给 cv::imread() 函数。如果你希望实时获取屏幕上的 RGB 值,你可能需要使用其他库或方法来实现。

C++ 获取屏幕指定位置的 RGB 值 - 使用 OpenCV

原文地址: https://www.cveoy.top/t/topic/qrss 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录