简体   繁体   English

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

[英]what is the error in this opencv-python code?

I have tried to execute the following code 我试图执行以下代码

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()

After running this code, it gives the following error: 运行此代码后,它将出现以下错误:

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'

Can anyone please tell me what is the mistake? 谁能告诉我这是什么错误?

You are trying to resize g_kernel which is a kernel. 您正在尝试调整作为内核的g_kernel的大小。 cv2.resize needs an image to be resized. cv2.resize需要调整图像大小。 If you want to resize the filtered_img then change the following line in your code: 如果要调整filtered_img的大小,请在代码中更改以下行:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM