Skip to content

Instantly share code, notes, and snippets.

@ChongyeWang
Created March 15, 2019 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChongyeWang/cfd473cdb9757271e5f14c7bde347f3b to your computer and use it in GitHub Desktop.
Save ChongyeWang/cfd473cdb9757271e5f14c7bde347f3b to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
image = cv2.imread('images/input.jpg')
height, width = image.shape[:2]
# Let's get the starting pixel coordiantes (top left of cropping rectangle)
start_row, start_col = int(height * .25), int(width * .25)
# Let's get the ending pixel coordinates (bottom right)
end_row, end_col = int(height * .75), int(width * .75)
# Simply use indexing to crop out the rectangle we desire
cropped = image[start_row:end_row , start_col:end_col]
cv2.imshow("Original Image", image)
cv2.waitKey(0)
cv2.imshow("Cropped Image", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment