Fork me on GitHub

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);
posted @ 2021-06-27 11:25  chrislzy  阅读(110)  评论(0编辑  收藏  举报