自动对焦搜索算法(未完)
程序员文章站
2022-03-13 15:57:36
...
自动对焦搜索算法
先黄金搜索再爬山搜索
1、爬山搜索法
2、黄金搜索法
3、代码
// AutoFocus.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <math.h>
#include <halconcpp.h>
using namespace HalconCpp;
using namespace std;
vector<int> GoldenSectionSearch(double m[], int a, int b, double k, int T);
int HillClimbingSearch(double m[], int c, int d);
int EvaluationImage(int k);
//自动对焦主函数
int _tmain(int argc, _TCHAR* argv[])
{
double m[30] = { 0.5, 1.7, 3.4, 5.5, 9.7, 13.5, 17.2, 20.6, 23.1, 25.9, 32.3, 38.7, 43.3, 50.5, 44.1, 37.7, 29.3, 22.7, 19.3, 13.6, 11.4, 8.5, 6.6, 4.7, 3.1, 1.9, 0.9, 0.7 ,0.6, 0.4 };
int a = 0, b = 29;
vector<int> tmp;
tmp = GoldenSectionSearch(m,a,b,0.618,5);
cout << tmp[0] << tmp[1] << endl;
int finalPosition = HillClimbingSearch(m, tmp[0], tmp[1]);
cout << finalPosition << endl;
system("pause");
return 0;
}
//黄金分割粗搜索阶段
/*
参数:已经确定的最大可调范围【a,b】;
一个脉冲移动1um;一个脉冲距离l;
黄金分割比例k=0.618;
已经确定的区间阈值T;
返回值:一个区间 vector()
*/
vector<int> GoldenSectionSearch(double m[], int a, int b, double k, int T)
{
vector<int> range;
if (abs(b - a) < T)
{
range.push_back(a);
range.push_back(b);
return range;
}
int c = b - k*(b - a);
int d = a + k*(b - a);
if (m[c] > m[d])
{
b = d;
return GoldenSectionSearch(m, a, b, k, T);
}
else
{
a = c;
return GoldenSectionSearch(m, a, b, k, T);
}
return range;
}
//爬山精细化搜索阶段
/*
参数:区间【c,d】;
最小脉冲数s;
*/
int HillClimbingSearch(double m[], int c, int d)
{
double max = m[c];
for (int i = 0; i < abs(c - d); i++)
{
double tmp = m[++c];
if (tmp < max)
{
return --c;
}
max = tmp;
}
return -1;
}
//图像评价函数--方差函数--Halocn转C++
int EvaluationImage(int k)
{
// Local iconic variables
HObject ho_Image;
HObject ho_GrayImage, ho_Regions, ho_RegionTrans, ho_ROI;
HObject ho_LU, ho_LD, ho_RU;
// Local control variables
HTuple hv_Width, hv_Height, hv_Row, hv_Column;
HTuple hv_Phi, hv_Length1, hv_Length2, hv_Mean, hv_Deviation;
ReadImage(&ho_Image, "F:/7号屏/W/20200923(较虚焦,实验平台,VP101M)-7号屏-5-W255-曝光时间20ms-OPT平面光源(10)-国产镜头(光圈5.6).bmp");
Rgb1ToGray(ho_Image, &ho_GrayImage);
GetImageSize(ho_GrayImage, &hv_Width, &hv_Height);
Threshold(ho_GrayImage, &ho_Regions, 50, 80);
ShapeTrans(ho_Regions, &ho_RegionTrans, "convex");
SmallestRectangle2(ho_RegionTrans, &hv_Row, &hv_Column, &hv_Phi, &hv_Length1, &hv_Length2);
GenRectangle2(&ho_ROI, hv_Row, hv_Column, hv_Phi, hv_Length1 / 5, hv_Length2 / 5);
GenRectangle2(&ho_LU, hv_Row - (0.6*hv_Length2), hv_Column - (0.6*hv_Length1), hv_Phi,hv_Length1 / 5, hv_Length2 / 5);
Union2(ho_ROI, ho_LU, &ho_ROI);
GenRectangle2(&ho_LD, hv_Row + (0.6*hv_Length2), hv_Column - (0.6*hv_Length1), hv_Phi,hv_Length1 / 5, hv_Length2 / 5);
Union2(ho_ROI, ho_LD, &ho_ROI);
GenRectangle2(&ho_RU, hv_Row - (0.6*hv_Length2), hv_Column + (0.6*hv_Length1), hv_Phi,hv_Length1 / 5, hv_Length2 / 5);
Union2(ho_ROI, ho_RU, &ho_ROI);
GenRectangle2(&ho_LU, hv_Row + (0.6*hv_Length2), hv_Column + (0.6*hv_Length1), hv_Phi,hv_Length1 / 5, hv_Length2 / 5);
Union2(ho_ROI, ho_LU, &ho_ROI);
Intensity(ho_ROI, ho_GrayImage, &hv_Mean, &hv_Deviation);
return hv_Deviation.I();
}