构建代码
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+2*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
执行代码
img = mpimg.imread('./test_images/114.jpg')
img_up, img_middle, img_down = cut_three_box_from_tl(img)
fig = plt.gcf()
fig.set_size_inches(6, 5)
plt.subplot(221)
plt.imshow(img)
plt.axis('off')
plt.title('original_img1')
plt.subplot(222)
plt.imshow(img_up)
plt.axis('off')
plt.title('img_up')
plt.subplot(223)
plt.imshow(img_middle)
plt.axis('off')
plt.title('img_middle')
plt.subplot(224)
plt.imshow(img_down)
plt.axis('off')
plt.title('img_down')
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/82b9d04bc356faa4d4d142966802d7ed.png#pic_center)