OpenCV从入门到放弃系列之——图像的基本操作

时间:2021-07-27 23:16:18

读取、修改、保存图像

  • 图像读取函数imread();
  • 图像颜色空间的转换cvtColor();
  • 图像保存至硬盘imwrite();
/*************************************************
* Author: zhonghuasong
* Email: zhonghsuestc@163.com
* Create Time: 2016-12-03 15:03:00
************************************************/ #include <iostream>
#include <opencv/cv.h>
#include <opencv2/highgui.hpp> using namespace cv;
using namespace std; int main(int argc, char** argv) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " <imagefile>" << endl;
} char* imageName = argv[1]; Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); if (!image.data) {
cout << "No image data" << endl;
return -1;
} Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY); imwrite("./Gray_Image.jpg", gray_image); namedWindow(imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE); imshow(imageName, image);
imshow("Gray image", gray_image); waitKey(0); return 0;
}