I am trying to display a png on a frame, which comes from my webcam, with opencv. How do I do this?
The png:
Code:
import cv2
cam= cv2.VideoCapture(0)
wounded_img= cv2.imread("media/wounded.png", 0)
while True:
ret, frame = cam.read()
# How do i add wounded.png to the bottom of this frame?
cv2.imshow("Cam",frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cam.release()
cv2.destroyAllWindows()
CodePudding user response:
This is somewhat a trivial stuff, but tedious so I will just be explaining how it should work.
Your "frame" variable should be a ??? x ??? x 3 numpy array (and also the wounded_img variable as well.) (Each ??? x ??? are B,G,R channels.) You can simply overwrite parts of the "frame" variable array with values from your "wounded_img" array.
You may consider resizing first by cv2.resize though.
Edit: Here is a sample code. You can adapt it as you may. I've basically just resized and truncated the image you wish to add, replace a part of the array in the image by the image you are trying to add.
You can do this for every frame.
Here is an example:
import cv2 as cv
import numpy as np
#For resizing image
def rescaleFrame(frame, scale=0.75):
width = int(frame.shape[1] * scale)
height = int(frame.shape[0] * scale)
dimensions = (width,height)
return cv.resize(frame,dimensions,interpolation=cv.INTER_AREA)
img = cv.imread('cat.jpg')
wounded_img =cv.imread('wounded.png')
wounded_img_rescaled = rescaleFrame(wounded_img,0.3)
#I checked while debugging that this is the range that removes the black background (if that was what you are looking for)
wounded_img_rescaled_cut = wounded_img_rescaled[82:116,117:282]
img[img.shape[0] - 34:img.shape[0],img.shape[1]-165:img.shape[1]] = wounded_img_rescaled_cut
#34 and 165 came from the height and width of the image being added
cv.imshow('With Wounded Logo',img)
cv.waitKey(0)
CodePudding user response:
I got this code from somewhere else, and i modified it. This works.
import cv2
# load the overlay image. size should be smaller than video frame size
img = cv2.imread('media/wounded.png')
# Get Image dimensions
img_height, img_width, _ = img.shape
# Start Capture
cap = cv2.VideoCapture(0)
# Get frame dimensions
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH )
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT )
# Print dimensions
print('image dimensions (HxW):',img_height,"x",img_width)
print('frame dimensions (HxW):',int(frame_height),"x",int(frame_width))
# Decide X,Y location of overlay image inside video frame.
# following should be valid:
# * image dimensions must be smaller than frame dimensions
# * x img_width <= frame_width
# * y img_height <= frame_height
# otherwise you can resize image as part of your code if required
x = 50
y = 50
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# add image to frame
frame[ y:y img_height , x:x img_width ] = img
# Display the resulting frame
cv2.imshow('frame',frame)
# Exit if ESC key is pressed
if cv2.waitKey(20) & 0xFF == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()