I have a reference image, which I use to compare the camera feed to. (For example, data/reference.jpg.
) From the subtraction result, is there a way to tell how much your camera feed’s current frame resembles the reference image?
Code example:
# OpenCV library.
import cv2
# Import Pi camera.
from picamera.array import PiRGBArray # Generates a 3D RGB array
from picamera import PiCamera # Gives usage to Pi Camera
# Resolution of the camera capture & display.
res_w = 640
res_h = 480
resolution = (res_w, res_h)
# Initialize the camera.
camera = PiCamera()
# Set the camera resolution.
camera.resolution = resolution
# Set the number of frames per second.
camera.framerate = 30
# Generates a 3D RGB array and stores it in rawCapture.
raw_capture = PiRGBArray(camera, size=resolution)
# Continues capture of frames.
cap = camera.capture_continuous(raw_capture, format="bgr", use_video_port=True)
# Capture frames continuously from the camera.
for frame in cap:
# Grab the raw NumPy array representing the image.
image = frame.array
# Path to the reference image.
reference_path = "data/reference.jpg"
# If the reference doesn't exist, take a picture.
if not path.isfile(reference_path):
# Write the initial frame to a file.
cv2.imwrite(reference_path, image)
# Reference image for detection.
reference = cv2.imread(reference_path)
# Subtract the two images from each other.
subtraction = reference - image
# Show the image (with lines).
cv2.imshow("Output", subtraction)
# Wait for keyPress for 1 millisecond.
key = cv2.waitKey(1) & 0xFF
# Clear the stream in preparation for the next frame.
raw_capture.truncate(0)
# If the `q` key was pressed, break from the loop.
if key == ord("q"):
quit()
I’m using picamera
so excuse me about the unusual capture method. Also, as you can see subtraction
is the variable I want to get this data from, but any other solution to the problem will be appreciated.