Python ROI crop
程序员文章站
2022-03-13 10:54:22
...
from PIL import Image
im = Image.open('F:/test.jpg')
# 图片的宽度和高度
img_size = im.size
print("图片宽度和高度分别是{}".format(img_size))
left = 80
upper = 210
right = 505
lower = 640
region = im.crop((left,upper,right,lower))
region.save("crop_test.jpg")
box = (100, 100, 400, 400)
region = im.crop(box)
s1 = cv2.imread('roi.jpg', 0)
print(s1.shape)
height, width = s1.shape
#cv2.imread() ---- Image.open()
cv2.imread() ---- Image.open()
In the Python bindings of OpenCV, images are represented as NumPy arrays in BGR order. This works fine when using the cv2.imshow function. However, if you intend on using Matplotlib, the plt.imshow function assumes the image is in RGB order. A simple call to cv2.cvtColor will resolve this problem, or you can use the opencv2 matplotlib convenience function.