OpenCV imread() image reading error solutions

After going through many difficulties, I was finally able to open the image file. It took five hours (the beautiful girl cried). I once suspected that my computer was broken and I was blind –■ – ■--… I hope this article can help people like
me Dabai brings good luck, please read on patiently!
First, let’s introduce the three methods of opening image files with OpenCV.
The correct opening methods are as follows:

#方法一
import cv2 as cv
import numpy as np
imgpath="D:\python\pictures/first.jpg";
img=cv.imread(imgpath,cv.IMREAD_ANYDEPTH)
cv.imshow("img",img)
cv.waitKey(0)

#方法二
import cv2 as cv
import numpy as np
imgpath="D:\python\pictures/first.jpg";
img=cv.imread(imgpath,cv.IMREAD_COLOR)
cv.imshow("img",img)
cv.waitKey(0)

#方法三
import cv2 as cv
import numpy as np
imgpath="D:\python\pictures/first.jpg";
img=cv.imread(imgpath,cv.IMREAD_GRAYSCALE)
cv.imshow("img",img)
cv.waitKey(0)

All three methods can be opened, only the suffixes are different.
Method one:
img=cv.imread(imgpath,cv.IMREAD_ANYDEPTH)
Method two:
img=cv.imread(imgpath,cv.IMREAD_COLOR)
Method three:
img=cv.imread (imgpath,cv.IMREAD_GRAYSCALE)

Next are the pitfalls you need to pay attention to. Don’t ask me how I know this. Ask me about the I Have a Friend series.
Tip 1: Check whether there are any errors in the most basic grammar. Although you can paste and copy, it is best to type it yourself to remember it better.

Tips 2: Chinese characters cannot appear in the image file path. I usually put the image file in the project directory to save trouble. Usually the first parameter of the imread function is usually the absolute path or relative path of the image file. For determining the path, imread does not support the single right slash form (\). When copying the path and pasting it into pycharm, an error will be reported. As shown in the figure,
Insert image description here
just replace " \ " with " / " to call it successfully.

Insert image description here

Tips 3: Pycharm needs to add OpenCV every time. For specific methods, please refer to my previous article: Pycharm imports and downloads successfully opencv.
If you have other convenient methods, you can tell me. Thank you very much and I will give you one!

Although the process was painful, I once felt that I was too stupid to compare with those big guys, but I was still very happy after finishing it!
Remember to leave three in a row for the beautiful girl--■ – ■–

Guess you like

Origin blog.csdn.net/CTaaaaa/article/details/126273981