When cropping an image with python's openCV, you have to specify the image index as below:
import cv2
img = cv2.imread("hoge.jpg ")
trimmedImg = img[0 : 50, 0: 50]
With this, you can't tell where you are trimming just by looking at it. (I was) So I made a trimming function like cv2.rectangle (). Please use it.
trim.py
import cv2
def trim(img, pt1, pt2):
return img[pt1[1]:pt2[1], pt1[0]:pt2[0]]
Enter pt1 and pt2 like this.
trim.py
import cv2
def trim(img, position, width, height):
x, y = position
return img[y:y+height, x:x+width]
Or
trim.py
import cv2
def trim(img, x, y, width, height):
return img[y:y+height, x:x+width]
The input looks like this.
Recommended Posts