当前位置:   article > 正文

ubuntu22.04@laptop OpenCV Get Started: 001_reading_displaying_write_image

ubuntu22.04@laptop OpenCV Get Started: 001_reading_displaying_write_image

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

确认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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/read_display_write
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python hello_opencv.py
  • 1
  • 2

3. 过程分析

整个应用分为四个步骤:

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

3.1 导入OpenCV库

C++:

//Include Libraries
#include<opencv2/opencv.hpp>
#include<iostream>
 
// Namespace nullifies the use of cv::function(); 
using namespace std;
using namespace cv;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Python:

# import the cv2 library 
import cv2
  • 1
  • 2

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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() 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.4 保存图像文件

C++:

imwrite("grayscale.jpg", img_grayscale);
  • 1

Python:

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

4. 总结

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

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

在这里插入图片描述

其他API函数:

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

5. 参考资料

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/72635
推荐阅读
相关标签
  

闽ICP备14008679号