cvtColor:转换图像的颜色格式
例1:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
void printImgInfor(Mat& img, string imgName)
{
cout << "------------------------------------------------" << endl;
cout << imgName << endl;
cout << "dims: " << img.dims << endl;
cout << "rows: " << img.rows << endl;
cout << "cols: " << img.cols << endl;
cout << "size: " << img.size << endl;
cout << "channels: " << img.channels() << endl;
cout << "type: " << img.type() << endl;
cout << "depth: " << img.depth() << endl;
cout << "elemSize: " << img.elemSize() << endl;
cout << "elemSize1:" << img.elemSize1() << endl;
cout << "step: " << img.step << endl;
cout << "step1: " << img.step1() << endl;
cout << "------------------------------------------------" << endl;
}
int main()
{
Mat img1;
Mat img2;
img1 = imread("D:/file_for_test/image/fuji.jpg");
cvtColor(img1, img2, COLOR_BGR2GRAY);
imshow("BGR", img1);
imshow("GRAY", img2);
printImgInfor(img1, "BGR");
printImgInfor(img2, "GRAY");
waitKey(0);
return 0;
}
运行结果:
例2:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
void printImgInfor(Mat& img, string imgName)
{
cout << "------------------------------------------------" << endl;
cout << imgName << endl;
cout << "dims: " << img.dims << endl;
cout << "rows: " << img.rows << endl;
cout << "cols: " << img.cols << endl;
cout << "size: " << img.size << endl;
cout << "channels: " << img.channels() << endl;
cout << "type: " << img.type() << endl;
cout << "depth: " << img.depth() << endl;
cout << "elemSize: " << img.elemSize() << endl;
cout << "elemSize1:" << img.elemSize1() << endl;
cout << "step: " << img.step << endl;
cout << "step1: " << img.step1() << endl;
cout << "------------------------------------------------" << endl;
}
int main()
{
Mat img1;
Mat img2;
img1 = imread("D:/file_for_test/image/fuji.jpg");
cvtColor(img1, img2, COLOR_BGR2HLS);
imshow("BGR", img1);
imshow("HLS", img2);
printImgInfor(img1, "BGR");
printImgInfor(img2, "HLS");
waitKey(0);
return 0;
}
运行结果: