如何使用python opencv显示房间的占用面积?

时间:2017-05-17 09:31:33

标签: python opencv

我想检查一下占用多少空间以及电梯或会议室剩余多少空间。以下我试过了

draw = False
ax, ay = -1, -1
roi = []
img = None
inputMode = False
template = []
def onclick(event, x, y, flags, param):
    global roi, img, inputMode,template
    if event == cv2.EVENT_RBUTTONDBLCLK:
        cv2.setMouseCallback("frame", draw_area)
        inputMode = True
        orig = img.copy()
        cv2.imshow("frame", img)
        cv2.waitKey(0)
        rf = open("RoiValue.txt", 'w')
        pickle.dump(roi, rf);
        rf.close()
        rf = open("RoiValue.txt", 'r');
        roi = pickle.load(rf);
        rf.close()
        for r in roi:
            (x1, y1, x2, y2) = r
            newtemplate = orig[y1:y2, x1:x2, :]
            template.append(cv2.Canny(newtemplate, 50, 50))

def draw_area(event, x, y, flags, param):
    global ax, ay, draw, roi, img
    if event == cv2.EVENT_LBUTTONDOWN:
        draw = True
        ax, ay = x, y
        cv2.imshow('frame',  cv2.line(img, (ax,ay), (x,y), (0,0,255), 2))
    elif event == cv2.EVENT_LBUTTONUP:
        draw = False
        roi.append((ax, ay, x, y));
        cv2.imshow('frame', cv2.rectangle(img, (ax, ay), (x, y), (255, 0, 0), 2))


def main():
    global img, roi,template
    camera = cv2.VideoCapture("video1.mp4")
    while True:
        grabbed, img = camera.read()
        cv2.setMouseCallback("frame", onclick)
        i = 0;
        if i % 150 == 0:
            for spot in range(0, len(roi)):
                (x1, y1, x2, y2) = roi[spot]
                new = img[y1:y2, x1:x2, :]
                newVal = cv2.Canny(new, 50, 50)
                sub = cv2.absdiff(template[spot], newVal)
                # cv2.imshow('SUB',sub)
                norm = sub / 255;
                thresh = 5;
                threshold = norm.size * thresh / 100;
                if sum(sum(norm)) > threshold:
                    # Todo:Occupied
                    cv2.rectangle(img, (x1, y1), (x2, y2), [0, 0, 255], 2)
        cv2.imshow('frame', img)
        key = cv2.waitKey(25)
        if (0xFF & key == 27) | (grabbed == False):
            break
        i = i + 1
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
if __name__ == "__main__":
    main()

从上面的代码中我没有得到所需的输出。我想要计算占用空间的百分比。提前谢谢/

0 个答案:

没有答案