Use python and opencv to resize the image to the target size without changing the image ratio by edge filling

guide

Recently, I am working on a project that needs to use the coordinate changes of key points of the face as features. However, it was found that after the previous face registration, cropping, and resize preprocessing, the image's aspect ratio has changed, and the face in it has been distorted. The key points detected on this distorted face are definitely inaccurate, let alone the later analysis. Therefore, there is a need for a method to resize the target image to the target size while ensuring that the aspect ratio of the image remains unchanged. I chose the edge filling method, and I will introduce it in detail below, and attach the code and effect for everyone to use and watch.

principle

Nothing high-tech hahaha, roughly divided into the following steps:

  • Calculate the ratio of the width and height of the original image to the width and height of the target image, and then take the smaller ratio as our resize ratio ;
  • Use the resize ratio obtained above to multiply the length and width of our original image, and obtain the image size after resize while retaining the aspect ratio of the original image ;
  • Since we choose a smaller ratio to scale the image, the size of the newly obtained image is smaller than the size of the target image. In order to meet the size requirements of the target image, we calculate the width and height difference between the current image and the target image, and Fill with pixels with a value of 0 , and get an image of the target size after filling.

After explaining what's going on, let's take a look at the code. The words are unfounded, and the code is used to see the effect.

the code

import cv2
import matplotlib.pyplot as plt

# 封装resize函数
def resize_img_keep_ratio(img_name,target_size):
    img = cv2.imread(img_name) # 读取图片
    old_size= img.shape[0:2] # 原始图像大小
    ratio = min(float(target_size[i])/(old_size[i]) for i in range(len(old_size))) # 计算原始图像宽高与目标图像大小的比例,并取其中的较小值
    new_size = tuple([int(i*ratio) for i in old_size]) # 根据上边求得的比例计算在保持比例前提下得到的图像大小
    img = cv2.resize(img,(new_size[1], new_size[0])) # 根据上边的大小进行放缩
    pad_w = target_size[1] - new_size[1] # 计算需要填充的像素数目(图像的宽这一维度上)
    pad_h = target_size[0] - new_size[0] # 计算需要填充的像素数目(图像的高这一维度上)
    top,bottom = pad_h//2, pad_h-(pad_h//2)
    left,right = pad_w//2, pad_w -(pad_w//2)
    img_new = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0)) 
    return img_new

if __name__ == "__main__":
	img = r'D:\SAMM\crop\006_1\1.jpg' # 待处理的图片地址, 替换成你的地址就好
	target_size=[224, 224] # 目标图像大小
	resized_img = resize_img_keep_ratio(img, target_size) 
	plt.imshow(resized_img)
	plt.show()

running result

Summary of insert image description here
the original image of the image after resizeinsert image description here

This article mainly introduces a method of using python and opencv to resize the image to the target size without changing the aspect ratio of the original image through edge filling. partner. If you feel that it is helpful to you, remember to give it a thumbs up~~ If you have any questions or comments, welcome to the comment area to exchange~

Guess you like

Origin blog.csdn.net/Just_do_myself/article/details/118656543