OpenCV Python Split - >拉普拉斯算子 - > HoughCircles =断言失败。

时间:2018-06-06 23:49:59

标签: python opencv

我尝试在分割通道后对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

我不明白为什么会这样。图像为灰度。我也尝试将拉普拉斯算子运算中的数据类型改为cv2.CV_16U,cv2.CV_8U;但是这会导致空白图像 - 而opencv会导致不同的错误。

1 个答案:

答案 0 :(得分:1)

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