【跌倒检测】基于matlab中值滤波+二值化跌倒检测【含Matlab源码 344期】

142 阅读3分钟

一、简介

中值滤波是基于排序统计理论的一种能有效抑制噪声的非线性信号处理技术,中值滤波的基本原理是把数字图像或数字序列中一点的值用该点的一个邻域中各点值的中值代替,让周围的像素值接近的真实值,从而消除孤立的噪声点。方法是用某种结构的二维滑动模板,将板内像素按照像素值的大小进行排序,生成单调上升(或下降)的为二维数据序列。二维中值滤波输出为g(x,y)=med{f(x-k,y-l),(k,l∈W)} ,其中,f(x,y),g(x,y)分别为原始图像和处理后图像。W为二维模板,通常为33,55区域。经中值滤波后图像如图1所示。

在这里插入图片描述

二、源代码

clear all clc
global Bili;
A=[];
B=imread('46.jpg');
B=rgb2gray(B);
B=medfilt2(B);        %中值滤波
fileName = '倾斜跌倒1.avi'; 
obj = VideoReader(fileName);           %读取录制的视频
vidFrames=read(obj);                 %读取所有帧图像
numFrames=get(obj, 'NumberOfFrames');%总帧数    
for k = 63:250                   %帧数循环,人为选取33帧作为背景帧
mov(k).cdata=vidFrames(:,:,:,k);
mov(k).cdata=rgb2gray(mov(k).cdata);
mov(k).cdata=medfilt2(mov(k).cdata);
R=imabsdiff(mov(k).cdata,B);
z=im2bw(R,graythresh(R));
z=bwmorph(z,'erode',3);                       % 3次腐蚀处理掉噪点
P = bwmorph(z,'dilate',3);                    %3次膨胀膨胀
P= bwareaopen(P,50);     
figure(1),subplot(121);imshow(vidFrames(:,:,:,k));
figure(1);subplot(122);imshow(P);
x=sum(P(:));
s=x/(480*640);
if s>=0.03
   [m1,n1] = find(P == 1); 
%第一级检测,角度
   top1= min(m1);%纵向
   bottom1= max(m1);
   left1= min(n1);%横向
   right1= max(n1);
   height1=bottom1-top1+1;%纵向高;
   width1 = right1-left1+1;%横向宽
   rectangle('Position',[left1,top1,width1,height1],'EdgeColor','r');
   line([left1,right1],[top1,bottom1],'color','r','LineWidth',1);%标记对角线
   line([left1,right1],[bottom1,top1],'color','r','LineWidth',1);
   tan=(bottom1-top1)/(right1-left1);
   tan=atan(tan);
   J=tan*180/pi;
%第二级检测,质心高度比Rz
   [rectx,recty,area,perimeter] = minboundrect(n1,m1,'p');
   line(rectx(:),recty(:),'color','w','LineWidth',1);
   line([rectx(1),rectx(3)],[recty(1),recty(3)],'color','b','LineWidth',1);
   line([rectx(2),rectx(4)],[recty(2),recty(4)],'color','b','LineWidth',1);
   line([0,640],[max(bottom1),max(bottom1)],'color','r','LineWidth',2);%水平线
   line([(rectx(1)+rectx(3))/2,(rectx(1)+rectx(3))/2],[(recty(1)+recty(3))/2,max(bottom1)],'color','b','LineWidth',2);%画高
   h=max(bottom1)-(recty(1)+recty(3))/2;%高度
   Rz=h/360;
   function [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
% minboundrect: Compute the minimal bounding rectangle of points in the plane
% usage: [rectx,recty,area,perimeter] = minboundrect(x,y,metric)
%
% arguments: (input)
%  x,y - vectors of points, describing points in the plane as
%        (x,y) pairs. x and y must be the same lengths.
%
%  metric - (OPTIONAL) - single letter character flag which
%        denotes the use of minimal area or perimeter as the
%        metric to be minimized. metric may be either 'a' or 'p',
%        capitalization is ignored. Any other contraction of 'area'
%        or 'perimeter' is also accepted.
%
%        DEFAULT: 'a'    ('area')
%
% arguments: (output)
%  rectx,recty - 5x1 vectors of points that define the minimal
%        bounding rectangle.
%
%  area - (scalar) area of the minimal rect itself.
%
%  perimeter - (scalar) perimeter of the minimal rect as found
%
%
% Note: For those individuals who would prefer the rect with minimum
% perimeter or area, careful testing convinces me that the minimum area
% rect was generally also the minimum perimeter rect on most problems
% (with one class of exceptions). This same testing appeared to verify my
% assumption that the minimum area rect must always contain at least
% one edge of the convex hull. The exception I refer to above is for
% problems when the convex hull is composed of only a few points,
% most likely exactly 3. Here one may see differences between the
% two metrics. My thanks to Roger Stafford for pointing out this
% class of counter-examples.
%
% Thanks are also due to Roger for pointing out a proof that the
% bounding rect must always contain an edge of the convex hull, in
% both the minimal perimeter and area cases.
%
%
% Example usage:
%  x = rand(50000,1);
%  y = rand(50000,1);
%  tic,[rx,ry,area] = minboundrect(x,y);toc
%
%  Elapsed time is 0.105754 seconds.
%
%  [rx,ry]
%  ans =
%      0.99994  -4.2515e-06
%      0.99998      0.99999
%   2.6441e-05            1
%  -5.1673e-06   2.7356e-05
%      0.99994  -4.2515e-06
%
%  area
%  area =
%      0.99994
%
%
% See also: minboundcircle, minboundtri, minboundsphere
%
%
% Author: John D'Errico
% E-mail: woodchips@rochester.rr.com
% Release: 3.0
% Release date: 3/7/07

% default for metric
if (nargin<3) || isempty(metric)
  metric = 'a';
elseif ~ischar(metric)
  error 'metric must be a character flag if it is supplied.'
else
  % check for 'a' or 'p'
  metric = lower(metric(:)');
  ind = strmatch(metric,{'area','perimeter'});
  if isempty(ind)
    error 'metric does not match either ''area'' or ''perimeter'''
  end

三、运行结果

在这里插入图片描述

四、备注

版本:2014a