构建代码
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
def get_three_box(img_shape):
if img_shape[0] > 2*img_shape[1]:
a = (img_shape[1], img_shape[0])
circle_point = (int(a[0]/2),int(a[1]/2))
x_inch = int(a[0]*0.45)
y_inch = int(a[1]*0.14)
dy = int(a[1]*0.015)
box_middle = (circle_point[0]-x_inch, circle_point[1]-y_inch+2*dy, circle_point[0]+x_inch, circle_point[1]+y_inch+2*dy)
box_up = (box_middle[0], 4*dy, box_middle[2], box_middle[1])
box_down = (box_middle[0], box_middle[3], box_middle[2], a[1]-4*dy)
return box_up, box_middle, box_down
else:
a = (img_shape[1], img_shape[0])
circle_point = (int(a[0]/2),int(a[1]/2))
x_inch = int(a[0]*0.14)
y_inch = int(a[1]*0.45)
dx = int(a[0]*0.015)
box_middle = (circle_point[0]-x_inch+6*dx, circle_point[1]-y_inch, circle_point[0]+x_inch+2*dx, circle_point[1]+y_inch)
box_up = (6*dx, box_middle[1], box_middle[0], box_middle[3])
box_down = (box_middle[2], box_middle[1], a[0]-6*dx, box_middle[3])
return box_up, box_middle, box_down
def cut_three_box_from_tl(img):
box_up, box_middle, box_down = get_three_box(img.shape)
img_up = img[box_up[1]:box_up[3], box_up[0]:box_up[2]]
img_middle = img[box_middle[1]:box_middle[3], box_middle[0]:box_middle[2]]
img_down = img[box_down[1]:box_down[3], box_down[0]:box_down[2]]
return img_up, img_middle, img_down
def resize_three_image(img, height, width):
box_up, box_middle, box_down = get_three_box(img.shape)
img_up = img[box_up[1]:box_up[3], box_up[0]:box_up[2]]
img_middle = img[box_middle[1]:box_middle[3], box_middle[0]:box_middle[2]]
img_down = img[box_down[1]:box_down[3], box_down[0]:box_down[2]]
img_up = cv2.resize(img_up,(height,width), interpolation=cv2.INTER_CUBIC)
img_middle = cv2.resize(img_middle,(height,width), interpolation=cv2.INTER_CUBIC)
img_down = cv2.resize(img_down,(height,width), interpolation=cv2.INTER_CUBIC)
return img_up, img_middle, img_down
执行代码
img = mpimg.imread('./test_images/100.jpg')
img_up, img_middle, img_down = cut_three_box_from_tl(img)
img_up1, img_middle1, img_down1 = resize_three_image(img, 20, 30)
fig = plt.gcf()
fig.set_size_inches(12, 7)
plt.subplot(241)
plt.imshow(img)
plt.title('original_img1')
plt.subplot(242)
plt.imshow(img_up)
plt.title('img_up')
plt.subplot(243)
plt.imshow(img_middle)
plt.title('img_middle')
plt.subplot(244)
plt.imshow(img_down)
plt.title('img_down')
plt.subplot(246)
plt.imshow(img_up1)
plt.title('img_up1')
plt.subplot(247)
plt.imshow(img_middle1)
plt.title('img_middle1')
plt.subplot(248)
plt.imshow(img_down1)
执行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/50b609f187f3c25ab0291d382fe716d2.png#pic_center)