参考自:如何生成人群密度图
1、标定图片
在路径下F:\m_and_c_aboutCounting\airport_tianjin_article\存放要标定的图片,图片命名规范如下:
原本的代码在标定的时候有一些不方便的地方,比如画错了不能撤销,不实时显示光标,这里进行了改良。
%对图片上某个点进行标注
clear all
close all
clc
set(gcf,'color','white')%图片的背景设置为白色
src='';
for i=1:2 %根据图片张数进行修改
num = i;
str = '.jpg' ;
Mstr='.mat';
filestr='F:\m_and_c_aboutCounting\airport_tianjin_article\';
refilestr='F:\m_and_c_aboutCounting\airport_tianjin_article\';
left_par='air(';
Mark='mark';
right_par=')';
STR = sprintf('%s%s%d%s%s', filestr,left_par,num ,right_par, str) ;
RSTR=sprintf('%s%s%d%s%s', refilestr,left_par,num ,right_par, str) ;
MSTR=sprintf('%s%s%d%s%s', refilestr,left_par,num ,right_par, Mstr) ;
MarkSTR=sprintf('%s%d%s', refilestr,Mark,num , Mstr) ;
pic_source=imread(STR);
pic=imresize(pic_source,[602,800]);%调整图像的大小,
imwrite(pic,RSTR);%实际修改本地的图片
showpic=imshow(pic);
gt_point = [0,0];%用于存储标记点
while(1)
[x1,y1]=ginput(1);
Atemp=[x1,y1];
gt_point = [gt_point;Atemp];
hold on
plot(x1,y1,'r+');
% Q键逐步撤销之前标注的点
if strcmpi(get(gcf,'CurrentCharacter'),'q')
if size(gt_point,1)>0
gt_point(size(gt_point,1),:)=[];%非回车会以鼠标当前点做一次存储,这是多余的,删除
end
%重新做图
close all
showpic=imshow(pic);
if size(gt_point,1) >0
gt_point(size(gt_point,1),:)=[];
for j = 1:size(gt_point,1)
hold on
plot(gt_point(j,1),gt_point(j,2),'r+');
end
end
end
%回车键结束本次图片标定
if strcmpi(get(gcf,'CurrentCharacter'),char(13))
break;
end
end
gt_point(1,:)=[];%删除第一行多余的0行
save(MSTR,'gt_point');
close all
end
按回车结束当前图片标定
标定完成后,会在当前目录下生成.mat文件,存储标定点的坐标(x,y),用NX2的矩阵表示,N为标定的点数。其中按
Q键
可以撤销之前标注错误的点。2、生成密度图
clear;
m=602;n=800;
%m=m/4;
%n=n/4;
GTStr='F:\m_and_c_aboutCounting\airport_tianjin_article\air(';
GTMAT=').mat';
IMGStr='F:\m_and_c_aboutCounting\airport_tianjin_article\air(';
IMG=').jpg';
%shanghaih5str='shanghai\train_data\train_data.h5';
%h5create(shanghaih5str,'/label',[259 192 1 2],'Datatype','double');
for num=2:2
gtSTR =sprintf('%s%d%s', GTStr,num , GTMAT ) ;
imgSTR=sprintf('%s%d%s', IMGStr,num , IMG ) ;
load(gtSTR);
img=imread(imgSTR);
%Reimg=imresize(img,0.25);
gt=gt_point;
%gt=F;
%gt=gt/4;
d_map = zeros(m,n);
for j=1:size(gt,1)
if(gt(j,2)>0&>(j,2)<235)%表示离镜头比较远
ksize=30;
end
if(gt(j,2)>=235&>(j,2)<260)
ksize=40;
end
if(gt(j,2)>=260&>(j,2)<360)
ksize=40;
end
if(gt(j,2)>=360&>(j,2)<=602)
ksize=40;
end
radius =ceil(ksize/2);
sigma = ksize/2.5;
h = fspecial('gaussian',[ksize],sigma);
x_ = max(1,floor(gt(j,1)));
y_ = max(1,floor(gt(j,2)));
%在索引区的素有列按照从右往左的顺序依次加到矩阵
if (x_-radius+1<1)
for ra = 0:radius-x_-1
h(:,end-ra) = h(:,end-ra)+h(:,1);
h(:,1)=[];
end
end
if (y_-radius+1<1)
for ra = 0:radius-y_-1
h(end-ra,:) = h(end-ra,:)+h(1,:);
h(1,:)=[];
end
end
if (x_+ksize-radius>n)
for ra = 0:x_+ksize-radius-n-1
h (:,1+ra) = h(:,1+ra)+h(:,end);
h(:,end) = [];
end
end
if(y_+ksize-radius>m)
for ra = 0:y_+ksize-radius-m-1
h (1+ra,:) = h(1+ra,:)+h(end,:);
h(end,:) = [];
end
end
d_map(max(y_-radius+1,1):min(y_+ksize-radius,m),max(x_-radius+1,1):min(x_+ksize-radius,n))...
= d_map(max(y_-radius+1,1):min(y_+ksize-radius,m),max(x_-radius+1,1):min(x_+ksize-radius,n))...
+ h;
end
vi_map=imagesc(d_map);
% figure;
VIstr=num2str(num,'F:\m_and_c_aboutCounting\airport_tianjin_R\density_map\(%d).jpg');
%imwrite(vi_map,VIstr);
saveas(gcf,'F:\m_and_c_aboutCounting\airport_tianjin_R\density_map\1.jpg');
end
网友评论