欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

crop and resize image

程序员文章站 2022-05-18 14:49:15
...
目标

利用Python版的OpenCV,根据2D bounding box , crop图像。

以下假设2D bouding box 的格式为:[xmin , ymin , w,  h],以[30 30 72 42]为例。

代码
import cv2
bb2d = [30, 30, 72 ,42]
image = cv2.imread('car.png')
pt1 = (int(bb2d[0]), int(bb2d[1]))
pt2 = (int(bb2d[0]) + int(bb2d[2]), int(bb2d[1]) + int(bb2d[3]))
cv2.rectangle(image, pt1, pt2, (0,0,255))
cv2.imshow('original',image)
image = cv2.imread('car.png')
image_cropped = image[int(bb2d[1]):int(bb2d[1]) + int(bb2d[3]),
                      int(bb2d[0]):int(bb2d[0]) + int(bb2d[2])]
cv2.imshow('cropped',image_cropped)
image_resized = cv2.resize(image_cropped,(224,224))
cv2.imshow('resized',image_resized)
cv2.waitKey(0)
运行结果

crop and resize image



参考:

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html

https://docs.opencv.org/3.0-beta/modules/imgproc/doc/geometric_transformations.html?highlight=resize#cv2.resize