import cv2
import imutils
def resize_crop_image(image,target_width,target_height):
(h, w) = image.shape[:2]
dH =0
dW=0
if w < h:
image = imutils.resize(image, width=target_width,
inter=cv2.INTER_AREA)
dH = int((image.shape[0] - target_height) / 2.0)
else:
image = imutils.resize(image, height=target_height,
inter=cv2.INTER_AREA)
dW = int((image.shape[1] - target_width) / 2.0)
(h, w) = image.shape[:2]
image = image[dH:h - dH, dW:w - dW]
return cv2.resize(image, (target_width, target_height),interpolation=cv2.INTER_AREA)
if __name__ =="__main__":
image_path = "F:\\yu_data\\002_a.jpg"
image = cv2.imread(image_path)
image_resize = cv2.resize(image,(512,512), cv2.INTER_AREA)
img = resize_crop_image(image,512,512)
cv2.namedWindow("input",cv2.WINDOW_NORMAL)
cv2.namedWindow("input_1",cv2.WINDOW_AUTOSIZE)
cv2.namedWindow("input_2",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input",image)
cv2.imshow("input_1",image_resize)
cv2.imshow("input_2",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
处理方法:沿图像宽高最短尺寸调整大小,然后以中心为原点裁剪图像