ubuntu22.04@laptop OpenCV Get Started: 001_reading_displaying_write_image

ubuntu22.04@laptop OpenCV Get Started: 001_reading_displaying_write_image

  • 1. 源由
  • 2. Read/Display/Write应用Demo
    • 2.1 C++应用Demo
    • 2.2 Python应用Demo
  • 3. 过程分析
    • 3.1 导入OpenCV库
    • 3.2 读取图像文件
    • 3.3 显示图像
    • 3.4 保存图像文件
  • 4. 总结
  • 5. 参考资料

1. 源由

读、写、显示图像作为计算机视觉OpenCV最为基础的操作,接下来了解熟悉下其API。

2. Read/Display/Write应用Demo

001_reading_displaying_write_image是OpenCV图像读写、显示例程。

2.1 C++应用Demo

C++应用Demo工程结构:

001_reading_displaying_write_image/CPP$ tree .
.
├── CMakeLists.txt
├── grayscale_image.jpg
├── read_display_write_opencv.cpp
└── test.jpg

0 directories, 4 files

确认OpenCV安装路径:

$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake


$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

C++应用Demo工程编译执行:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/read_display_write

2.2 Python应用Demo

Python应用Demo工程结构:

001_reading_displaying_write_image/Python$ tree .
.
├── grayscale.jpg
├── read_display_write_opencv.py
├── requirements.txt
└── test.jpg

0 directories, 4 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python hello_opencv.py

3. 过程分析

整个应用分为四个步骤:

  1. 导入OpenCV库
  2. 读取图像文件
  3. 显示图像
  4. 保存图像文件

3.1 导入OpenCV库

C++:

//Include Libraries
#include
#include
 
// Namespace nullifies the use of cv::function(); 
using namespace std;
using namespace cv;

Python:

# import the cv2 library 
import cv2

3.2 读取图像文件

C++:

// Read an image 
Mat img_color = imread("test.jpg", IMREAD_COLOR);
Mat img_grayscale = imread("test.jpg", IMREAD_GRAYSCALE);
Mat img_unchanged = imread("test.jpg", IMREAD_UNCHANGED);

or
Mat img_color = imread("test.jpg", 1);
Mat img_grayscale = imread("test.jpg", 0);
Mat img_unchanged = imread("test.jpg", -1);

Python:

# Read an image
img_color = cv2.imread('test.jpg',cv2.IMREAD_COLOR)
img_grayscale = cv2.imread('test.jpg',cv2.IMREAD_GRAYSCALE)
img_unchanged = cv2.imread('test.jpg',cv2.IMREAD_UNCHANGED)

or
img_color = cv2.imread('test.jpg',1)
img_grayscale = cv2.imread('test.jpg',0)
img_unchanged = cv2.imread('test.jpg',-1)

3.3 显示图像

C++:

// Create a window.
namedWindow( "color image", WINDOW_AUTOSIZE );
namedWindow( "grayscale image", WINDOW_AUTOSIZE );
namedWindow( "unchanged image", WINDOW_AUTOSIZE );
 
// Show the image inside it.
imshow( "color image", img_color ); 
imshow( "grayscale image", img_grayscale );
imshow( "unchanged image", img_unchanged ); 
 
// Wait for a keystroke.   
waitKey(0);  
 
// Destroys all the windows created                         
destroyAllWindows();

Python:

#Displays image inside a window
cv2.imshow('color image',img_color)  
cv2.imshow('grayscale image',img_grayscale)
cv2.imshow('unchanged image',img_unchanged)
 
# Waits for a keystroke
cv2.waitKey(0)  
 
# Destroys all the windows created
cv2.destroyAllwindows() 

3.4 保存图像文件

C++:

imwrite("grayscale.jpg", img_grayscale);

Python:

cv2.imwrite('grayscale.jpg',img_grayscale)

4. 总结

主要通过以下三个函数API实现:

  1. imread() - 读取图像文件
  2. imshow() - 显示图像
  3. imwrite() - 保存图像文件

ubuntu22.04@laptop OpenCV Get Started: 001_reading_displaying_write_image_第1张图片

其他API函数:

  • waitKey() - 等待按键操作
  • destroyAllWindows() - 类似析构函数

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

你可能感兴趣的:(Linux,opencv,人工智能,计算机视觉)