c++代码怎么获取图片的rgb
时间: 2023-11-04 15:15:12 浏览: 182
要获取图片的RGB值,需要使用一些图像处理库,例如OpenCV。下面是一个使用OpenCV获取RGB值的例子:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("test.jpg", IMREAD_COLOR); // 读取图片
if (image.empty()) // 判断是否成功读取图片
{
cout << "Failed to load image!" << endl;
return -1;
}
int x = 100; // 假设要获取像素点 (100, 100) 的RGB值
int y = 100;
Vec3b color = image.at<Vec3b>(Point(x, y)); // 获取像素点的RGB值
int r = color[2]; // R通道值
int g = color[1]; // G通道值
int b = color[0]; // B通道值
cout << "RGB: (" << r << ", " << g << ", " << b << ")" << endl;
return 0;
}
```
在上面的代码中,我们假设要获取图片中像素点 (100, 100) 的RGB值,通过 `image.at<Vec3b>(Point(x, y))` 获取该像素点的RGB值。其中,`Vec3b` 代表一个长度为3的向量,分别表示R、G、B通道的值。要获取具体的通道值,可以通过 `color[0]`、`color[1]`、`color[2]` 来获取B、G、R通道的值。
阅读全文
相关推荐















