In OpenCV I've got a mask of an area of a frame. How would I then insert another image into that location on the original frame?
<p>I'm brand new to OpenCV and I can't seem to find a way to do this (It probably has to do with me not knowing any of the specific lingo).</p>

<p>I'm looping through the frames of a video and pulling out a mask from the video where it is green-screened using inRange. I'm looking for a way to then insert an image into that location on the original frame. The code i'm using to pull the frames/mask is below.</p>

<pre><code>import numpy as np
import cv2

cap = cv2.VideoCapture('vid.mov')
image = cv2.imread('photo.jpg')


# green digitally added not much variance
lower = np.array([0, 250, 0])
upper = np.array([10, 260, 10])

while(True):
  # Capture frame-by-frame
  ret, frame = cap.read()
  cv2.imshow('frame', frame)

  # get mask of green area
  mask = cv2.inRange(frame, lower, upper)
  cv2.imshow('mask', mask)

  if cv2.waitKey(1) &amp; 0xFF == ord('q'):
      break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
</code></pre>

<p>Use Bitwise operations for masking and related binary operations. Please check below code to see how Bitwise operations are done.</p>

<p><strong>Code</strong></p>

<pre><code>import numpy as np
import cv2

cap = cv2.VideoCapture('../video.mp4')
image = cv2.imread('photo.jpg')


# green digitally added not much variance
lower = np.array([0, 250, 0])
upper = np.array([10, 260, 10])

while(True):
  # Capture frame-by-frame
  ret, frame = cap.read()
  cv2.imshow('frame', frame)

  # get mask of green area
  mask = cv2.inRange(frame, lower, upper)
  notMask = cv2.bitwise_not(mask)
  imagePart=cv2.bitwise_and(image, image, mask = mask)
  videoPart=cv2.bitwise_and(frame, frame, mask = notMask)
  output = cv2.bitwise_or(imagePart, videoPart)
  cv2.imshow('output', output)

  if cv2.waitKey(1) &amp; 0xFF == ord('q'):
      break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
</code></pre>

<p><strong>RGB bad color space</strong></p>

<p>Since, you are doing color processing, I would suggest you to use appropriate color space. HSV would be a good start for you. For finding good range of HSV values, try this <a href="https://github.com/saurabheights/ImageProcessingExperimentScripts/blob/master/AnalyzeHSV/hsvThresholder.py" rel="nofollow">script</a>.</p>

<p><strong>Generating Video Output</strong>
You need a to create a video writer and once all image processing is done, write the processed frame to a new video. I am pretty sure, you cannot read and write to same video file.</p>

<p>Further see official docs <a href="http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html" rel="nofollow">here</a>.</p>

<p>It has both examples of how to read and write video.</p>

python opencv video