使用抓取算法也可以使用python opencv

时间:2018-12-13 09:14:47

标签: python opencv

这是我的编码。我尝试使用grabcut剪切图像的背景。但我无法获得实际结果。 请任何人解释我在此编码中需要调整的地方。 我只想要人类的物体,但输出我不会得到它 在此处输出图像。enter image description here  这是我的代码

image = cv2.imread(args["image"])
mask = np.zeros(image.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)

(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843, (300, 
300), 127.5)

print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()
for i in np.arange(0, detections.shape[2]):
    # extract the confidence (i.e., probability) associated with the
# prediction
    confidence = detections[0, 0, i, 2]

# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
    if confidence > args["confidence"]:
    # extract the index of the class label from the `detections`,
    # then compute the (x, y)-coordinates of the bounding box for
    # the object
        idx = int(detections[0, 0, i, 1])
        if CLASSES[idx] in IGNORE:
            continue
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype(int)
    # display the prediction
        label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)

        print("[INFO] {}".format(label))
        cv2.rectangle(image, (endX, endY), (startX, startY),
        COLORS[idx], 2)
    # crop = image[startY:endY + startY, startX:endX + startX]
        cv2.grabCut(image, mask, (startX, startY, endX, endY), bgdModel, 
        fgdModel, 5, cv2.GC_INIT_WITH_RECT)
        mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
         image_cut = image * mask2[:, :, np.newaxis]

0 个答案:

没有答案