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