当前位置:网站首页>AttributeError: module ‘skimage. draw‘ has no attribute ‘circle‘

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

2022-06-22 10:06:00 Evergreen cypress

Project scenario and problem description :

Tips : Here is a brief introduction to the background of the project : I wrote a function myself , To draw a list of key positions in the image , A circle . The code originally passed the test , But I can't use it on the server today , Report errors :AttributeError: module 'skimage.draw' has no attribute 'circle'
Locate the error code location

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


Cause analysis :

Tips : Fill in the analysis of the problem here : Mainly caused by version changes .

I reverse position , Find the specific location :rr, cc = draw.circle(coor[1], coor[0], 4) Find out circle Function cannot be connected . Find the source code of the package , Find out __init__.py and draw.py It's all gone from the file circle function :
 Insert picture description here
My version is different , original scikit-image 0.15.0 Of draw.py There are also... In the document circle function , Currently in use scikit-image 0.19.3 Has deleted circle function .
This is a lower version :
 Insert picture description here


Solution :

Tips : Fill in the specific solution to the problem here :

1. Since it is caused by the version upgrade , Of course, you can return the version , Install the old version :pip install scikit-image==0.15.0, This is a more earthy way .
2. Method 2 Well , We should have such a sense , It can't be upgraded and lose the ability to draw circles , therefore , We found the source code of the old version , You'll find that ,circle The essence of a function is an elliptic function .

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)

It looks like a lot of content , Are all notes. , There is only one useful sentence :return ellipse(r, c, radius, radius, shape).
therefore , We modify the code :draw.circle(r, c, radius, shape=None) by draw.ellipse(r, c, radius,radius, shape=None). The parameters of the function are also changed from the original 4 A into 5 individual .
 Insert picture description here
radius,radius = a, b
A circle is also a special ellipse , Round a=b.

The modified code :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()

It works perfectly :
 Insert picture description here

原网站

版权声明
本文为[Evergreen cypress]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220938239293.html