深度學(xué)習(xí)數(shù)據(jù)預(yù)處理中常用opencv讀入圖片,一般在__getitem__
函數(shù)中調(diào)用。本文主要介紹opencv讀取圖片的一些細(xì)節(jié)以及注意事項。本文中使用的圖片見第6節(jié)“opencv測試使用的圖片”。
1.如何通過opencv顯示圖片?
通過Jupyter Notebook運(yùn)行OpenCV,運(yùn)行cv2.imshow()時報錯。
import cv2
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
cv2.imshow("img", img)
報錯:
---------------------------------------------------------------------------
error Traceback (most recent call last)
Input In [60], in 1>()
----> 1 cv2.imshow("img", img)
error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/window.cpp:1267: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
|
解決方法:先卸載opencv,再重新安裝。( 未解決,可直接轉(zhuǎn)下述方法 )
conda remove opencv
conda install -c menpo opencv
pip install --upgrade pip
pip install opencv-contrib-python
參考鏈接:https://blog.csdn.net/tudou2013goodluck/article/details/108402055
報錯:內(nèi)核崩潰。
Kernel Restarting
The kernel for Untitled.ipynb appears to have died. It will restart automatically.
解決方法 :opencv讀取圖片,之后使用matplotlib庫顯示圖片。
下述兩個代碼均可以:
%matplotlib inline
from matplotlib import pyplot as plt
import cv2
img = cv2.imread('圖片路徑')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
import cv2
def cv2_imshow(a, **kwargs):
a = a.clip(0, 255).astype('uint8')
# cv2 stores colors as BGR; convert to RGB
if a.ndim == 3:
if a.shape[2] == 4:
a = cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA)
else:
a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
return plt.imshow(a, **kwargs)
參考鏈接:https://github.com/jupyter/notebook/issues/3935
2.opencv讀入圖片默認(rèn)為BGR
img_path = "00001.png" # 圖片路徑
mask_path = "00001_matte.png" # 對應(yīng)的圖像分割標(biāo)簽路徑
%matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np
img_bgr = cv2.imread(img_path, cv2.IMREAD_COLOR)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR->RGB
plt.imshow(img)
plt.show()
-
OpenCV
+關(guān)注
關(guān)注
30文章
628瀏覽量
41273 -
深度學(xué)習(xí)
+關(guān)注
關(guān)注
73文章
5493瀏覽量
120998
發(fā)布評論請先 登錄
相關(guān)推薦
評論