当前位置:网站首页>如何只将人体步态轮廓中的人给裁剪出来
如何只将人体步态轮廓中的人给裁剪出来
2022-07-20 13:57:00 【舞雩.】
如何只将人体步态轮廓中的人给裁剪出来
不想多说,看看就懂
总览:
import os
import numpy as np
from PIL import Imagedef get_cImg(path, size=64):
'''
剪切图片
:param path: 输入图片路径
:param cut_path: 剪切图片后的输出路径
:param size: 要剪切的图片大小
:return:
'''
save_path = path.replace('Fgmask', 'Cuted')
if not os.path.exists(save_path):
os.makedirs(save_path)img_list = os.listdir(path)
start = int(img_list[0].split('.')[0])
end = int(img_list[-1].split('.')[0])
for id in range(start, end):
img_name = str(id) + '.png'
img = Image.open(os.path.join(path, img_name))
image, flag = cut(img)
if not flag:
Image.fromarray(image).convert('L').resize((size, size)).save(os.path.join(save_path, img_name))
print(path + ' is Cuted!')def cut(image):
'''
通过找到人的最小最大高度与宽度把人的轮廓分割出来,、
因为原始轮廓图为二值图,因此头顶为将二值图像列相加后,形成一列后第一个像素值不为0的索引。
同理脚底为形成一列后最后一个像素值不为0的索引。
人的宽度也同理。
:param image: 需要裁剪的图片 N*M的矩阵
:return: temp: 裁剪后的图片 size*size的矩阵。flag:是否是符合要求的图片
'''
image = np.array(image)# 找到人的最小最大高度与宽度
height_min = (image.sum(axis=1) != 0).argmax()
height_max = ((image.sum(axis=1) != 0).cumsum()).argmax()
width_min = (image.sum(axis=0) != 0).argmax()
width_max = ((image.sum(axis=0) != 0).cumsum()).argmax()
head_top = image[height_min, :].argmax()
# 设置切割后图片的大小,为size*size,因为人的高一般都会大于宽
size = height_max - height_min
temp = np.zeros((size, size))# 将width_max-width_min(宽)乘height_max-height_min(高,szie)的人的轮廓图,放在size*size的图片中央
# l = (width_max-width_min)//2
# r = width_max-width_min-l
# 以头为中心,将width_max-width_min(宽)乘height_max-height_min(高,size)的人的轮廓图,放在size*size的图片中央
l1 = head_top-width_min
r1 = width_max-head_top
# 若宽大于高,或头的左侧或右侧身子比要生成图片的一般要大。则此图片为不符合要求的图片
flag = False
if size <= width_max-width_min or size//2 < r1 or size//2 < l1:
flag = True
return temp, flag
# centroid = np.array([(width_max+width_min)/2,(height_max+height_min)/2],dtype='int')
temp[:, (size//2-l1):(size//2+r1)] = image[height_min:height_max, width_min:width_max]return temp, flag
# 使用案例
if __name__ == "__main__":
path = 'Package/Fgmask/tds_nm_03' # 数据集路径
get_cImg(path)
核心代码:
def cut(image):
'''
通过找到人的最小最大高度与宽度把人的轮廓分割出来,、
因为原始轮廓图为二值图,因此头顶为将二值图像列相加后,形成一列后第一个像素值不为0的索引。
同理脚底为形成一列后最后一个像素值不为0的索引。
人的宽度也同理。
:param image: 需要裁剪的图片 N*M的矩阵
:return: temp: 裁剪后的图片 size*size的矩阵。flag:是否是符合要求的图片
'''
image = np.array(image)
# 找到人的最小最大高度与宽度
height_min = (image.sum(axis=1) != 0).argmax()
height_max = ((image.sum(axis=1) != 0).cumsum()).argmax()
width_min = (image.sum(axis=0) != 0).argmax()
width_max = ((image.sum(axis=0) != 0).cumsum()).argmax()
head_top = image[height_min, :].argmax()
# 设置切割后图片的大小,为size*size,因为人的高一般都会大于宽
size = height_max - height_min
temp = np.zeros((size, size))
# 将width_max-width_min(宽)乘height_max-height_min(高,szie)的人的轮廓图,放在size*size的图片中央
# l = (width_max-width_min)//2
# r = width_max-width_min-l
# 以头为中心,将width_max-width_min(宽)乘height_max-height_min(高,size)的人的轮廓图,放在size*size的图片中央
l1 = head_top-width_min
r1 = width_max-head_top
# 若宽大于高,或头的左侧或右侧身子比要生成图片的一般要大。则此图片为不符合要求的图片
flag = False
if size <= width_max-width_min or size//2 < r1 or size//2 < l1:
flag = True
return temp, flag
# centroid = np.array([(width_max+width_min)/2,(height_max+height_min)/2],dtype='int')
temp[:, (size//2-l1):(size//2+r1)] = image[height_min:height_max, width_min:width_max]
return temp, flag
枝干代码:
def get_cImg(path, size=64):
'''
剪切图片
:param path: 输入图片路径
:param cut_path: 剪切图片后的输出路径
:param size: 要剪切的图片大小
:return:
'''
save_path = path.replace('Fgmask', 'Cuted')
if not os.path.exists(save_path):
os.makedirs(save_path)
img_list = os.listdir(path)
start = int(img_list[0].split('.')[0])
end = int(img_list[-1].split('.')[0])
for id in range(start, end):
img_name = str(id) + '.png'
img = Image.open(os.path.join(path, img_name))
image, flag = cut(img)
if not flag:
Image.fromarray(image).convert('L').resize((size, size)).save(os.path.join(save_path, img_name))
print(path + ' is Cuted!')
使用教程:
# 使用案例
if __name__ == "__main__":
path = 'Package/Fgmask/tds_nm_03' # 数据集路径
get_cImg(path)
回顾:
import os
import numpy as np
from PIL import Imagedef get_cImg(path, size=64):
'''
剪切图片
:param path: 输入图片路径
:param cut_path: 剪切图片后的输出路径
:param size: 要剪切的图片大小
:return:
'''
save_path = path.replace('Fgmask', 'Cuted')
if not os.path.exists(save_path):
os.makedirs(save_path)img_list = os.listdir(path)
start = int(img_list[0].split('.')[0])
end = int(img_list[-1].split('.')[0])
for id in range(start, end):
img_name = str(id) + '.png'
img = Image.open(os.path.join(path, img_name))
image, flag = cut(img)
if not flag:
Image.fromarray(image).convert('L').resize((size, size)).save(os.path.join(save_path, img_name))
print(path + ' is Cuted!')def cut(image):
'''
通过找到人的最小最大高度与宽度把人的轮廓分割出来,、
因为原始轮廓图为二值图,因此头顶为将二值图像列相加后,形成一列后第一个像素值不为0的索引。
同理脚底为形成一列后最后一个像素值不为0的索引。
人的宽度也同理。
:param image: 需要裁剪的图片 N*M的矩阵
:return: temp: 裁剪后的图片 size*size的矩阵。flag:是否是符合要求的图片
'''
image = np.array(image)# 找到人的最小最大高度与宽度
height_min = (image.sum(axis=1) != 0).argmax()
height_max = ((image.sum(axis=1) != 0).cumsum()).argmax()
width_min = (image.sum(axis=0) != 0).argmax()
width_max = ((image.sum(axis=0) != 0).cumsum()).argmax()
head_top = image[height_min, :].argmax()
# 设置切割后图片的大小,为size*size,因为人的高一般都会大于宽
size = height_max - height_min
temp = np.zeros((size, size))# 将width_max-width_min(宽)乘height_max-height_min(高,szie)的人的轮廓图,放在size*size的图片中央
# l = (width_max-width_min)//2
# r = width_max-width_min-l
# 以头为中心,将width_max-width_min(宽)乘height_max-height_min(高,size)的人的轮廓图,放在size*size的图片中央
l1 = head_top-width_min
r1 = width_max-head_top
# 若宽大于高,或头的左侧或右侧身子比要生成图片的一般要大。则此图片为不符合要求的图片
flag = False
if size <= width_max-width_min or size//2 < r1 or size//2 < l1:
flag = True
return temp, flag
# centroid = np.array([(width_max+width_min)/2,(height_max+height_min)/2],dtype='int')
temp[:, (size//2-l1):(size//2+r1)] = image[height_min:height_max, width_min:width_max]return temp, flag
# 使用案例
if __name__ == "__main__":
path = 'Package/Fgmask/tds_nm_03' # 数据集路径
get_cImg(path)
边栏推荐
猜你喜欢
Redis - 管理工具 redis-cli 详解
This ide plug-in 3.0 makes you the most security aware programmer in the company
App automated test-2 Appium recording test cases
如何实现一个状态机?
解析jpeg解码格式
游戏合作伙伴专题:BreederDAO 与 MonkeyLeague 的合作拉开序幕
7.16 - 每日一题 - 408
App automated test -3 Appium element positioning and waiting
IT运维管理指什么?如何建立有效的IT运维管理系统?
Scala advanced (VIII): collection content summary (Part 2)
随机推荐
Redis - 管理工具 redis-cli 详解
查看磁盘空间占用情况
今日来介绍关于淘宝相似商品搜索的API接口
7.20 - 每日一题 - 408
V Uni app api[routing and jump, network request, data cache]
记录封装组件和项目中防抖的使用
Bubble sort and selection sort
[server data recovery] a data recovery case in which the storage raid6 array was paralyzed due to power failure
[vulnerability recurrence] Apache log4j2 Remote Code Execution Vulnerability
Scala advanced (VIII): collection content summary (Part 2)
In microservices * IML file deletion
Online communication - knowledge driven multi strategy and multi-modal question and answer Technology Practice (Qingyuan talk, issue 22, Wang haofen)
Explain the tax arrears with deduction: there is no tax arrears, and we will continue to operate in compliance as usual
APP自动化测试-1. Appium的安装与配置
Today's sleep quality record 73 points
Opportunities to segment industries
APP自动化测试-5.触屏操作及toast处理
labelme voc数据格式转yolo txt数据格式
Fork in multi process
IV Uni app component [view component, basic content (official self-contained, such as form class), UI component library, pit of component library]