opencv之图像的加载、显示和保存
简介
- 加载图像: cv::imread()
- 创建图像窗口: cv::namedWindow()
- 显示图像:cv::imshow()
- 保存图像:cv::imwrite()
测试代码
#include <opencv2/opencv.hpp> //总的头文件
// #include <opencv2/core.hpp>
// #include <opencv2/imgcodecs.hpp>
// #include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
std::string image_name = "/home/liuzhiyang/test/color_image.png";
cv::Mat color_image;
color_image = cv::imread(image_name, cv::IMREAD_COLOR);
if (color_image.empty())
{
std::cout << "fail to read image!" << std::endl;
return 0;
}
cv::namedWindow("color_image");
cv::namedWindow("gray_image");
cv::Mat gray_image;
cv::cvtColor(color_image, gray_image, cv::COLOR_BGR2GRAY);
cv::imshow("color_image", color_image);
cv::imshow("gray_image", gray_image);
cv::waitKey(0);
cv::imwrite("/home/liuzhiyang/test/gray_image.png", gray_image);
return 0;
}
注: 读取灰色图像
cv::imread(filename, IMREAD_GRAYSCALE);
chrislzy: 如有疑惑,错误或者建议,请在评论区留下您宝贵的文字; 转载请注明作者和出处,未经允许请勿用于商业用途!