在分割通道,对分割的结果(分别进行模糊处理)并对组成部分应用拉普拉斯运算之后,我尝试对image执行霍夫圆操作。

img = cv2.imread(fileName)


b, g, r = cv2.split(img)
kSize = 3
gBlur = cv2.blur(g, (kSize, kSize))
print(gBlur.shape)
test = np.array([])
gEdges = cv2.Laplacian(gBlur, cv2.CV_64F)
circles = cv2.HoughCircles(gEdges, cv2.HOUGH_GRADIENT, 40, 10,
                        param1=50,param2=60,minRadius=2,maxRadius=15)

但是,我从OpenCV收到一个错误,指出:
OpenCV(3.4.1) Error: Assertion failed (!_image.empty() &&
_image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) &&
(_image.isMat() || _image.isUMat())) in HoughCircles, file
/io/opencv/modules/imgproc/src/hough.cpp, line 1659


cv2.error: OpenCV(3.4.1) /io/opencv/modules/imgproc/src/hough.cpp:1659:
error: (-215) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1))
+ (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function
HoughCircles

我不明白为什么会这样。图像为灰度。我还尝试过将Laplacian操作中的数据类型更改为cv2.CV_16U,cv2.CV_8U甚至。但是这些会导致图像空白-并且opencv会因此引发不同的错误。

最佳答案

您在cv2.Laplacian中提到的图像类型就是问题所在。另一件事是您的代码未完成。这是完整的代码。

import cv2
import numpy as np
img = cv2.imread('stack1.png')

b, g, r = cv2.split(img)
kSize = 3
gBlur = cv2.blur(g, (kSize, kSize))
print(gBlur.shape)
test = np.array([])
gEdges = cv2.Laplacian(gBlur, cv2.CV_8UC1)
circles = cv2.HoughCircles(gEdges, cv2.HOUGH_GRADIENT, 40, 10, param1=50,param2=60,minRadius=2,maxRadius=15)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - OpenCV Python拆分-> Laplacian-> HoughCircles =断言失败。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50731170/

10-13 07:04