OpenCV:元组没有属性副本

时间:2018-04-02 12:34:54

标签: python opencv bounding-box

我试图在图像附近生成边界框,但是获取元组没有属性错误。图像的轮廓传递给“blobbify”,返回blob的轮廓。

这是代码

# erodes image based on given kernel size (erosion = expands black areas)
def erode( img, kern_size = 3 ):
    retval, img = cv2.threshold(img, 254.0, 255.0, cv2.THRESH_BINARY) # threshold to deal with only black and white.
    kern = np.ones((kern_size,kern_size),np.uint8) # make a kernel for erosion based on given kernel size.
    eroded = cv2.erode(img, kern, 1) # erode your image to blobbify black areas
    y,x = eroded.shape # get shape of image to make a white boarder around image of 1px, to avoid problems with find contours.
    return cv2.rectangle(eroded, (0,0), (x,y), (255,255,255), 1)

# finds contours of eroded image
def prep( img, kern_size = 3 ):    
    img = erode( img, kern_size )
    retval, img = cv2.threshold(img, 200.0, 255.0, cv2.THRESH_BINARY_INV) #   invert colors for findContours
    return cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # Find Contours of Image

# given img & number of desired blobs, returns contours of blobs.
def blobbify(img, num_of_labels, kern_size = 3, dilation_rate = 10):
    print(type(img))
    prep_img, contours, hierarchy = prep( img.copy(), kern_size ) # dilate img and check current contour count.
    while len(contours) > num_of_labels:
        kern_size += dilation_rate # add dilation_rate to kern_size to increase the blob. Remember kern_size must always be odd.
        previous = (prep_img, contours, hierarchy)
        processed_img, contours, hierarchy = prep( img.copy(), kern_size ) # dilate img and check current contour count, again.
    if len(contours) < num_of_labels:
        return (processed_img, contours, hierarchy)
    else:
        return previous

# finds bounding boxes of all contours
def bounding_box(contours):
    bBox = []
    for curve in contours:
        box = cv2.boundingRect(curve)
    bBox.append(box)
    return bBox

我收到了这个错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-155-5f2b07e02ca2> in <module>()
     43 e_i = erode(img,kern_size=3)
     44 c_i = prep(e_i)
---> 45 b_i = blobbify(c_i,num_of_labels=5,kern_size=3,dilation_rate=10)
     46 bbox_img = bounding_box(b_i)
     47 

<ipython-input-155-5f2b07e02ca2> in blobbify(img, num_of_labels, kern_size, dilation_rate)
     19 def blobbify(img, num_of_labels, kern_size = 3, dilation_rate = 10):
     20     print(type(img))
---> 21     prep_img, contours, hierarchy = prep( img.copy(), kern_size ) # dilate img and check current contour count.
     22     while len(contours) > num_of_labels:
     23         kern_size += dilation_rate # add dilation_rate to kern_size to increase the blob. Remember kern_size must always be odd.

AttributeError: 'tuple' object has no attribute 'copy'

1 个答案:

答案 0 :(得分:0)

c_i是图像,轮廓和层次结构的元组。

所以在第45行,如果你想传递图像,你必须指定c_i [0]