本文提供基於 VLFeat 函式庫的 SiftDetector 完整 C# 封裝,支援三種偵測層級:Basic(僅關鍵點)、Normal(含方向)、Extended(含方向與 128 維描述子)。核心流程包含:
vl_sift_new,設定影像寬高、金字塔層數(noctaves)、每層間隔(nlevels)、最小八度索引(o_min)。Process 方法依序呼叫 vl_sift_process_first_octave 進入首層,再以 vl_sift_process_next_octave 迭代後續層級。vl_sift_detect 取得關鍵點,透過 VlSiftFilt.keys 指標遍歷。vl_sift_calc_keypoint_orientations 計算主方向(最多 4 個),若為 Extended 模式再對每個方向呼叫 vl_sift_calc_keypoint_descriptor 產生 128 維浮點數描述子。SiftFeature 結構(含 VlSiftKeypoint 與 SiftKeyPointOrientation[]),回傳 List<SiftFeature>。IDisposable,於 Dispose 中呼叫 vl_sift_delete 釋放非受控資源。public class SiftDetector : IDisposable
{
private IntPtr ptrSiftFilt;
// ... 建構函式、屬性、Process 多載、Dispose 實作 ...
}
public struct SiftFeature
{
public VlSiftKeypoint keypoint;
public SiftKeyPointOrientation[] keypointOrientations;
}
public struct SiftKeyPointOrientation
{
public double angle;
public float[] descriptors;
}
public enum SiftDetectorResultType { Basic, Normal, Extended }
OpenCV 的 cvExtractMSER 及 EmguCV 的 Image<TColor,TDepth>.ExtractMSER 實作最大穩定極值區域(MSER)偵測。結果為各區域所有像素點的序列(如矩形區域會依順時針回傳頂點)。範例程式展示參數設定(delta、maxArea、minArea 等)、計時、繪製多邊形與詳細頂點輸出。
Seq<Point>[] regions = imageSource.ExtractMSER(null, ref mserParam, storage);
foreach (Seq<Point> region in regions)
imageResult.DrawPolyline(region.ToArray(), true, new Bgr(255,0,0), 2);
| 特徵類型 | 耗時(ms) | 特徵數 |
|---|---|---|
| Sobel 算子 | 5.99 | N/A |
| 拉普拉斯算子 | 3.13 | N/A |
| Canny 算子 | 3.41 | N/A |
| 霍夫線變換 | 13.71 | 10 |
| 霍夫圓變換 | 78.08 | 0 |
| Harris 角點 | 9.42 | N/A |
| Shi-Tomasi 角點 | 16.98 | 18 |
| 亞像素級角點 | 3.63 | 18 |
| SURF 角點 | 266.27 | 151 |
| Star 關鍵點 | 14.83 | 56 |
| FAST 角點 | 31.30 | 159 |
| SIFT 角點 | 287.52 | 54 |
| MSER 區域 | 40.63 | 2 |
測試環境: AMD Athlon II X2 240、DDR3 4GB、GeForce 9500 GT、Windows 7。
下一篇將介紹如何追蹤本文提及的特徵點(角點)。