电子说
深度学习数据预处理中常用opencv读入图片,一般在__getitem__
函数中调用。本文主要介绍opencv读取图片的一些细节以及注意事项。本文中使用的图片见第6节“opencv测试使用的图片”。
通过Jupyter Notebook运行OpenCV,运行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,再重新安装。( 未解决,可直接转下述方法 )
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
报错:内核崩溃。
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
img_path = "00001.png" # 图片路径
mask_path = "00001_matte.png" # 对应的图像分割标签路径
%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()
全部0条评论
快来发表一下你的评论吧 !