如何生成人群密度图

一、行人标注。标记在人头的位置。

%对图片上某个点进行标注
clear all
close all
clc
set(gcf,'color','white')
src='';
for i=1:12
    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]);
    %pic=pic_source;
    imwrite(pic,RSTR);
    showpic=imshow(pic);
    %set(B,  'X', [0 1000], 'Y', [0 1000])
    [x1,y1] = ginput;
    B=[x1,y1];
    hold on
    plot(x1,y1,'r+');

    [x2,y2] = ginput;
    C=[x2,y2];
    hold on
    plot(x2,y2 ,'r+');

    [x3,y3] = ginput;
    D=[x3,y3];
    hold on
    plot(x3,y3,'r+');

    [x4,y4] = ginput;
    E=[x4,y4];
    hold on
    plot(x4,y4,'r+');

    gt_point=[B;C;D;E];
    save(MSTR,'gt_point');   
end

标注完会得到每个图像的.mat数据,这些mat数据是点(x,y)的坐标,依据这些坐标去做人群密度图。自适应密度图那个在GitHub上找py程序做,这里的是固定核大小的密度图,

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=167:167
    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&&gt(j,2)<235)
            ksize=20;
        end
        if(gt(j,2)>=235&&gt(j,2)<260)
            ksize=30;
        end
         if(gt(j,2)>=260&&gt(j,2)<360)
            ksize=30;
         end
        if(gt(j,2)>=360&&gt(j,2)<=602)
            ksize=30;
        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,VIstr);   
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

这里给出制作的人群密度图的示例。
这里写图片描述

方法二、通过透视矩阵制作人群密度图
mall数据集人群密度图的制作

备注:如果是用来训练卷积神经网络的,要注意图像和密度图的大小关系,使其对应。

猜你喜欢

转载自blog.csdn.net/snwang_miss/article/details/80410501