1.创建纯色图片
cv::Mat mat(100, 100, CV_8UC3);
int es = mat.elemSize(); // 单个数据大小
int size = mat.rows * mat.cols * es; // 所有数据大小
for (int i = 0; i < size; i +=es){
mat.data[i] = 0; // Blue
mat.data[i + 1] = 0; // Green
mat.data[i + 2] = 0; // Red
}
cv::imwrite("cv_blue.jpg",mat);
2.opencv颜色转换(RGB-HSV)
opencv中HSV的取值范围:H:[0,180],S:[0,255],V:[0,255]
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
//std::cout << cv::getVersionString() << std::endl;
cv::Mat image = cv::imread("cv_blue.jpg");
cv::cvtColor(image,image,CV_BGR2RGB);
/**
* H: [0,180],S:[0,255],V:[0,255]
*/
cv::Mat temp = image.clone();
cv::cvtColor(temp,temp,CV_RGB2HSV);
int h = temp.at<cv::Vec3b>(0,0)[0];
int s = temp.at<cv::Vec3b>(0,0)[1];
int v = temp.at<cv::Vec3b>(0,0)[2];
std::cout << "h:" << h / 180.0 << ", s:" << s / 255.0<<", v:" << v / 255.0 << std::endl;
int red = static_cast<int>(image.at<cv::Vec3b>(0,0)[0]);
int green = static_cast<int>(image.at<cv::Vec3b>(0,0)[1]);
int blue = static_cast<int>(image.at<cv::Vec3b>(0,0)[2]);
std::cout << "red:" << red << ", green:" << green <<", blue:" << blue << std::endl;
}
h:0.666667, s:1, v:0.996078
red:0, green:0, blue:254
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!