完整代码如下
import cv2
import numpy as np
from scipy import ndimage
kernel133 = np.array([
[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]
])
kernel133_D = np.array([
[1, 1, 1],
[1, -8, 1],
[1, 1, 1]
])
img = cv2.imread('jj.jpg')
img = ndimage.convolve(img, kernel133_D)
cv2.imshow('enhanced', img)
cv2.waitKey(0)
完整的报错如下
Traceback (most recent call last):
File "D:/Python/测试专用文件夹/opencv测试.py", line 19, in
img = ndimage.convolve(img, kernel133_D)
File "C:\Users\yjc\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\ndimage\filters.py", line 745, in convolve
origin, True)
File "C:\Users\yjc\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\ndimage\filters.py", line 595, in _correlate_or_convolve
raise RuntimeError('filter weights array has incorrect shape.')
RuntimeError: filter weights array has incorrect shape.
通过这个文章,我知道了,cv2.imread要加一个额外的参数
img = cv2.imread('jj.jpg', cv2.IMREAD_GRAYSCALE)
或者
img = cv2.imread('jj.jpg', 0)#0表示加载一张灰度图。
OpenCV_Python API 官方文档学习_ cv2.imread()
cv2.IMREAD_COLOR : 默认使用该种标识。加载一张彩色图片,忽视它的透明度。
cv2.IMREAD_GRAYSCALE : 加载一张灰度图。
cv2.IMREAD_UNCHANGED : 加载图像,包括它的Alpha通道。