<div style="clear:both;"> <div style="clear:both;">
1.MPLAB IDE集成开发环境
MPLAB 集成开发环境 (IDE) 是一个采用Microchip 的PICmicro 和 dsPIC开发嵌入式应用的免费集成工具箱。MPLAB IDE在32-bit 的MS Windows下运行,是一个简单易用的开发环境,并且包含很多可进行快速应用开发和调试的免费软件。MPLAB IDE同样也充当一个附加的Microchip和第三方软件和硬件工具的图形用户界面。
2.实现目标跟踪算法的应用程序
应用程序是针对需求编写的仅适用于本系统的专用程序。本系统应用程序流程如图2所示。初始化后打开设备文件(摄像头等),查询和确认设备性能,设置捕获的图像的宽和高,设置色深,建立内存映射,读取图像数据,对图像进行处理,关闭设备。图像采集有两种方式:内存映射(mmap)和直接读取设备(read)。前者将设备文件映射到内存,绕过I/O访问,使得读取速度更快,但是占用更多系统资源。考虑系统实时性,因此采用内存映射方式。
图2系统应用程序流程图
在本系统中,采用MPLAB C32 C编译器将目标跟踪的算法编译实现,由一系列C函数和少量C++类构成,可以实现图像处理和计算机视觉方面的很多通用算法。
3.目标检测和跟踪算法的设计方案:
①目标的检测部分:
对航空图片进行contourlet变换。该变换能满足各向异性的性质。Contourlet变换是使用拉普拉斯滤波器对图象进行多尺度分解,以捕获二维图像中存在的点奇异性,得到原图像的低频图像和高频图像,递归地对低通图像进行分解,得到整个多分辨率图像。对分解后每一尺度上的高频图像使用方向滤波器组,得到各奇异点的多方向性图像。采用contourlet变换提取各尺度下的稳定数值特征,构建相应的特征库,利用目标质心建立目标跟踪点,匹配真正的飞行目标。
②目标的跟踪部分:
本系统采用的目标跟踪算法为Mean-Shift算法。Mean-Shift算法是一种计算局部最优的搜索算法,通过计算候选目标与目标模块直接之间相似度的概率密度分布,然后利用概率密度梯度下降的方向来获取匹配搜索的最佳路径,加速运动目标的定位降低搜索的时间。
摄像头驱动程序
#include "stdafx.h"
#include "Camera.h"
#include "CameraDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CAboutDlg dialog used for App About
IplImage*image=NULL;
CvHistogram *hist = 0;
int backproject_mode = 0;
int select_object = 0;
int track_object = 0;
int show_hist = 1;
CvPoint origin;
CvRect selection;
CvRect track_window;
CvBox2D track_box; // tracking ·????????ò box????????
CvConnectedComp track_comp;
int hdims = 48; // ??·?HIST?????????????????·
float hranges_arr[] = {0,255};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
bool g_StopFlag=0;
CvScalar hsv2rgb( float hue )
{
int rgb[3], p, sector;
static const int sector_data[][3]=
{{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
hue *= 0.033333333333333333333333333333333f;
sector = cvFloor(hue);
p = cvRound(255*(hue - sector));
p ^= sector & 1 ? 255 : 0;
rgb[sector_data[sector][0]] = 255;
rgb[sector_data[sector][1]] = 0;
rgb[sector_data[sector][2]] = p;
return cvScalar(rgb[2], rgb[1], rgb[0],0);
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
目标检测与跟踪算法程序
#include "stdafx.h"
#include "ParticleMeanShift.h"
#include "ParticleMeanShiftDlg.h"
#include "cvx_defs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <it/io.h>
#include <it/distance.h>
#include <stdio.h>
#include <math.h>
#include <it/wavelet2D.h>
#include <it/mat.h>
//extern "C"{#include "contourlet.h"};
//#ifdef __cplusplus
//extern "C" {#include "contourlet.h}
//#endif
#include "contourlet.h"
#include "dfb.h"
#include "ezbc.h"
#define BUFFER_SIZE (1*1024*1024)
int iFlag_choose=0;
// 9/7 contourlet low subband norm [level]
double norm_low[6] = {
1.000000,
0.982948,
1.030575,
1.051979,
1.058014,
1.058312
};
// 9/7 contourlet high subbands norms [level][dfb_levels][subband]
double norm_high[6][5][16] = {
// DFB
{
{1.000000},
{1.338955, 0.768281},
{1.788734, 1.031742, 1.031699, 0.588007},
{2.350204, 1.388625, 1.473061, 0.755227, 1.521047, 0.718018, 0.760509, 0.466449},
{2.990107, 1.859578, 2.009466, 0.993439, 2.153701, 1.040220, 1.071638, 0.565028,
2.310007, 1.015735, 1.043946, 0.511974, 1.108749, 0.539535, 0.580279, 0.383226}
},
// Highest frequencies
{
{0.759782},
{1.068118, 0.710115},
{1.557636, 0.922336, 0.885625, 0.513870},
{2.066849, 1.199964, 1.312638, 0.679034, 1.314328, 0.611044, 0.667065, 0.406727},
{2.591734, 1.650462, 1.726335, 0.866216, 1.933047, 0.919827, 0.946988, 0.519211,
2.004114, 0.871491, 0.880225, 0.441114, 0.979973, 0.469240, 0.495562, 0.338999}
},
{
{0.709848},
{1.006673, 0.691288},
{1.505208, 0.880912, 0.857108, 0.490243},
{2.004624, 1.154857, 1.268061, 0.637940, 1.281535, 0.585212, 0.641248, 0.383733},
{2.461666, 1.619596, 1.693704, 0.813626, 1.870790, 0.884116, 0.917510, 0.470455,
1.949415, 0.851772, 0.859531, 0.412020, 0.943970, 0.448550, 0.476647, 0.313942}
},
{
{0.753806},
{1.067996, 0.730151},
{1.591337, 0.929780, 0.908668, 0.518957},
{2.142259, 1.210500, 1.324341, 0.681005, 1.369928, 0.613891, 0.670235, 0.410106},
{2.646395, 1.724389, 1.779619, 0.850579, 1.943830, 0.929273, 0.975566, 0.504108,
2.089051, 0.907208, 0.903755, 0.431151, 0.981607, 0.471800, 0.507208, 0.336407}
},
{
{0.775910},
{1.098225, 0.747825},
{1.631322, 0.953360, 0.932293, 0.532706},
{2.204255, 1.236997, 1.351154, 0.702051, 1.409039, 0.627751, 0.684257, 0.422790},
{2.703611, 1.755877, 1.805906, 0.865382, 1.966184, 0.947038, 0.995714, 0.521175,
2.134306, 0.924937, 0.917630, 0.439011, 0.993215, 0.481139, 0.517305, 0.348460}
},
// Lowest frequencies
{
{0.782607},
{1.107343, 0.753079},
{1.643244, 0.959912, 0.939323, 0.536721},
{2.203433, 1.234178, 1.349443, 0.704742, 1.411630, 0.628675, 0.683316, 0.427423},
{2.846164, 2.127241, 1.898790, 1.298897, 2.080579, 1.299276, 1.085920, 0.808484,
2.180500, 1.169607, 0.982991, 0.594266, 1.034878, 0.563380, 0.533165, 0.393168}
}
};
int func(CParticleMeanShiftDlg*dlg);
int func1(CParticleMeanShiftDlg*dlg);
int func2(CParticleMeanShiftDlg*dlg);
int func3(CParticleMeanShiftDlg*dlg);
int func4(CParticleMeanShiftDlg*dlg);
#define region 32
#define calc_point(kalman) \
cvPoint( cvRound(kalman[0]), \
cvRound(kalman[1]))
#define phi2xy(mat) \
cvPoint( cvRound(img->width/2 + img->width/3*cos(mat->data.fl[0])),\
cvRound( img->height/2 - img->width/3*sin(mat->data.fl[0])) )
#define CVCLOSE_ITR 1
#define CVCONTOUR_APPROX_LEVEL 2
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0,*showbackproject;
CvHistogram *hist = 0;
CvHistogram *histtemp = 0;
int backproject_mode = 0;
int select_object = 0;
int track_object = 0;
int show_hist = 1;
CvPoint origin;
CvRect selection;
CvRect track_window;
CvBox2D track_box; // tracking ·????????ò box????????
CvConnectedComp track_comp;
int hdims = 48; // ??·?HIST?????????????????·
float hranges_arr[] = {0,255};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
bool g_StopFlag=0;
CvScalar hsv2rgb( float hue )
{
int rgb[3], p, sector;
static const int sector_data[][3]=
{{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
hue *= 0.033333333333333333333333333333333f;
sector = cvFloor(hue);
p = cvRound(255*(hue - sector));
p ^= sector & 1 ? 255 : 0;
rgb[sector_data[sector][0]] = 255;
rgb[sector_data[sector][1]] = 0;
rgb[sector_data[sector][2]] = p;
return cvScalar(rgb[2], rgb[1], rgb[0],0);
}
部分跟踪结果图
图3是本系统在实际环境中对飞行目标进行连续跟踪的效果图,飞行目标为18个像素,并且对连续跟踪的视频流共截取了6幅图像,分别是第10帧,第30帧,第50帧,第60帧,如图所示:
Frame10(odd field) Frame30(odd field)
Frame50(odd field) Frame60(odd field)
图3部分跟踪结果图
三.总结
本设计通过PIC32单片机实现了对运动目标的检测跟踪,目的是以PIC32单片机做为硬件平台,将算法在PIC32单片机中运行,使其完成对运动目标的准确捕获和稳定跟踪。通过利用USB摄像头完成了图像采集的功能,然后将采集到的图像送入PIC32单片机进行数据处理,利用云台控制器控制云台的转动,使摄像头对准运动目标,进而实现了目标准确跟踪,并且使整个系统趋于小型化智能化。 |