看了FAST算法,用C#实践一遍,先理解,再优化
效果如图:
提取关键点只是第一步,还有特征计算和透视变换,任重道远,慢慢来
部分代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 加速区域测试_关键点
/// </summary>
public class FAST : AKeyPoint {
float Threshold = 0.1f;
public override void Init(params object[] p) {
Threshold = Mathf.Clamp(0.1f, int.Parse(p[0].ToString()), 0.9f);
}
public override IList<AKeyPoint> GetKeyPoint(Texture2D src) {
Color[] inCol = src.GetPixels();
List<AKeyPoint> lst_kp = new List<AKeyPoint>();
int cw = src.width;
int ch = src.height;
Texture2D outTex = new Texture2D(cw,ch);
for (int h = 0; h < ch;h++ ) {
for (int w = 0; w < cw; w++ ) {
int index = h * cw + w;
Color cCol = CVTools.GetCell<Color>(inCol, cw, ch, new CVTools.IntV2(w, h), CVTools.GetCellType.Repeat, Color.black);
var cFast_12 = FAST_12(new CVTools.IntV2(w, h));
int suitCount = 0;
for (int i = 0; i < cFast_12.Length;i++ ) {
Color tKPC = CVTools.GetCell<Color>(inCol, cw, ch, cFast_12[i], CVTools.GetCellType.Repeat, Color.black);
float cDelta = Mathf.Abs(cCol.grayscale - tKPC.grayscale);
if (cDelta > Threshold)
suitCount++;
}
if (suitCount >= cFast_12.Length * 0.75f) {
lst_kp.Add(new FAST(){x = w,y = h});
}
}
}
return lst_kp;
}
public static CVTools.IntV2[] FAST_12(CVTools.IntV2 point) {
CVTools.IntV2[] fast_12 = new CVTools.IntV2[12];
int ix = point.x;
int iy = point.y;
fast_12[0] = new CVTools.IntV2(ix - 1, iy - 3);
fast_12[1] = new CVTools.IntV2(ix, iy - 3);
fast_12[2] = new CVTools.IntV2(ix + 1, iy - 3);
fast_12[3] = new CVTools.IntV2(ix + 3, iy - 1);
fast_12[4] = new CVTools.IntV2(ix + 3, iy);
fast_12[5] = new CVTools.IntV2(ix + 3, iy + 1);
fast_12[6] = new CVTools.IntV2(ix - 1, iy + 3);
fast_12[7] = new CVTools.IntV2(ix, iy + 3);
fast_12[8] = new CVTools.IntV2(ix + 1, iy + 3);
fast_12[9] = new CVTools.IntV2(ix - 3, iy - 1);
fast_12[10] = new CVTools.IntV2(ix - 3, iy);
fast_12[11] = new CVTools.IntV2(ix - 3, iy + 1);
return fast_12;
}
}