Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to perspective-warp the input image before motion-detection #153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
warp image, optionally
  • Loading branch information
jeremybmerrill committed Nov 8, 2023
commit a81b93fc6e3842745b82b190c5dcdc770ae06e23
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
MO_CROP_X_RIGHT = 250 # Default=250
MO_CROP_Y_UPPER = 90 # Default=90
MO_CROP_Y_LOWER = 150 # Default=150
MO_WARP_ON = False # Default False; cv2.warpPerspective using 4+ control points defined below.
# on my 4gb Rpi 4, this costs ~0.17 fps
MO_WARP_INPUT_PTS = [[188,345], [179,411], [489,436], [489,363]]
MO_WARP_OUTPUT_PTS = [[179,363],[179,436], [489,436], [489,363]]

# Plugins override the specified config.py variable settings
# ----------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions speed-cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
"MO_CROP_X_RIGHT": 250,
"MO_CROP_Y_UPPER": 90,
"MO_CROP_Y_LOWER": 150,
"MO_WARP_ON": False,
"MO_WARP_INPUT_PTS": [1,2,3,4],
"MO_WARP_OUTPUT_PTS": [1,2,3,4],
"CAMERA": "pilibcam",
"CAM_LOCATION": "Front Window",
"USBCAM_SRC": 0,
Expand Down Expand Up @@ -1151,6 +1154,9 @@ def get_motion_contours(grayimage1):
image = vs.read() # Read image data from video steam thread instance
# crop image to motion tracking area only
try:
if MO_WARP_ON:
M = cv2.getPerspectiveTransform(np.float32(MO_WARP_INPUT_PTS),np.float32(MO_WARP_OUTPUT_PTS))
image = cv2.warpPerspective(image,M,(image.shape[1], image.shape[0]),flags=cv2.INTER_NEAREST)
image_crop = image[MO_CROP_Y_UPPER:MO_CROP_Y_LOWER, MO_CROP_X_LEFT:MO_CROP_X_RIGHT]
image_ok = True
except (ValueError, TypeError):
Expand Down Expand Up @@ -1302,6 +1308,9 @@ def speed_camera():
# initialize a cropped grayimage1 image
image2 = vs.read() # Get image from VideoSteam thread instance
try:
if MO_WARP_ON:
M = cv2.getPerspectiveTransform(np.float32(MO_WARP_INPUT_PTS), np.float32(MO_WARP_OUTPUT_PTS))
image2 = cv2.warpPerspective(image2,M,(image2.shape[1], image2.shape[0]),flags=cv2.INTER_NEAREST)
# crop image to motion tracking area only
image_crop = image2[MO_CROP_Y_UPPER:MO_CROP_Y_LOWER, MO_CROP_X_LEFT:MO_CROP_X_RIGHT]
except:
Expand Down