opencv-计算机视觉库

opencv-计算机视觉库

opencv的安装

  • 在github上下载opencvrelease版本仓库
  • 安装依赖项
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev  // option
  • 使用CMake编译安装
cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE = Release -D CMAKE_INSTALL_PREFIX = /usr/local ..
make -j4
sudo make install

注: 构建类型: CMAKE_BUILD_TYPE=Release\Debug

注:安装路径: CMAKE_INSTALL_PREFIX=/usr/local

加载、显示和保存图片

  • 加载图像: cv::imread()
  • 创建图像窗口: cv::namedWindow()
  • 显示图像:cv::imshow()
  • 保存图像:cv::imwrite()
#include  //总的头文件
// #include 
// #include 
// #include 


#include 
#include 

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);

转载于:https://www.cnblogs.com/ChrisCoder/p/10083764.html

你可能感兴趣的:(opencv-计算机视觉库)