opencv-python[cv2] reads Chinese path images

foreword

With the continuous development of AI artificial intelligence, the technology of image processing is becoming more and more important. Many schools have opened the course of image processing for undergraduates, and learning image processing development naturally cannot avoid opencv-python[ cv2 ] Intel-led open source library.

1. What is cv2?

cv2 refers to the Python interface library of OpenCV. OpenCV (Open Source Computer Vision Library) is an open source computer vision library that provides a wealth of image processing and computer vision algorithms. The cv2 library is the official Python interface of OpenCV, which enables developers to use the Python language to call various functions and functions in the OpenCV library. The cv2 library provides various image processing and computer vision functions, including image reading and saving, image transformation and filtering, edge detection, feature extraction, target detection, image recognition, etc. With the cv2 library, developers can use Python to write concise and powerful image processing and computer vision applications.

2. Problem description

Many students who have just learned opencv will encounter the problem of failure to read the picture of the Chinese path when using python's opencv-python[cv2]. Obviously the image file exists, but the program fails to run alive. The root cause is cv2 .imread does not support Chinese paths .

1.1 Problem recurrence

First of all, there is indeed a "kitten and puppy.jpg" under the image file path, as shown in the figure below.
insert image description here
The code is as follows (directly call cv.imread):

import cv2

image_file_name= './图像/小猫小狗.jpg'
image_numpy_data = cv2.imread(image_file_name)

cv2.imshow('显示图像', image_numpy_data)

cv2.waitKey(0)

When we configure the python environment and run main.py, the following error message appears.

cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

This error is to tell the developer that the image file is None . If new students don’t know how to configure the environment, they can refer to the [ Anaconda3 and PyCharm Installation and Configuration Nanny Tutorial ] written by the blogger.

1.2 Bug fixes

Since cv2.imread cannot be read directly, we can read it indirectly through numpy. Here is a repaired code for your reference.

import cv2
import numpy as np

# 通过numpy读取中文路径图像
def image_read_from_chinese_path(image_file_name):
    image_numpy_data = cv2.imdecode(np.fromfile(image_file_name, dtype=np.uint8), -1)
    #返回numpy的ndarray
    return image_numpy_data

image_file_name = './图像/小猫小狗.jpg'
image_numpy_data = image_read_from_chinese_path(image_file_name)

cv2.imshow('image', image_numpy_data)
cv2.waitKey(0)

Running again, imshow successfully displays the image
insert image description here

1.3 Summary

The solution of this article is through the numpy library, the np.fromfile() function is used to read the original data of the image file and store it in a NumPy array. Then, use the cv2.imdecode() function to decode the NumPy array into image data in OpenCV format.

By using the numpy library, you can avoid the limitation of the cv2.imread() function on the Chinese path, and successfully read the image file containing Chinese characters.

conclusion

Due to the limited ability of bloggers, the methods mentioned in this article will inevitably have omissions. I hope you can enthusiastically point out the mistakes, so that the next revision can be presented to everyone in a more perfect and rigorous manner. before.

Guess you like

Origin blog.csdn.net/weixin_40280870/article/details/131259846