Ask Your Question

rbs90's profile - activity

2016-01-06 02:25:29 -0600 answered a question Detect open door with traincascade?

I was also interessted in detecting a door state with OpenCV. My best result was using the floodfill algorithm:

import cv2
from numpy import *

test_imgs = ['night_open.jpg', 'night_closed.jpg', 'day_open.jpg', 'day_closed.jpg']

for imgFile in test_imgs:
    img = cv2.imread(imgFile)
    height, width, channels = img.shape
    mask = zeros((height+2, width+2), uint8)

    #the starting pixel for the floodFill
    start_pixel = (510,110)
    #maximum distance to start pixel:
    diff = (2,2,2)

    retval, rect = cv2.floodFill(img, mask, start_pixel, (0,255,0), diff, diff)

    print retval

    #check the size of the floodfilled area, if its large the door is closed:
    if retval > 10000:
        print imgFile + ": garage door closed"
    else:
        print imgFile + ": garage door open"

    cv2.imwrite(imgFile.replace(".jpg", "") + "_result.jpg", img)

Programm output:

681
night_open.jpg: garage door open
19802
night_closed.jpg: garage door closed
639
day_open.jpg: garage door open
19847
day_closed.jpg: garage door closed

Result images:

day closed day open night closed night open

2016-01-06 02:15:32 -0600 received badge  Supporter (source)