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

博客讲述了在使用scikit-image库时遇到的问题,由于版本升级,draw.circle函数不再可用。作者通过查看源码发现circle已被替换为ellipse。为保持功能,代码被修改为使用draw.ellipse,并解释了圆可以视为a=b的特殊椭圆。通过这个修改,程序成功运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景和问题描述:

提示:这里简述项目相关背景:自己写了一个函数,要在图像中画出一些列关键点的位置,画圆。代码本来是测试通过了的,但是今天换到服务器上就不能用了,报错: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()

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

AttributeError: module 'skimage' has no attribute 'io'是一个错误提示,它意味着在使用skimage库时,没有找到io模块。 要解决这个问题,可以尝试以下几个方法: 1. 检查skimage库的版本。确保你使用的是最新版的skimage库,因为io模块可能在较旧的版本中不存在。 2. 确保正确导入了skimage库。可以使用`import skimage`或`from skimage import io`来导入skimage库,并确保没有拼写错误。 3. 检查skimage库的安装。如果你没有安装skimage库,可以使用pip或conda来安装,例如`pip install scikit-image`。 4. 如果以上方法都没有解决问题,可能是因为skimage库的安装有问题。你可以尝试重新安装skimage库,或者考虑使用其他类似的库来替代。 总结一下,要解决AttributeError: module 'skimage' has no attribute 'io'的问题,你可以检查skimage库的版本和安装情况,确保正确导入了库,并考虑重新安装或使用其他库来替代。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [成功解决AttributeError: moduleskimagehas no attribute ‘io](https://blog.csdn.net/qq_41185868/article/details/121643945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [AttributeError: moduleskimage.drawhas no attributecircle](https://blog.csdn.net/beauthy/article/details/125374312)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏常青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值