这个opencv-python代码有什么错误?

时间:2019-03-09 12:54:05

标签: python-3.x opencv

我尝试执行以下代码

import numpy as np
import cv2
g_kernel = cv2.getGaborKernel((21, 21), 8.0, np.pi/4, 10.0, 0.5, 0, ktype=cv2.CV_32F)

img = cv2.imread('H:\PyCharm WorkSpace\images\image-0.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
filtered_img = cv2.filter2D(img, cv2.CV_8UC3, g_kernel)

cv2.imshow('image', img)
cv2.imshow('filtered image', filtered_img)

h, w = g_kernel.shape[:2]
g_kernel = cv2.resize(g_kernel, (3*w, 3*h), interpolation=cv2.INTER_CUBIC)
cv2.imshow('gabor kernel (resized)', g_kernel)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行此代码后,会出现以下错误:

Traceback (most recent call last):
  File "H:/PyCharm WorkSpace/opencv.py", line 24, in <module>
  cv2.imshow('gabor kernel (resized)', g_kernel)
  cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window_w32.cpp:1230: error: 
  (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 
  'cvShowImage'

谁能告诉我这是什么错误?

1 个答案:

答案 0 :(得分:0)

您正尝试调整作为内核的g_kernel的大小。 cv2.resize需要调整图像大小。如果要调整filtered_img的大小,请在代码中更改以下行:

g_kernel = cv2.resize(g_kernel, (3*w, 3*h), interpolation=cv2.INTER_CUBIC)

收件人:

g_kernel = cv2.resize(filtered_img, (3*w, 3*h), interpolation=cv2.INTER_CUBIC)