Crowd Counting读取data及density map

crowd counting data为图像;density map采用mat、csv文件进行存储;

Task: 读取图像、并进行图像大小及density map大小的调整(调整为512*384);

image格式大小为1024*768,density map也在该数据大小上进行标注;

1、读取图像及调整大小

        longest_size = 512 

        img = Image.open(img_path).convert('RGB')
        w, h = img.size # 用于计算图像大小调整的比例

        if w > h:
            factor = w / longest_size
            img = img.resize((longest_size, int(h / factor)), Image.BICUBIC) # 调整
        else:
            factor = h / longest_size
            img = img.resize((int(w / factor), longest_size), Image.BICUBIC)

2、读取.mat density map及调整大小

        gt_path = 'xxx.mat'

        target = sio.loadmat(gt_path)
        ground_truth = target['map']

        ground_truth = ground_truth.astype(np.float32, copy=False)  # 1080*1920
        ground_truth = Image.fromarray(ground_truth) # 实现array到image的转换

        if w > h: # 为代码1确定的图像尺寸
            ground_truth = np.array(ground_truth.resize((longest_size, int(h / factor)), Image.BICUBIC)) * factor * factor # 对密度图中数值进行扩大,扩大为factor*factor
        else:
            ground_truth = np.array(ground_truth.resize((int(w / factor), longest_size), Image.BICUBIC)) * factor * factor

3、读取.csv文件中的density map

        gt_path = "xxx.csv"
        ground_truth = np.loadtxt(gt_path, delimiter=',')

4、读取.h5文件中的density map

        import h5py

        gt_path = ".h5"

        f = h5py.File(gt_path, 'r')
        ground_truth = np.asarray(f['density'])

5、数据增强:

        den = np.fliplr(den)  # 将数组在左右方向上翻转
        img = img.transpose(Image.FLIP_LEFT_RIGHT)  # 将图像进行左右对换

        if self.mode == 'train':
            if random.random() > 0.8:
                target = np.fliplr(target)
                img = img.transpose(Image.FLIP_LEFT_RIGHT)

            # resizing target based on the dataset
            target = cv2.resize(target, (int(target.shape[1]), int(target.shape[0])), interpolation=cv2.INTER_CUBIC) * 64
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值