OpenCV-based traditional vision application OpenCV-Python library and matplotlib library for image processing

image generation

OpenCv is a classic special-purpose library in computer vision. It has the advantages of supporting multiple languages ​​and cross-platforms, and has powerful functions.
OpenCv-Python provides a Python interface for OpenCV, so that users can call C/C++ in Python, so as to achieve the required functions under the premise of ensuring legibility and operating efficiency.

  • The cv2.imshow(window_name, img) function displays the image in the window, and the window will automatically adapt to different image sizes.

The first parameter window_name is the name of the window, which is a string, and the user can create as many windows as needed; the second parameter img is the name of the image.

Display the image using OpenCV:

import cv2

image = cv2.imread("./dog.jpg")  # 读取图像
# 第二个参数填cv2.IMREAD_GRAYSCALE  就会变成灰度图

cv2.namedWindow("window")   # 创建窗口
cv2.imshow("window", image)  # 显示图像
cv2.waitKey(0)        # 等待键盘输入
cv2.destroyAllWindows()   # 销毁窗口

insert image description here

Display the image using matplotlib:

# 目标:提供一张图像,使用matplotlib显示该图像
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("./dog.jpg")

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # 将颜色通道从bgr转化成rgb

# 使用matplotlib显示图像
plt.imshow(image)
plt.show()

Matplotlib is also a commonly used image processing library. You can use the matplotlib.pyplot.imshow(img) function to display images, and the parameter img represents the image object.

It should be noted that the image channel read by OpenCV is BGR, while the color channel used by Matplotlib is RGB, so color channel conversion is required. First convert the BGR color channel read by OpenCV to an RGB color image, and then use matplotlib to display the image correctly.

insert image description here

Summary:
In opencv, you can use v2.imshow() to read images.
This function usually has two parameters, the first parameter is the image address, and the second parameter is a flag that specifies the way to read the image.

The cv2.cvtColor(img,color_change) function converts the color dimension.
The first parameter img is an image object.
The second parameter color_change can be cv2.COLOR_BGR2GRAY, which is used to convert the BGR channel color image into a grayscale image.

  • This function returns the modified matrix of image numbers, so we can print it to see the modified matrix

In Matplotlib, images can be read using the matplotlib.pyplot.imread(fname,format=None) function.
Among them, fname is the image path; format is the image format, and the default value is None. If no image format is provided, the imread() function will extract the image format from fname (such as jpg png). The
return value of this function is a matrix of image numbers, and the dimensions are as follows:
(M, N, 3): for RGB color images
(M , N): for grayscale images

The use of imread()

import cv2
import matplotlib.pyplot as plt

image_gray = cv2.imread('./dog.jpg', flags=0)   # 读取单通道灰度图
print(image_gray.shape)   # (600, 960, 3)
print(image_gray.size)   # 1728000
print(image_gray.dtype)  # uint8
plt.imshow(image_gray, cmap="gray")
plt.show()

image_bgr = cv2.imread("./dog.jpg", flags=1)  # 读取三通道彩色图
image_rgb = image_bgr[:, :, ::-1]
print(image_bgr.shape)
print(image_bgr.size)
print(image_bgr.dtype)
plt.imshow(image_rgb)
plt.show()

image_gray2 = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2GRAY)  # 将彩色图转换为灰度图
plt.imshow(image_gray2, cmap="gray")
plt.show()

Single channel grayscale image:
insert image description here

Three-channel colormap:
insert image description here

Convert a color image to grayscale:
insert image description here

from matplotlib import pyplot as plt

image_rgb = plt.imread("./dog.jpg")
print(image_rgb.shape)   # (600, 960, 3)  高度、宽度、通道数
print(image_rgb.size)   # 1728000    高度*宽度*通道数
print(image_rgb.dtype)  # uint8     储存图像使用的数据类型
plt.imshow(image_rgb)
plt.show()

insert image description here

image save

In OpenCV, you can use the cv2.imwrite(dir,img) function to save images.
The first parameter dir is the location where the image is stored.
The second parameter img is the image object.
This function is used to save the ndarray (numpy array) object into an image file and return the image result. By default, this function saves as an 8-bit single-channel BGR image.

In matplotlib, you can use the matplotlib.pyplot,imsave(dir,img,**kwargs) function to save images.
The first parameter dir is the image storage location.
The second parameter img is the image object.
The third parameter is a dictionary parameter with more content. The following summarizes several commonly used parameter values.

  • format: Specifies the image format, the possible formats are png, jpg, svg, etc., and supports most image formats.
  • dpi: resolution, used to adjust the sharpness of the image
  • cmap: color mapping, this parameter is ignored for color images, only valid for grayscale images.

Case: Saving images with OpenCV

import numpy as np
import cv2
import matplotlib.pyplot as plt

# 使用OpenCV保存 uint8 类型的图像
image_array = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[255, 255, 0], [255, 0, 255], [0, 255, 255]],
    [[255, 255, 255], [128, 128, 128], [0, 0, 0]],
], dtype=np.uint8)
cv2.imwrite("./opencv_imwrite.jpg", image_array)

# 读取保存的unit8类型的图像
image = cv2.imread('./opencv_imwrite.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(image)
plt.show()

Saved image:
insert image description here
Image displayed after reading:
insert image description here

image_array_2 = np.array([
    [[1,0,0],[0,1,0],[0,0,1]],
    [[1,1,0],[1,0,1],[0,1,1]],
    [[1,1,1],[0.5,0.5,0.5],[0,0,0]],
],dtype=np.float64)
cv2.imwrite('./opencv_imwrite2.jpg',image_array_2)

#读取保存的float64图像
image = cv2.imread('./opencv_imwrite2.jpg')
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
plt.imshow(image)
plt.show()

Image displayed:
insert image description here

Save images with matplotlib

Here is only one demonstration: save the image of uint8 type, the method of saving other types of images is the same

import numpy as np
from matplotlib import pyplot as plt

# 使用Matplotlib保存uint8类型的图像
image_array = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[255, 255, 0], [255, 0, 255], [0, 255, 255]],
    [[255, 255, 255], [128, 128, 128], [0, 0, 0]],
], dtype=np.uint8)

plt.imsave('./matplotlib_imwrite.jpg', image_array)

# 读取保存的uint8类型的图像
image = plt.imread('./matplotlib_imwrite.jpg')
plt.imshow(image)
plt.show()

Image read after saving:
insert image description here

Guess you like

Origin blog.csdn.net/fuhao6363/article/details/129637130