import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread('test.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray, (3, 3), 0)
img_sobelx = cv2.Sobel(img_gaussian, cv2.CV_8U, 1, 0, ksize=5)
img_sobely = cv2.Sobel(img_gaussian, cv2.CV_8U, 0, 1, ksize=5)
img_sobel = img_sobelx + img_sobely
kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)
img_prewitt = img_prewittx + img_prewitty
cv2_imshow(img)
cv2_imshow(np.hstack((img_sobelx, img_sobely, img_sobel)))
cv2_imshow(np.hstack((img_prewittx, img_prewitty, img_prewitt)))
我不知道为什么会收到此错误?我要做的就是图像平滑/锐化。 我也得到这个警告(“ IPython.utils.traitlets已移至顶级traitlets包。”)
这是错误消息
/home/saurabh/anaconda3/lib/python3.7/site-packages/IPython/utils/traitlets.py:5:
UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package. warn("IPython.utils.traitlets has moved to a top-level traitlets package.")
Traceback (most recent call last): File "exp10.py", line 18, in <module> cv2_imshow(img) File "/home/saurabh/anaconda3/lib/python3.7/site-packages/google/colab/patches/__init__.py", line 29, in cv2_imshow display.display(PIL.Image.fromarray(a)) AttributeError: module 'PIL' has no attribute 'Image' –
答案 0 :(得分:2)
尝试将其添加到文件顶部
from PIL import Image