Ask Your Question
1

Using a webcam, how do I keep a drawing function in one window from appearing on to another window?

asked 2018-01-19 01:53:21 -0500

masterenol gravatar image

image description

In the picture on the right, I have a window named 'frame' which shows a green square and a cyan circle. On the left, there is a window named 'binary' which shows the square and circle in white.

The binary window shows in white objects that are moving.

What can I do so that the drawing functions do not show in the 'binary' window and only stay in the 'frame' window?

I'd greatly appreciate any help I can receive.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

ret, last_frame = cap.read()

if last_frame is None:
    exit()

while(cap.isOpened()):
    ret, frame = cap.read()

    if frame is None:
        exit()

    binary = cv2.absdiff(last_frame, frame)
    binary = cv2.cvtColor(binary, cv2.COLOR_BGR2GRAY) 
    ret, binary = cv2.threshold(binary,50,255,cv2.THRESH_BINARY) 

    #Rectangle
    frame = cv2.rectangle(frame,(0,0),(320,240),(0,255,0),3)

    #Circle
    vRow, vCol, vCH = frame.shape
    cirR = 30
    cirRowPos = int(vRow/2)
    cirColPos = int(vCol-cirR) - int(vCol*0.05)
    frame = cv2.circle(frame,(cirColPos, cirRowPos), cirR, (255,255,0), 3)

    cv2.imshow('frame', frame)
    cv2.imshow('binary', binary)

    if cv2.waitKey(33) >= 0:
        break

    last_frame = frame

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-01-19 02:24:59 -0500

berak gravatar image

updated 2018-01-19 02:26:06 -0500

it's quite a puzzle here, but what happens:

  • you draw something into your frame
  • then you assign it to last_frame (unfortunately, with the drawing in it)
  • in the next round, you absdiff last_frame to frame, so this already has your previous drawing in it !

remedy: you need to copy to last_frame before you draw into it, also, it needs a "deep copy", not just an assignment:

 binary = cv2.absdiff(last_frame, frame)
 binary = cv2.cvtColor(binary, cv2.COLOR_BGR2GRAY) 
 ret, binary = cv2.threshold(binary,50,255,cv2.THRESH_BINARY) 

 last_frame = frame.copy()

 # now draw into your frame, as you like !
edit flag offensive delete link more

Comments

1

Cool! I deleted the line 'last_frame = frame" and I added "last_frame = frame.copy()" before my drawing functions.Thank you so much @berak!

masterenol gravatar imagemasterenol ( 2018-01-19 17:27:09 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-19 01:53:21 -0500

Seen: 191 times

Last updated: Jan 19 '18