遮罩前景提取和背景减法

时间:2019-05-30 09:39:20

标签: python opencv 3d

我正在尝试从背景中屏蔽前景,并尝试了所有openCV函数(MOG,MOG2,KNN),分段,检测以被屏蔽而不是从前景(羽毛)关闭,但是没有任何作用。

我希望背景为黑色,白色的蒙面前景完全填充为白色。

阈值增加会填充对象蒙版吗?如果可以,怎么办? TIA

我尝试了所有openCV函数(MOG,MOG2,KNN),分割和检测,以使边界线不从前景(羽毛)闭合,但是没有作用。

# load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])

# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255,
    cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# loop over the contours
for c in cnts:
    # compute the bounding box of the contour and then draw the
    # bounding box on both input images to represent where the two
    # images differ
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

# show the output images
cv2.imshow("Original", imageA)
cv2.imshow("Modified", imageB)
cv2.imshow("Diff", diff)
cv2.imshow("Thresh", thresh)
cv2.waitKey(0)

我希望脱粒机的输出能填满整个对象,但实际输出不是完全填满蒙版

Output

0 个答案:

没有答案