飞道的博客

【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真

387人阅读  评论(0)

1.软件版本

matlab2015b

2.算法概述

       人群密度情况分为三个等级,(1)稀少和不拥挤情况下为绿色提醒。(2)比较拥挤情况下,黄色预警。(3)非常拥挤情况下,红色报警。 不同人群密度情况通过相应的报警级别在界面上实时显示出来

人群密度分类两种思路:

(1)估计在景人数,根据人数多少,判断人群密度情形。

(2)提取分析人群的整体特征,训练样本,利用分类器学习分类。

首先对视频进行纹理提取,采用的方法是灰度共生矩阵:

http://wenku.baidu.com/view/d60d9ff5ba0d4a7302763ae1.html?from=search

然后通过GRNN神经网络训练识别算法:

        广义回归神经网络(Generalized regression neural network, GRNN)是一种建立在非参数核回归基础之上的神经网络,通过观测样本计算自变量和因变量之间的概率密度函数。GRNN结构如图1所示,整个网络包括四层神经元:输入层、模式层、求和层与输出层。

 

        GRNN神经网络的性能,主要通过对其隐回归单元的核函数的光滑因子来设置的,不同的光滑因子可获得不同的网络性能。输入层的神经元数目与学习样本中输入向量的维数m相等。每个神经元都分别对应一个不同的学习样本,模式层中第i个神经元的传递函数为:

        由此可以看出,当选择出学习样本之后,GRNN网络的结构与权值都是完全确定的,因而训练GRNN网络要比训练BP网络和RBF网络便捷得多。根据上述GRNN网络的各个层的输出计算公式,整个GRNN网络的输出可用如的式子表示:

3.部分源码


  
  1. function pushbutton2_Callback(hObject, eventdata, handles)
  2. % hObject handle to pushbutton2 (see GCBO)
  3. % eventdata reserved - to be defined in a future version of MATLAB
  4. % handles structure with handles and user data (see GUIDATA)
  5. global frameNum_Original;
  6. global frameNum_Originals;
  7. global Obj;
  8. %%
  9. %参数初始化
  10. %处理视频大小
  11. RR = 200;
  12. CC = 300;
  13. K = 3; %组件
  14. Alpha = 0.02; %适应权重速度
  15. Rho = 0.01; %适应权重速度协方差
  16. Deviation_sq = 49; %阈值用于查找匹配
  17. Variance = 2; %初始方差为新放置组件
  18. Props = 0.00001; %最初为新放置
  19. Back_Thresh = 0.8; %体重的比例必须占背景模型
  20. Comp_Thresh = 10; %滤掉连接组件的较小的尺寸
  21. SHADOWS =[ 0.7, 0.25, 0.85, 0.95]; %设置阴影去除门限值
  22. CRGB = 3;
  23. D = RR * CC;
  24. Temps = zeros(RR,CC,CRGB, 'uint8');
  25. Temps = imresize(read(Obj, 1),[RR,CC]);
  26. Temps = reshape(Temps,size(Temps, 1)*size(Temps, 2),size(Temps, 3));
  27. Mus = zeros(D,K,CRGB);
  28. Mus(:, 1,:) = double(Temps(:,:, 1));
  29. Mus(:, 2:K,:) = 255*rand([D,K- 1,CRGB]);
  30. Sigmas = Variance*ones(D,K,CRGB);
  31. Weights = [ones(D, 1),zeros(D,K- 1)];
  32. Squared = zeros(D,K);
  33. Gaussian = zeros(D,K);
  34. Weight = zeros(D,K);
  35. background = zeros(RR,CC,frameNum_Original);
  36. Shadows = zeros(RR,CC);
  37. Images0 = zeros(RR,CC,frameNum_Original);
  38. Images1 = zeros(RR,CC,frameNum_Original);
  39. Images2 = zeros(RR,CC,frameNum_Original);
  40. background_Update = zeros(RR,CC,CRGB,frameNum_Original);
  41. indxx = 0;
  42. for tt = frameNum_Originals
  43. disp( '当前帧数');
  44. tt
  45. indxx = indxx + 1;
  46. pixel_original = read(Obj,tt);
  47. pixel_original2 = imresize(pixel_original,[RR,CC]);
  48. Temp = zeros(RR,CC,CRGB, 'uint8');
  49. Temp = pixel_original2;
  50. Temp = reshape(Temp,size(Temp, 1)*size(Temp, 2),size(Temp, 3));
  51. image = Temp;
  52. for kk = 1:K
  53. Datac = double(Temp)-reshape(Mus(:,kk,:),D,CRGB);
  54. Squared(:,kk) = sum((Datac.^ 2)./reshape(Sigmas(:,kk,:),D,CRGB), 2);
  55. end
  56. [junk,index] = min(Squared,[], 2);
  57. Gaussian = zeros(size(Squared));
  58. Gaussian(sub2ind(size(Squared), 1:length(index),index ')) = ones(D,1);
  59. Gaussian = Gaussian&(Squared<Deviation_sq);
  60. %参数更新
  61. Weights = ( 1-Alpha).*Weights+Alpha.*Gaussian;
  62. for kk = 1:K
  63. pixel_matched = repmat(Gaussian(:,kk), 1,CRGB);
  64. pixel_unmatched = abs(pixel_matched- 1);
  65. Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
  66. Sigma_kk = reshape(Sigmas(:,kk,:),D,CRGB);
  67. Mus(:,kk,:) = pixel_unmatched.*Mu_kk+pixel_matched.*((( 1-Rho).*Mu_kk)+(Rho.* double(image)));
  68. Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
  69. Sigmas(:,kk,:) = pixel_unmatched.*Sigma_kk+pixel_matched.*((( 1-Rho).*Sigma_kk)+repmat((Rho.* sum(( double(image)-Mu_kk).^ 2, 2)), 1,CRGB));
  70. end
  71. replaced_gaussian = zeros(D,K);
  72. mismatched = find(sum(Gaussian, 2)== 0);
  73. for ii = 1:length(mismatched)
  74. [junk,index] = min(Weights(mismatched(ii),:)./sqrt(Sigmas(mismatched(ii),:, 1)));
  75. replaced_gaussian(mismatched(ii),index) = 1;
  76. Mus(mismatched(ii),index,:) = image(mismatched(ii),:);
  77. Sigmas(mismatched(ii),index,:) = ones( 1,CRGB)*Variance;
  78. Weights(mismatched(ii),index) = Props;
  79. end
  80. Weights = Weights./repmat(sum(Weights, 2), 1,K);
  81. active_gaussian = Gaussian+replaced_gaussian;
  82. %背景分割
  83. [junk,index] = sort(Weights./sqrt(Sigmas(:,:, 1)), 2, 'descend');
  84. bg_gauss_good = index(:, 1);
  85. linear_index = (index- 1)*D+repmat([ 1:D] ',1,K);
  86. weights_ordered = Weights(linear_index);
  87. for kk = 1:K
  88. Weight(:,kk)= sum(weights_ordered(:, 1:kk), 2);
  89. end
  90. bg_gauss(:, 2:K) = Weight(:, 1:(K- 1)) < Back_Thresh;
  91. bg_gauss(:, 1) = 1;
  92. bg_gauss(linear_index) = bg_gauss;
  93. active_background_gaussian = active_gaussian & bg_gauss;
  94. foreground_pixels = abs(sum(active_background_gaussian, 2)- 1);
  95. foreground_map = reshape(sum(foreground_pixels, 2),RR,CC);
  96. Images1 = foreground_map;
  97. objects_map = zeros(size(foreground_map), 'int32');
  98. object_sizes = [];
  99. Obj_pos = [];
  100. new_label = 1;
  101. %计算连通区域
  102. [label_map,num_labels] = bwlabel(foreground_map, 8);
  103. for label = 1:num_labels
  104. object = (label_map == label);
  105. object_size = sum(sum( object));
  106. if(object_size >= Comp_Thresh)
  107. objects_map = objects_map + int32( object * new_label);
  108. object_sizes(new_label) = object_size;
  109. [X,Y] = meshgrid( 1:CC, 1:RR);
  110. object_x = X.* object;
  111. object_y = Y.* object;
  112. Obj_pos(:,new_label) = [sum(sum(object_x)) / object_size;
  113. sum(sum(object_y)) / object_size];
  114. new_label = new_label + 1;
  115. end
  116. end
  117. num_objects = new_label - 1;
  118. %去除阴影
  119. index = sub2ind(size(Mus),reshape(repmat([ 1:D],CRGB, 1),D*CRGB, 1),reshape(repmat(bg_gauss_good ',CRGB,1),D*CRGB,1),repmat([1:CRGB]',D,1));
  120. background = reshape(Mus(index),CRGB,D);
  121. background = reshape(background ',RR,CC,CRGB);
  122. background = uint8(background);
  123. if indxx <= 500;
  124. background_Update = background;
  125. else
  126. background_Update = background_Update;
  127. end
  128. background_hsv = rgb2hsv(background);
  129. image_hsv = rgb2hsv(pixel_original2);
  130. for i = 1:RR
  131. for j = 1:CC
  132. if (objects_map(i,j))&&...
  133. (abs(image_hsv(i,j, 1)-background_hsv(i,j, 1))< SHADOWS( 1))&&...
  134. (image_hsv(i,j, 2)-background_hsv(i,j, 2)< SHADOWS( 2))&&...
  135. ( SHADOWS( 3)<=image_hsv(i,j, 3)/background_hsv(i,j, 3)<= SHADOWS( 4))
  136. Shadows(i,j) = 1;
  137. else
  138. Shadows(i,j) = 0;
  139. end
  140. end
  141. end
  142. Images0 = objects_map;
  143. objecs_adjust_map = Shadows;
  144. Images2 = objecs_adjust_map;
  145. %%
  146. %根据像素所在区域大小比例以及纹理特征分析获得人密度
  147. %腐蚀处理
  148. se = strel( 'ball',6,6);
  149. Images2BW = floor(abs(imdilate(Images2,se)- 5));
  150. Images3BW = zeros(size(Images2BW));
  151. X1 = round( 168/ 2);
  152. X2 = round( 363/ 2);
  153. Y1 = round( 204/ 2);
  154. Y2 = round( 339/ 2);
  155. if indxx > 80;
  156. %计算区域内像素值
  157. S1 = sum(sum(Images2BW(Y1:Y2,X1:X2)));
  158. S2(indxx- 80) = S1/((X2-X1)*(Y2-Y1));
  159. end
  160. Images3BW(Y1:Y2,X1:X2) = Images2BW(Y1:Y2,X1:X2);
  161. Images3Brgb = pixel_original2(Y1:Y2,X1:X2,:);
  162. %纹理检测
  163. %计算纹理
  164. [A,B] = func_wenli(rgb2gray(Images3Brgb));
  165. %选择能量 熵作为判断依据
  166. if indxx > 80;
  167. F1(indxx- 80) = A( 1);
  168. F2(indxx- 80) = A( 2);
  169. F3(indxx- 80) = A( 3);
  170. end
  171. if indxx > 80;
  172. load train_model.mat
  173. P = [S2(indxx- 80);F2(indxx- 80)];
  174. y = round(NET(P));
  175. if y == 1
  176. set( handles.text2, 'String','低密度');
  177. set( handles.text2, 'ForegroundColor',[0 1 0]) ;
  178. end
  179. if y == 2
  180. set( handles.text2, 'String','中密度');
  181. set( handles.text2, 'ForegroundColor',[1 1 0]) ;
  182. end
  183. if y == 3
  184. set( handles.text2, 'String','高密度');
  185. set( handles.text2, 'ForegroundColor',[1 0 0]) ;
  186. end
  187. end
  188. axes( handles.axes1)
  189. imshow(pixel_original2);
  190. % title( '定位检测区域');
  191. hold on
  192. line([X1,X2],[Y1,Y1], 'LineWidth',1,'Color',[0 1 0]);
  193. hold on
  194. line([X2,X2],[Y1,Y2], 'LineWidth',1,'Color',[0 1 0]);
  195. hold on
  196. line([X2,X1],[Y2,Y2], 'LineWidth',1,'Color',[0 1 0]);
  197. hold on
  198. line([X1,X1],[Y2,Y1], 'LineWidth',1,'Color',[0 1 0]);
  199. axes( handles.axes2)
  200. imshow(uint8(background_Update));
  201. % title( '背景获得');
  202. axes( handles.axes3)
  203. imshow(Images0,[]);
  204. % title( '动态背景提取');
  205. axes( handles.axes4)
  206. imshow(Images3BW,[]);
  207. % title( '动态背景提取(检测区域内)');
  208. pause( 0.0000001);
  209. end

4.仿真结果


转载:https://blog.csdn.net/ccsss22/article/details/125568328
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场