目标检测中图片预处理之图片大小分析

2020-05-20  本文已影响0人  犬夜叉写作业

获取图片大小,并绘制成散点图:


import time
import matplotlib.image as mping
import os
import matplotlib.pyplot as plt

#获取单个图片的大小
def get_Image_Size(image_Path):
    #print("获取图片尺寸大小:"+ image_Path)
    try:
        lena = mping.imread(image_Path)
    except:
        time.sleep(0.5)
        print("读图出错,重读")
        getPngSize(image_Path)
    size = tuple(lena.shape)
    #print(size)
    return size[1],size[0]

#遍历目标文件夹下图片的大小,保存到list中
def get_dir_list(image_path):
    w_list = []
    h_list = []
    files = os.listdir(image_path)
    for i in files:
        if i.endswith('.jpg'):
            w,h = get_Image_Size(image_path + i)
            #print(w,h)
            w_list.append(w)
            h_list.append(h)
    return w_list,h_list

#画散点图
def plot_pic(w_list,h_list):
    fig = plt.figure()
    ax1 = fig.add_subplot(1, 1, 1)
    ax1.set_title('DataSets\' Size Analysis')
    ax1.scatter(w_list, h_list, s=5, c='k', marker='.')
    plt.show()



if __name__ == '__main__':
    
    image_path = "./13/"  #目标文件夹,也就是图片存放的目录
    w_list,h_list = get_dir_list(image_path)
    plot_pic(w_list,h_list)

运行结果:


image.png
上一篇下一篇

猜你喜欢

热点阅读