Мне не удалось найти контур и нарисовать вокруг него прямоугольник, используя это видео. Ссылка: http://www.filedropper.com/outcutscaledВозникли проблемы с функцией cv2.findContours()
Первоначально, я попытался найти контур, просто используя функцию cv2.findContours()
, но он показывал несколько точек. Затем я попытался применить некоторые морфологические преобразования, но до сих пор не повезло, поскольку контур не является правильным.
Не могли бы вы рассказать мне, что я делаю неправильно?
P.S Я использую cv2.bitwise_and()
для устранения теней. Можно также использовать cv2.bitwise_or()
.
Это мой код: версия
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture('out_cut_scaled.mp4')
fgbg = cv2.BackgroundSubtractorMOG2(history=50, varThreshold=50)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
while True:
result=diffImg(t_minus, t, t_plus)
#frame = cv2.resize(result, (20, 20))
#fgmask = result
fgmask = cv2.blur(result, (10, 10))
fgmask = fgbg.apply(fgmask)
fgmask = cv2.medianBlur(fgmask, 7)
#ret, thresh = cv2.threshold(result, 127, 255, 0)
thresh = cv2.dilate(fgmask, None, iterations=2)
#thresh = cv2.erode(thresh, None, iterations=2)
"""
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#cv2.drawContours(thresh, contours, -1, (0, 255, 0), 3)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if w > 10 and h > 10:
cv2.rectangle(thresh, (x, y), (x + w, y + h), (0, 255, 0), 2)
print len(contours)
"""
cv2.imshow(winName, thresh)
cv2.imshow("frame", t)
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
OpenCV: версия 2.4.13
Python: 2.7