C函数如下:
#define CPM0(a, b) (a == b)
#define CPM1(a, b) (a < b)
#define MUX(a, c, d) (a ?(c):(d))
#define VIDEO_WIDTH_FD1 704
#define COVER_STEPFD1 4
#define COVER_RIGHTFD1 2
#define COVER_STEPCIF 4
#define COVER_RIGHTCIF 2
/********************************************************************
* 功能 : 获取一帧视频图像的亮度信号,计算其方差,判断是否被遮挡
* 输入参数: width-图像宽度
height-图像高度
array-像素数组
* level-遮挡灵敏度:3-高; 2-中; 1-低.
* 输出参数: 无
* 返回值 : 0-无视频遮挡报警;1-有视频遮挡报警.
********************************************************************/
int C_StandardDeviation(const int width, const int height, const int level, unsigned char *array, unsigned std_dev)
{
unsigned i;
unsigned threshold = 0;
unsigned tmp = 0;
unsigned mean = 0;
unsigned total_pixel = 0;
total_pixel = width*height;
if (width == VIDEO_WIDTH_FD1)
{
for (i = width + 1; i < total_pixel - width - 1; i += COVER_STEPFD1)
{
mean += (array[i - width] + array[i - 1] + array[i + 1] + array[i + width]) >> 2;
}
mean /= ((total_pixel - (width << 1) - 2) >> COVER_RIGHTFD1); //计算像素亮度均值
for (i = width + 1; i < total_pixel - width - 1; i += COVER_STEPFD1) // 计算像素亮度方差
{
tmp = mean - array[i];
tmp = tmp * tmp;
std_dev += tmp;
}
std_dev /= ((total_pixel - (width << 1) - 2) >> COVER_RIGHTFD1);
}
else
{
for (i = width + 1; i < total_pixel - width - 1; i += COVER_STEPCIF)
{
mean += (array[i - width] + array[i - 1] + array[i + 1] + array[i + width]) >> 2;
}
mean /= ((total_pixel - (width << 1) - 2) >> COVER_RIGHTCIF); //计算像素亮度均值
//T64x_DbgSprintf("get mean = %dn", mean);
for (i = width + 1; i < total_pixel - width - 1; i += COVER_STEPCIF) // 计算像素亮度方差
{
tmp = mean - array[i];
tmp = tmp * tmp;
std_dev += tmp;
}
std_dev /= ((total_pixel - (width << 1) - 2) >> COVER_RIGHTCIF);
T64x_DbgSprintf("get std_dev = %dn", std_dev);
}
if (level == 1)
{
threshold =MUX(CPM0(width, VIDEO_WIDTH_FD1), 450, 350); //遮挡灵敏度低
}
else if (level == 2)
{
threshold = MUX(CPM0(width, VIDEO_WIDTH_FD1), 500, 400); //遮挡灵敏度中
}
else if(level == 3)
{
threshold = MUX(CPM0(width, VIDEO_WIDTH_FD1), 550, 450); //遮挡灵敏度高
}
return MUX(CPM1(std_dev, threshold), 1, 0);
}
线性汇编实现其中部分功能:
;********************************************************************
; 功能 : 获取一帧CIF (352*288) 视频图像的亮度信号,计算其方差,判断是否被遮挡
; 输入参数:array-像素数组
;level-遮挡灵敏度:3-高; 2-中; 1-低.
; 输出参数:均方差std_dev
;返回值 : 0-无视频遮挡报警;1-有视频遮挡报警.
;********************************************************************
.sect ".text:_StandardDeviation"
.global _StandardDeviation
_StandardDeviation .cproc level, array, arr_std_dev
; ============================ SYMBOLIC REGISTERS ============================
.no_mdep
.regA_counter1,B_counter1
.regA_flag,B_flag
.regA_tmp0,B_tmp0
.regA_tmp1,B_tmp1
.regA_tmp2,B_tmp2
.regA_tmp3,B_tmp3
.regA_tmp4,B_tmp4
.regA_tmp5,B_tmp5
.regA_tmp6,B_tmp6
.regA_tmp7,B_tmp7
.regA_tmp8,B_tmp8
.regA_tmp9,B_tmp9
.regA_mean,B_mean
.regA_std_dev,B_std_dev
.regA_arr,B_arr
;******************************************************************************
;计算均值
ZERO.1A_mean
ZERO.2B_mean
ZERO.1A_std_dev
ZERO.2B_std_dev
MVK.112581,A_counter1;A_counter1 = ((101017-353)/4)/2 -2
MVK.212581,B_counter1
MV.2array,B_arr
MV. 1XB_arr,A_arr
ADD.1A_arr,1,A_tmp1;A_tmp1 = array[1]
MVK.1352,A_tmp2
ADD.1A_arr,A_tmp2,A_tmp2;A_tmp2 = array[width]
MVK.1354,A_tmp3
ADD.1A_arr,A_tmp3,A_tmp3;A_tmp3 = array[witch+2]
MVK.1705,A_tmp4
ADD.1A_arr,A_tmp4,A_tmp4;A_tmp4 = array[2witch+1]
ADD.2B_arr,5,B_tmp1;B_tmp1 = array[5]
MVK.2356,B_tmp2
ADD.2B_arr,B_tmp2,B_tmp2;B_tmp2 = array[width + 4]
MVK.2358,B_tmp3
ADD.2B_arr,B_tmp3,B_tmp3;B_tmp3 = array[witch+6]
MVK.2709,B_tmp4
ADD.2B_arr,B_tmp4,B_tmp4;B_tmp4 = array[2witch+5]
LOOP1:.trip1
LDBU.1*A_tmp1++[8],A_tmp5
LDBU.1*A_tmp2++[8],A_tmp6
LDBU.1*A_tmp3++[8],A_tmp7
LDBU.1*A_tmp4++[8],A_tmp8
LDBU.2*B_tmp1++[8],B_tmp5
LDBU.2*B_tmp2++[8],B_tmp6
LDBU.2*B_tmp3++[8],B_tmp7
LDBU.2*B_tmp4++[8],B_tmp8
ADD.1A_tmp5,A_tmp6,A_tmp5
ADD.1A_tmp7,A_tmp8,A_tmp7
ADD.1A_tmp5,A_tmp7,A_tmp7
SHRU.1A_tmp7,2,A_tmp7
ADD.1A_tmp7,A_mean,A_mean
ADD.2B_tmp5,B_tmp6,B_tmp5
ADD.2B_tmp7,B_tmp8,B_tmp7
ADD.2B_tmp5,B_tmp7,B_tmp7
SHRU.2B_tmp7,2,B_tmp7
ADD.2B_tmp7,B_mean,B_mean
BDEC.1LOOP1,A_counter1
LDBU.1*A_tmp1,A_tmp5;得到最后一个奇数值
LDBU.1*A_tmp2,A_tmp6
LDBU.1*A_tmp3,A_tmp7
LDBU.1*A_tmp4,A_tmp8
ADD.1A_tmp5,A_tmp6,A_tmp5
ADD.1A_tmp7,A_tmp8,A_tmp7
ADD.1A_tmp5,A_tmp7,A_tmp7
SHRU.1A_tmp7,2,A_tmp7
ADD.1A_tmp7,A_mean,A_mean
ADD.2XB_mean,A_mean,B_mean
SHRU.2B_mean,14,B_mean;((total_pixel - (width << 1) - 2) >> cover) = 25167
MVK.2667,B_tmp0;25167 = (2)14 * (2)10 /667
MPY.2B_mean,B_tmp0,B_mean
SHRU.2B_mean,10,B_mean
MV.1XB_mean,A_mean
;STW.1B_mean,*arr_std_dev
;计算标准差
MVK.1353,A_tmp0
ADD.1A_arr,A_tmp0,A_tmp1;A_tmp1 = array[width + 1]
MVK.2357,B_tmp0
ADD.2B_arr,B_tmp0,B_tmp1;B_tmp1 = array[width + 5]
LOOP2:.trip1
LDBU.1*A_tmp1++[8],A_tmp5
LDBU.2*B_tmp1++[8],B_tmp5
SUB.1A_mean,A_tmp5,A_tmp6
MPY.1A_tmp6,A_tmp6,A_tmp6
ADD.1A_std_dev,A_tmp6,A_std_dev
SUB.2B_mean,B_tmp5,B_tmp6
MPY.2B_tmp6,B_tmp6,B_tmp6
ADD.2B_std_dev,B_tmp6,B_std_dev
BDEC.2LOOP2,B_counter1
LDBU.1*A_tmp1,A_tmp5;得到最后一个奇数值
LDBU.2*B_tmp1,B_tmp5
SUB.1A_mean,A_tmp5,A_tmp6
MPY.1A_tmp6,A_tmp6,A_tmp6
ADD.1A_std_dev,A_tmp6,A_std_dev
ADD.2XA_std_dev,B_std_dev,B_std_dev
SHRU.2B_std_dev,14,B_std_dev;((total_pixel - (width << 1) - 2) >> cover) = 25167
MVK.2667,B_tmp0;25167 = (2)14 * (2)10 /667
MPY.2B_std_dev,B_tmp0,B_std_dev
SHRU.2B_std_dev,10,B_std_dev
STW.1B_std_dev,*arr_std_dev
MVK.2450,B_tmp9
CMPLT.2B_std_dev,B_tmp9,B_flag;默认等级为level = 3
;******************************************************************************
.returnB_flag
.endproc