AttributeError: module ‘skimage.draw‘ has no attribute ‘circle‘

项目场景和问题描述:

提示:这里简述项目相关背景:自己写了一个函数,要在图像中画出一些列关键点的位置,画圆。代码本来是测试通过了的,但是今天换到服务器上就不能用了,报错:AttributeError: module 'skimage.draw' has no attribute 'circle'
定位错误码位置

def showImageAndCoor(img, coords):
    for coor in coords:
        if coor[2] == -1:
            pass
        else:
            # print(coor)
            rr, cc = draw.circle(coor[1], coor[0], 4)
            draw.set_color(img, [rr, cc], [255, 0, 0])
    io.imshow(img)
    io.show()


原因分析:

提示:这里填写问题的分析:主要是版本变化引起的。

我反向定位,找到具体的位置:rr, cc = draw.circle(coor[1], coor[0], 4)发现circle函数不能连接。找到包的源码里,发现__init__.py和draw.py文件中都已经没有了circle函数:
在这里插入图片描述
原来我的版本不一样的,原来scikit-image 0.15.0的draw.py文件里还有circle函数,现在当前使用的scikit-image 0.19.3已经删除了circle函数。
这是低版本:
在这里插入图片描述


解决方案:

提示:这里填写该问题的具体解决方案:

1.既然是版本升级引起的,当然可以把版本退回去,安装老的版本:pip install scikit-image==0.15.0,这是一种比较土的办法。
2.方法2呢,我们应该有这样的意识,它变来变去不能应为升级还失去画圆的能力,所以,我们找到老版本的源码,你会发现,circle函数的本质是椭圆函数。

def circle(r, c, radius, shape=None):
    """Generate coordinates of pixels within circle.

    Parameters
    ----------
    r, c : double
        Centre coordinate of circle.
    radius : double
        Radius of circle.
    shape : tuple, optional
        Image shape which is used to determine the maximum extent of output
        pixel coordinates. This is useful for circles that exceed the image
        size. If None, the full extent of the circle is used.

    Returns
    -------
    rr, cc : ndarray of int
        Pixel coordinates of circle.
        May be used to directly index into an array, e.g.
        ``img[rr, cc] = 1``.

    Examples
    --------
    >>> from skimage.draw import circle
    >>> img = np.zeros((10, 10), dtype=np.uint8)
    >>> rr, cc = circle(4, 4, 5)
    >>> img[rr, cc] = 1
    >>> img
    array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
           [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
           [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    """
    return ellipse(r, c, radius, radius, shape)

看上去很多内容,都是注释,有用的只有一句:return ellipse(r, c, radius, radius, shape)
所以,我们修改代码:draw.circle(r, c, radius, shape=None)draw.ellipse(r, c, radius,radius, shape=None)。函数的参数也由原来的4个变成5个。
在这里插入图片描述
radius,radius = a, b
圆也是一种特殊的椭圆,圆的a=b

修改后的代码:rr, cc = draw.ellipse(coor[1], coor[0], 4, 4)

def showImageAndCoor(img, coords):
    for coor in coords:
        if coor[2] == -1:
            pass
        else:
            # print(coor)
            rr, cc = draw.ellipse(coor[1], coor[0], 4, 4)
            draw.set_color(img, [rr, cc], [255, 0, 0])
    io.imshow(img)
    io.show()

运行完美:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/beauthy/article/details/125374312