cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182

Call cvtColorthe processing image of the OpenCV module:

img_rgb = r'G:\手机照片2022\IMG_122440.jpg'
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

An error occurred, and the content of the error is:

[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('G:\手机照片2022\IMG_122440.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
  File "G:\Test39.py", line 26, in <module>
    control_to_sketch(image, to_image)
  File "G:Test39.py", line 10, in control_to_sketch
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Reason for error: The warning on line 1 actually gives the answer, that is 路径问题, the file cannot be opened or read, and the path ( ) needs to be changed can't open/read file: check file path/integrity.
Solution:

  • Method 1: Change the name of the folder involving Chinese in the path to a non-Chinese name, such as:
img_rgb = r'G:\mobile_photo_2022\IMG_122440.jpg'
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  • Method 2: Create a new folder with an English name, and put the files in this folder for testing.

Guess you like

Origin blog.csdn.net/qq_45476428/article/details/127287685