Ask Your Question
0

Difference between two captures

asked 2020-06-01 05:18:00 -0600

JSAM gravatar image

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

edit retag flag offensive close merge delete

Comments

1

if cv2.countNonZero(b) == 0

basically -- this is unreasonable

(camera noise)

berak gravatar imageberak ( 2020-06-01 14:29:23 -0600 )edit

Thanks, i add a blur filter and it's better but not perfect….

JSAM gravatar imageJSAM ( 2020-06-02 04:14:07 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-06-01 14:14:02 -0600

mvuori gravatar image

Did you test the images in an image editor that they really are the same? You could compare an image with itself. Two captures from camera would not be absolutely same. There will be some differences: noise, lights flicker, camera vibrates... You need to allow some tolerance.

edit flag offensive delete link more

Comments

Thanks, i change the code because i don't think about the parameters you give me :

 if frame.shape == frame1.shape:

            print("Les caracteristiques sont identiques")

            diff = cv2.subtract(capture, capture1)

            diff1 = cv2.blur(diff, (5, 5))

            imask =  diff1 > 14

            canvas = np.zeros_like(capture1, np.uint8)

            canvas[imask] = capture1[imask]

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

It's better.

JSAM gravatar imageJSAM ( 2020-06-02 04:16:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-01 05:18:00 -0600

Seen: 1,359 times

Last updated: Jun 01 '20