cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:1

When using opencv to read images, you may encounter the following error message:

cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:1

This problem is most likely caused by a problem with the file path.
PS: The reason for the error has not been checked on the official website. The following analysis is only based on experiments.

Possible causes and solutions:

There is Chinese in the image path:

This just requires checking the path and changing it.

It is recommended that the naming convention of files and folders be consistent with the naming convention of python.

There is a problem with the file (picture)

Files with wrong file names
filename include file extensions, which should be in the form flower.jpgof flower.png, rather than simply flower.

IDE configuration:
If your code is migrated, this problem may occur because the IDE you are using is different from the original code.

Jupyter notebook
If you are using Jupyter notebook. The file reading path of Jupyter notebook is from the folder where the code is located as the root directory by default.
In short, if the code and pictures are in the same directory, you can use cv.imread('flower.jpg').
If the code and the "img" folder are in the same directory, this is the form:

├─opencv_learn.py
├─img
│  ├─flower.jpg

You need to write like this: cv.imread("img/flower.jpg").

VScode
The file reading path of VScode generally starts from the current workspace (the first-level folder of the resource manager) as the root directory. That is, the folder you created (opened).

For example, as shown below: Demo
The folder with the serial number "1" is opened at this time, the code is the file in the serial number "3", and the cv2.imread("test.jpg",1)read path is a simple picture name, so it should be done from the folder with the serial number "1" Search, but test.jpgthe file cannot be found in the first-level folder , so an error will be reported.

If the folder you open (that is, the first-level folder) is changed to the folder where the serial number "3" is located - "code_001", then you will most likely have no problem running the code. VScode will search from the folder opened at this time (that is, code_001).

In VScode, if the code reading method you migrated is in cv.imread("flower.jpg")the form of simple image names, or we like to use this reading method, but we also want to classify the code and store it into multi-level directories. We can make simple settings:

  1. Click Run => Open configuration (if not, click Add configuration )Insert image description here
  2. At this time, a new file will be .vscodeadded to the folder launch.json. We can add a cwdparameter configuration, just copy and paste as follows:
"cwd": "${fileDirname}"

Insert image description here

Note the comma at the end of the previous line ,.

This configuration means that you need to write code as in Jupyter notebook, that is: the file to be read in your code is in the same folder as your current code . Your code will be almost impossible to write because at this point you have configured the folder where your code is located as the root directory.
cv2.imread(./python/code_001/test.jpg",1)

Guess you like

Origin blog.csdn.net/m0_67313306/article/details/127263410