用OpenCV讀取影象資料
img_bgr
=
cv2
。
imread
(
image_dir
)
失敗的原因:
1、路徑中不能有中文
2、影象的名字不能有中文
3、絕對路徑呼叫方式,要雙反斜槓
image_dir
=
‘D:
\\
Documents
\\
GitHub
\\
my_OpenCV
\\
note_Machine Learning for OpenCV
\\
data
\\
longmao。jpg’
img_bgr
=
cv2
。
imread
(
image_dir
)
4、絕對路徑呼叫方式,要加‘r’
image_dir=r‘D:\Documents\GitHub\my_OpenCV\note_Machine Learning for OpenCV\data\longmao。jpg’
img_bgr = cv2。imread(image_dir)
5、(6月7號)親試有些參考書的呼叫方式,發現不行,比如:
image_dir=‘data/longmao。jpg’
(6月8號)又可以了,不知道為什麼?(中間晚上的時候電腦重啟了)
讀取失敗時的報錯:
print(img_bgr) 的結果是none
plt。imshow(img_bgr)的結果報錯並顯示空白圖
TypeError: Image data cannot be converted to float
下面是我調通的正確程式碼
import
cv2
import
matplotlib。pyplot
as
plt
%
matplotlib
inline
image_dir
=
‘D:
\\
Documents
\\
GitHub\my_OpenCV
\\
note_Machine Learning for OpenCV
\\
data
\\
longmao。jpg’
img_bgr
=
cv2
。
imread
(
image_dir
)
(
img_bgr
)
plt
。
imshow
(
img_bgr
)
執行環境為:OpenCV 3。4。1 、python 3。5。2 、Windows 7
IDE:jupyter lab
請酌情參考!