如何在opencv中实现从遮罩去除阴影的代码?

时间:2019-05-05 10:42:25

标签: python-3.x opencv shadow-removal

我在opencv中使用MoG方法检测静态背景框架中的运动对象,但是它也检测阴影。我想去除遮罩上的阴影。我尝试对灰色使用阈值(因为阴影在蒙版中用灰色标记),但是阈值也删除了对象的灰色部分。我正在尝试实施https://pdfs.semanticscholar.org/53e0/7f60d03461def8ed4f765f2a6b7dfc4bfbd0.pdf 本文的算法。谁能告诉我如何在python中实现这一点?

import cv2
import numpy as np

cap = cv2.VideoCapture('TownCentreXVID.avi')
fgbg = cv2.createBackgroundSubtractorMOG2()

while(1):

    _, frame = cap.read()
    mask = fgbg.apply(frame)



    kernel = np.ones((5,5),np.uint8)
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

    window = cv2.namedWindow('Original', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
    window = cv2.namedWindow('Mask', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)
    window = cv2.namedWindow('Opening', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO )
    #window = cv2.namedWindow('Closing', cv2.WINDOW_NORMAL| cv2.WINDOW_KEEPRATIO)

    cv2.imshow('Original',frame)
    cv2.imshow('Mask',thresh)
    cv2.imshow('Opening',opening)
    #cv2.imshow('Closing',closing)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

1 个答案:

答案 0 :(得分:0)

如果您使用BackgroundSubtractorMOG2BackgroundSubtractorKNN背景减法器,则可以轻松地将ShadowValue设置为false0

mog2->setShadowValue(0);
// Or
knn->setShadowValue(0);

它会去除蒙版上的阴影。

阴影值= true

enter image description here

阴影值= false

enter image description here