# 2、实现方法

## 2.1 算法实现过程

void MainWindow::AutoFocus(HObject ho_Image)
{

HObject  ho_ImagePart00, ho_ImagePart20;
HObject  ho_ImageSub, ho_ImageResult, ho_ImagePart01, ho_ImagePart10;
HObject  ho_ImageSub1, ho_ImageResult1, ho_ImageSub2, ho_ImageResult2;

HTuple  hv_I, hv_Width, hv_Height, hv_WindowID;
HTuple  hv_Value, hv_Deviation;
try
{
GetImageSize(ho_Image,&hv_Width,&hv_Height);
CropPart(ho_Image, &ho_ImagePart00, 0, 0, hv_Width, hv_Height-2);
ConvertImageType(ho_ImagePart00, &ho_ImagePart00, "real");
CropPart(ho_Image, &ho_ImagePart20, 2, 0, hv_Width, hv_Height-2);
ConvertImageType(ho_ImagePart20, &ho_ImagePart20, "real");
SubImage(ho_ImagePart20, ho_ImagePart00, &ho_ImageSub, 1, 0);
MultImage(ho_ImageSub, ho_ImageSub, &ho_ImageResult, 1, 0);
Intensity(ho_ImageResult, ho_ImageResult, &hv_Value, &hv_Deviation);

double d=hv_Deviation.D();
QString strDev=QString::number(d,'f',3);
ui->labDev->setText(strDev);

//记录最大偏差值
if(hv_PreDeviation

## 2.2 模拟采集流程

camera = new CameraCtrl();
connect(this,&MainWindow::ContinuousGrab,camera,&CameraCtrl::HandleContinuousGrab);

//读取图像并调用Brenner算法函数
void CameraCtrl::HandleContinuousGrab()
{
HTuple  hv_I;
HObject  ho_Image;
for (hv_I=1; hv_I<=30; hv_I+=1)
{
Delay_MSec(tInter);
if(isFocus==true)
{
emit hobjectFocus(ho_Image);
}
}
}

void CameraCtrl::Delay_MSec(unsigned int msec)
{
QEventLoop loop;//定义一个新的事件循环
QTimer::singleShot(msec, &loop, SLOT(quit()));//创建单次定时器，槽函数为事件循环的退出函数
loop.exec();//事件循环开始执行，程序会卡在这里，直到定时时间到，本循环被退出
}