赞
踩
读、写、显示图像作为计算机视觉OpenCV最为基础的操作,接下来了解熟悉下其API。
001_reading_displaying_write_image是OpenCV图像读写、显示例程。
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
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
整个应用分为四个步骤:
C++:
//Include Libraries
#include<opencv2/opencv.hpp>
#include<iostream>
// Namespace nullifies the use of cv::function();
using namespace std;
using namespace cv;
Python:
# import the cv2 library
import cv2
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)
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()
C++:
imwrite("grayscale.jpg", img_grayscale);
Python:
cv2.imwrite('grayscale.jpg',img_grayscale)
主要通过以下三个函数API实现:
其他API函数:
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。