Skip to content

Instantly share code, notes, and snippets.

@nunenuh
Created July 16, 2018 18:43
Show Gist options
  • Save nunenuh/f9eb1b971011d8a7ca270829fdfb84dc to your computer and use it in GitHub Desktop.
Save nunenuh/f9eb1b971011d8a7ca270829fdfb84dc to your computer and use it in GitHub Desktop.
AutoCrop center
import numpy as np
import cv2
def center_rectangle(frame):
h,w,d = frame.shape
x1,y1 = h//4, w//4
x2,y2 = h-x1, w-y1
rr = cv2.rectangle(frame,(y1,x1),(y2,x2),(0,255,0),3)
crop = frame[x1:x2, y1:y2]
return rr
def crop_center(frame):
h,w,d = frame.shape
x1,y1 = h//4, w//4
x2,y2 = h-x1, w-y1
crop = frame[x1:x2, y1:y2]
return crop
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rr = center_rectangle(frame)
crop = crop_center(frame)
cv2.imshow('frame',rr);
cv2.imshow('crop area',crop)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment