17 lines
797 B
Python
Raw Normal View History

2024-07-29 11:43:52 +08:00
import cv2
def crop_top(img, percent=0.15): #对图像的上部进行裁剪,去除不必要信息
offset = int(img.shape[0] * percent)
return img[offset:]
def central_crop(img): #裁剪为长宽相等且居中
size = min(img.shape[0], img.shape[1])
offset_h = int((img.shape[0] - size) / 2)
offset_w = int((img.shape[1] - size) / 2)
return img[offset_h:offset_h + size, offset_w:offset_w + size] #切片表示
def process_image_file(filepath, size, top_percent=0.08, crop=True):
img = cv2.imread(filepath) #返回三维列表 长 宽 通道
img = crop_top(img, percent=top_percent)
if crop:
img = central_crop(img)
img = cv2.resize(img, (size, size)) #默认双线性插值 调整长宽大小为size
return img