图像及label处理为密度图并保存为csv格式
该方法使用matlab
427更新,解决了保存3通道图片,密度图3通道保存出错的问题
主程序
################################################
#############MERCY ROIN 2020/03/21##############
########https://blog.csdn.net/qq_42732137#######
################################################
clc; clear all;
seed = 95461354;
rng(seed)
N = 2;
dataset = 'train';
%图片链接
path = ['I:/crowd_dataset/L/'];
output_path = 'I:/crowd_dataset/L/formatted_trainval_1';
train_path_img = strcat(output_path, '/train/');
train_path_den = strcat(output_path, '/train_den/');
val_path_img = strcat(output_path, '/val/');
val_path_den = strcat(output_path, '/val_den/');
gt_path = ['I:/crowd_dataset/L/'];
img = dir('I:\crowd_dataset/L\*.jpg');%load img
mkdir(output_path)
mkdir(train_path_img);
mkdir(train_path_den);
mkdir(val_path_img);
mkdir(val_path_den);
num_images = length(img);
num_val = ceil(num_images*0.1);
indices = randperm(num_images);
for idx = 1:num_images
i = indices(idx);
imgname = img(idx).name;
matname = imgname;
csvname = imgname;
matname(end-2:end) = 'mat';
csvname(end-2:end) = 'csv';
load([gt_path matname]);%gt.mat path
if (mod(idx,10)==0)
fprintf(1,'Processing %3d/%d files\n', idx, num_images);
end
image = imread([path, imgname]);
annPoints = gt_point; %mat里函数名注意修改为自己的
%灰度处理。如果图像使用RGB
% [h, w, c] = size(image);
if (c == 3)
im = rgb2gray(image);
end
%% gaussian kernel function
im_density = get_density_map_autogaussian(image,annPoints);%%导入自适应高斯核函数
%% data processing
for j = 1:N
%% total image 图像翻转增加数据量
im_sampled = image;
im_density_sampled = im_density(:, :, 1);
if (j==2)
im_sampled = fliplr(image);
im_density_sampled = fliplr(im_density(:, :, 1));
end
%%
%show img&den
den=255*im_density_sampled;
figure(1)
subplot(1,2,2);
imshow(den)
hold on
plot(annPoints(:,1),annPoints(:,2),'r*')
subplot(1,2,1);
imshow(im_sampled)
hold on
plot(annPoints(:,1),annPoints(:,2),'r*')
img_idx = strcat(num2str(i), '_',num2str(j));
if(idx < num_val)
imwrite(im_sampled, [val_path_img num2str(img_idx) '.jpg']);
csvwrite([val_path_den num2str(img_idx) '.csv'], im_density_sampled);
else
imwrite(im_sampled, [train_path_img num2str(img_idx) '.jpg']);
csvwrite([train_path_den num2str(img_idx) '.csv'], im_density_sampled);
end
end
end
自适应高斯核 1
%function:通过高斯核产生相应的密度矩阵
%parameter: im:输入图像,灰度图; points:标注的点[X Y],n*2的矩阵
function im_density = get_density_map_autogaussian(im,points)
im_density = zeros(size(im));
[h,w] = size(im_density);
if(isempty(points))
return;
end
%points为1行
if(length(points(:,1))==1)
x1 = max(1,min(w,round(points(1,1)))); %round:四舍五入,x1变成points(1,1)处的整数
y1 = max(1,min(h,round(points(1,2))));
im_density(y1,x1) = 255;
return;
end
for j = 1:length(points)
max_kernel = 65; %最大高斯核尺寸
normal_kernel = 35; %默认高斯核尺寸
beta = 0.3; %MCNN中给定的参数
k = 6; %近邻数
maxpixel = 80; %近邻的最大距离,像素
% f_sz = normal_kernel;
% sigma = beta * f_sz;
x = min(w,max(1,abs(double(floor(points(j,1))))));
y = min(h,max(1,abs(double(floor(points(j,2))))));
if(x > w || y > h)
continue;
end
%--------%auto adaptive 自适应高斯核--------
atemp = [];
for i = 1:length(points)
if i == j
continue;
end
xk = min(w,max(1,abs(double(floor(points(i,1))))));
yk = min(w,max(1,abs(double(floor(points(i,2))))));
if(xk > w || yk > h)
continue;
end
dis = sqrt( ((xk-x)^2 + (yk-y)^2) );
atemp = [atemp dis];
end
btemp = sort(atemp);
sum = 0;
count = 0;
if k >= length(points)
k = length(points) - 1;
end
for m = 1:k
if btemp(m) > maxpixel
break;
end
sum = sum + btemp(m);
count = count + 1;
end
if count > 0
temp = sum/count;
else
temp = normal_kernel;
end
f_sz = double(floor(temp));
if mod(f_sz, 2) == 0
f_sz = double(f_sz + 1);
end
if f_sz > (max_kernel + 1)
f_sz = max_kernel;
end
sigma = beta * f_sz;
% fprintf("x:%d,y:%d,f_sz:%d,sigma:%d\n",x,y,f_sz, sigma);
%--------auto adaptive 自适应高斯核定义结束--------
H = fspecial('Gaussian',[f_sz, f_sz],sigma);
%高斯核边界限定,x方向:→,y方向:↓
x1 = x - double(floor(f_sz/2)); y1 = y - double(floor(f_sz/2)); %x1左边界,y1上边界
x2 = x + double(floor(f_sz/2)); y2 = y + double(floor(f_sz/2)); %x2右边界,y2下边界
dfx1 = 0; dfy1 = 0; dfx2 = 0; dfy2 = 0;
change_H = false;
if(x1 < 1)
dfx1 = abs(x1)+1;
x1 = 1;
change_H = true;
end
if(y1 < 1)
dfy1 = abs(y1)+1;
y1 = 1;
change_H = true;
end
if(x2 > w)
dfx2 = x2 - w;
x2 = w;
change_H = true;
end
if(y2 > h)
dfy2 = y2 - h;
y2 = h;
change_H = true;
end
x1h = 1+dfx1; y1h = 1+dfy1; x2h = f_sz - dfx2; y2h = f_sz - dfy2;
if (change_H == true)
H = fspecial('Gaussian',[double(y2h-y1h+1), double(x2h-x1h+1)],sigma);
end
im_density(y1:y2,x1:x2) = im_density(y1:y2,x1:x2) + H;
end
end
固定核
function im_density = get_density_map_gaussian(im,points)
im_density = zeros(size(im));
[h,w] = size(im_density);
if(length(points)==0)
return;
end
if(length(points(:,1))==1)
x1 = max(1,min(w,round(points(1,1))));
y1 = max(1,min(h,round(points(1,2))));
im_density(y1,x1) = 255;
return;
end
for j = 1:length(points)
f_sz = 15;
sigma = 4.0;
H = fspecial('Gaussian',[f_sz, f_sz],sigma);
x = min(w,max(1,abs(int32(floor(points(j,1))))));
y = min(h,max(1,abs(int32(floor(points(j,2))))));
if(x > w || y > h)
continue;
end
x1 = x - int32(floor(f_sz/2)); y1 = y - int32(floor(f_sz/2));
x2 = x + int32(floor(f_sz/2)); y2 = y + int32(floor(f_sz/2));
dfx1 = 0; dfy1 = 0; dfx2 = 0; dfy2 = 0;
change_H = false;
if(x1 < 1)
dfx1 = abs(x1)+1;
x1 = 1;
change_H = true;
end
if(y1 < 1)
dfy1 = abs(y1)+1;
y1 = 1;
change_H = true;
end
if(x2 > w)
dfx2 = x2 - w;
x2 = w;
change_H = true;
end
if(y2 > h)
dfy2 = y2 - h;
y2 = h;
change_H = true;
end
x1h = 1+dfx1; y1h = 1+dfy1; x2h = f_sz - dfx2; y2h = f_sz - dfy2;
if (change_H == true)
H = fspecial('Gaussian',[double(y2h-y1h+1), double(x2h-x1h+1)],sigma);
end
im_density(y1:y2,x1:x2) = im_density(y1:y2,x1:x2) + H;
end
end
https://www.jianshu.com/p/935a74d83a50 ↩︎