繁体   English   中英

读取带有斑点检测的相机 - TypeError: 'numpy.ndarray' object is not callable

[英]Reading camera with blob detection - TypeError: 'numpy.ndarray' object is not callable

我正在尝试从视频捕获中获取图像并通过斑点检测器和轮廓检测运行它。 我可以让它从 png 中工作,但如果可能的话,真的需要从视频捕获中进行处理。

导致错误的行是cnt_idx = np.squeeze(np.where(hier[0, :, 3] == b_idx)) *

def camera(frame):
    frame_blurred = cv2.medianBlur(frame, 7)
    hsv = cv2.cvtColor(frame_blurred, cv2.COLOR_BGR2HSV)
    lower_green = np.array([0,49,255])
    upper_green = np.array([179,227,255])
    masked = cv2.inRange(hsv, lower_green, upper_green)
    kernel = np.ones((3,3),np.uint8)
    opening = cv2.dilate(masked,kernel, iterations=2)

    #####################
    ### Circle Filter ###
    #####################
    height, width, depth = frame.shape
    mask = np.zeros((height,width),np.uint8)
    cv2.circle(mask, (int(round(width/2)), int(round(height/2))), 200, 1, -1)
    blob = cv2.bitwise_and(opening, opening, mask = mask)
    
      
    # keypoints = detector.detect(MaskedImage)
    # blob = cv2.drawKeypoints(MaskedImage, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    
    cv2.imshow("masked",blob)

# Read image; add an additional hole; find contours with hierarchy
    # blob = (frame, cv2.IMREAD_GRAYSCALE)
    cv2.circle(blob, (380, 120), 25, 0, cv2.FILLED)
    contours, hier = cv2.findContours(blob(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # Define sufficient enough colors for blobs
    colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]

    # Get blob indices with respect to hierarchy
    blob_idx = np.squeeze(np.where(hier[0, :, 3] == -1))

    # Initialize blob images
    blob_imgs = []

    # Iterate all blobs
    k = 0
    for b_idx in np.nditer(blob_idx):

        # Add outer contour of blob to list
        blob_cnts = [contours[b_idx]]

        # Add inner contours of blob to list, if present
        **cnt_idx = np.squeeze(np.where(hier[0, :, 3] == b_idx))**
        if (cnt_idx.size > 0):
            blob_cnts.extend([contours[c_idx] for c_idx in np.nditer(cnt_idx)])

        # Generate blank BGR image with same size as input; draw contours
        img = np.zeros((blob.shape[0], blob.shape[1], 3), np.uint8)
        cv2.drawContours(img, blob_cnts, -1, colors[k], 2)
        blob_imgs.append(img)

以下代码似乎是您的问题:

contours, hier = cv2.findContours(blob(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

将其更改为:

contours, hier = cv2.findContours(blob.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

请记住 findContours 在二值图像上效果最好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM