功能描述:生成一张宝蓝色图片,并对其修改H通道,使之成为一张紫色图片。 环境:OpenCV4.3.0,VS2017
#include <opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat img(400, 400, CV_8UC3, cv::Scalar(167, 47, 0));
imshow("原图", img);
Mat HSV;
cvtColor(img, HSV, COLOR_BGR2HSV);
Mat img0, img1, img2;
Mat result;
vector<Mat> imgv;
split(HSV, imgv);
img0 = imgv.at(0);
img1 = imgv.at(1);
img2 = imgv.at(2);
merge(imgv, result);
imshow("未修改,合并后的HSV", result);
cvtColor(result, result, COLOR_HSV2BGR);
imshow("未修改,合并后的BGR", result);
cout << "H通道改变前:" << (int)img0.at<uchar>(0, 0) << endl;
for (int i = 0; i < img0.rows; i++)
{
for (int j = 0; j < img0.cols; j++)
{
img0.at<uchar>(i, j) += 20;
}
}
cout << "H通道改变后:" << (int)img0.at<uchar>(0, 0) << endl;
cout << "S通道改变前:" << (int)img1.at<uchar>(0, 0) << endl;
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{
img1.at<uchar>(i, j) -= 0;
}
}
cout << "S通道改变后:" << (int)img1.at<uchar>(0, 0) << endl;
cout << "V通道改变前:" << (int)img2.at<uchar>(0, 0) << endl;
for (int i = 0; i < img2.rows; i++)
{
for (int j = 0; j < img2.cols; j++)
{
img2.at<uchar>(i, j) -= 0;
}
}
cout << "V通道改变后:" << (int)img2.at<uchar>(0, 0) << endl;
merge(imgv, result);
imshow("修改后,合并后的HSV", result);
Mat img_RGB;
cvtColor(result, img_RGB, COLOR_HSV2BGR);
imshow("修改后,合并后的RGB", img_RGB);
waitKey(0);
return 0;
}
程序运行结果图:
时人不识余心乐,将谓偷闲学少年。(选自《春日偶成》程颢)
|