现有的Crowding Counter任务数据集大小均不大,例如ShanghaiTech数据集的大小就几百张,因此为了增强训练的效果,通常会将数据集进行扩充,较为简单的方法就是将原始的图像进行局部裁剪,从而达到扩充的目的。
以下代码以ShanghaiTech的数据集为示例(不同示例数据集的.mat文件读取方法不同)
文件说明:
原始.mat文件放在groundPath/下;
原始图像放在imagePath/下;
因为将一张图像裁剪为9份,因此用for循环控制每一次的图像存储到单独的一个文件夹中;
def createImageAndMat():
originalIndex = [ # 从这些点开始裁剪矩阵
[50,50],[250,50],[400,50],
[50,100],[250,100],[400,100],
[50,250],[250,250],[400,250]
]
h = 425 # 需要裁剪矩阵的长和宽
w = 565
image_index = 300
for index_i in range(0, 9):
x = originalIndex[index_i][0]
y = originalIndex[index_i][1]
for i in range(1,301):
print(i)
image = imagePath+"IMG_"+str(i)+".jpg"
img = cv2.imread(image)
cropImg = img[(y):(y+h), (x):(x+w)]
cv2.imwrite("./justPoint/img_"+str(index_i+1)+"/IMG_"+str(image_index+i)+".jpg", cropImg)
mat = loadmat(groundPath+"GT_IMG_"+str(i)+".mat")
coordinates = mat["image_info"][0][0][0][0][0]
coords = []
for j in range(0, len(coordinates)):
x_1 = coordinates[j][0]
y_1 = coordinates[j][1]
if x_1 >= x and x_1 <= x+w:
if y_1 >= y and y_1 <= y+h:
x_1 = x_1-x
y_1 = y_1-y
coords.append([x_1, y_1])
mat["image_info"][0][0][0][0][0] = coords
scipy.io.savemat("./justPoint/ground_truth_"+str(index_i+1)+"/GT_IMG_"+str(image_index+i)+".mat", mat)
image_index += 300 # 原始图像为300张,每次裁剪生成300个新图像,因此加300
当得到裁剪后的图像以及.mat文件后,可以用下面代码进行将mat文件中的点标注到图像上来查看是否正确:
def showImageaAfterProcess():
for i in range(601,900):
img = "./justPoint/img_2/IMG_"+str(i)+".jpg"
image = cv2.imread(img)
mat = loadmat("./justPoint/ground_truth_2/GT_IMG_"+str(i)+".mat")
coords = mat["image_info"][0][0][0][0][0]
print(len(coords))
point_size = 1
point_color = (0, 0, 255) # BGR
thickness = 4 # 0 、4、8
for coor in coords:
cv2.circle(image, (int(coor[0]),int(coor[1])), point_size, point_color, thickness)
cv2.imwrite("./justPoint/img_3/IMG_"+str(i)+".jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY),95])
由于裁剪后的图像尺寸小于原始图像,因此如果需要将原始图像和裁剪后的图像进行大小调整,可以参考这篇博客:https://blog.csdn.net/zxs0222/article/details/107899079
完毕;