python - cv2.HoughCircles 中的图像加载问题

标签 python python-3.x opencv hough-transform

我正在使用 float32 类型的 128 x 128 数组。这些数组是从二进制文件中提取的,我试图在每个数组中定位磁盘。

当我尝试使用 HoughCircles 示例代码时:

img = Image.fromarray(fa)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

我收到以下错误:

File "", line 2, in cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

TypeError: src is not a numpy array, neither a scalar



如果我不使用 Image.fromarray 转换为图像,我会收到以下错误,表明我仍然没有使用正确的类型。

File "", line 1, in circles = cv2.HoughCircles(fa,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)

error: OpenCV(3.4.4) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/hough.cpp:1736: error: (-215:Assertion failed) !_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()) in function 'HoughCircles'

最佳答案

Houghcircles 采用 8 位、单 channel 、灰度输入图像。你的数组是 128 x 128 类型的 float32 数组。所以尝试改变类型。

并在此操作之前和之后显示您的图像以进行完整性检查

img = Image.fromarray(fa)

关于python - cv2.HoughCircles 中的图像加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53766240/

相关文章:

python - 从 Facebook 数据导出中解码看似格式错误的 Unicode 指针的 utf8 表示形式

python - 如何在文件浏览器中使用动态模块?

opencv - 将OpenCV IplImage或Mat复制到Freeimage FIBITMAP中

python - cPickle.PicklingError : Could not serialize object: NotImplementedError

python - 纸笔角色扮演游戏中骰子概率的动态规划

python - 3D矩阵透视变换

python - 如何打印谷歌搜索结果的数量(Beautifulsoup)

python - 正则表达式:匹配字符串开头以外的字符

c++ - OpenCV:C++ 和 C 性能比较

opengl - 在 OpenGL 中动态改变纹理