簡體   English   中英

這個opencv-python代碼有什么錯誤?

[英]what is the error in this opencv-python 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()

運行此代碼后,它將出現以下錯誤:

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'

誰能告訴我這是什么錯誤?

您正在嘗試調整作為內核的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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM