Ask Your Question

Revision history [back]

Difference between two captures

Hi, i am trying to detect if two captures separated by 1 second are differents using an IP cam. The code i use is :

import cv2

camera = 'http://192.168.0.50:8081' # CAM 800x600 60fps

cap = cv2.VideoCapture(camera)

while cap.isOpened():

    ret, frame = cap.read()

    if ret == True:        

        cv2.imshow('Direct', frame)            

        k = cv2.waitKey(1)

        if k%256 == 27: # ESC presse

            print("Escape, fermeture...")

            break

        framecount = 0

        capture = frame

        while True:

            framecount += 1           

            #print(framecount)

            if framecount == 600:

                _, frame1 = cap.read()

                framecount = 0

                capture1 = frame1

                break        

        if frame.shape == frame1.shape:

            print("Les caracteristiques sont identiques")

            difference = cv2.subtract(capture, capture1)

            b, g, r = cv2.split(difference)

            b1 = cv2.resize(b, (400, 300) , interpolation = cv2.INTER_AREA)                  
            cv2.imshow('B ', b1)

            g1 = cv2.resize(g, (400, 300) , interpolation = cv2.INTER_AREA)                  
            cv2.imshow('G ', g1) 

            r1 = cv2.resize(b, (400, 300) , interpolation = cv2.INTER_AREA)                  
            cv2.imshow('R ', r1)

            print(cv2.countNonZero(b))
            print(cv2.countNonZero(g))
            print(cv2.countNonZero(r))


        if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:

            print('Les images sont identiques.')

        else:

            print('Les images sont differentes.')

            difference1 = cv2.resize(difference, (400, 300) , interpolation = cv2.INTER_AREA)       

            cv2.imshow('Difference ', difference1)  

    else:

        cap.release()
        cv2.destroyAllWindows()

An idea why it gives a bad answer and it says that they are different when they are the same?

Best regards.

Jacques