首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

影像特徵偵測實戰(二):FAST、Lepetit 與 SIFT 多重實作

     編輯:關於.NET

FAST 角點偵測

FAST(Features from Accelerated Segment Test) 由 Rosten 教授提出,以極高速度著稱。OpenCV 的 cvCornerFast 參數包含:影像(8-bit)、閾值(0-255,越大角點越少)、弧長 N(9/10/11/12,通常 9 最佳)、非極大值抑制旗標。EmguCV 的 GetFASTKeypoints 僅保留 threshold 與 nonmaxSuppression 兩參數,N 預設為 9。

MKeyPoint[] keyPoints = imageSourceGrayscale.GetFASTKeypoints(threshold, nonmaxSuppression);
foreach (MKeyPoint kp in keyPoints)
    imageResult.Draw(new CircleF(kp.Point, kp.Size/2), new Bgr(255,0,0), kp.Size/4);

Lepetit 關鍵點

由 Vincent Lepetit 提出,EmguCV 以 LDetector 類別實作。參數包含基礎特徵大小、群聚距離、金字塔層數、視圖數、半徑、閾值、詳細輸出旗標、最大關鍵點數、座標縮放。

LDetector detector = new LDetector();
detector.BaseFeatureSize = ...; detector.NOctaves = ...; // 等參數設定
MKeyPoint[] keyPoints = detector.DetectKeyPoints(imageSourceGrayscale, maxCount, scaleCoords);

SIFT 角點:從 P/Invoke 到 .NET 封裝

SIFT(Scale-Invariant Feature Transform)廣泛用於物體追蹤、影像拼接,但 OpenCV 未內建。David Lowe 提供 C/Matlab 參考實作,而 VLFeat(Andrea Vedaldi & Brian Fulkerson)提供更易整合的 C 版本。使用流程:

  1. vl_sift_new 初始化濾波器(可重複用於同尺寸影像)
  2. vl_sift_process_first_octave + vl_sift_process_next_octave 遍歷八度
  3. 各八度呼叫 vl_sift_detect 取得關鍵點
  4. 對每關鍵點呼叫 vl_sift_calc_keypoint_orientations 取得方向
  5. 對每方向呼叫 vl_sift_calc_keypoint_descriptor 產生 128 維描述子
  6. 結束呼叫 vl_sift_delete 釋放資源

直接 P/Invoke 呼叫範例

IntPtr ptrSiftFilt = VlFeatInvoke.vl_sift_new(w, h, noctaves, nlevels, o_min);
Image<Gray, Single> img = imageSourceGrayscale.ConvertScale<Single>(1, 0);
if (VlFeatInvoke.vl_sift_process_first_octave(ptrSiftFilt, img.MIplImage.imageData) != VL_ERR_EOF) {
    while (true) {
        VlFeatInvoke.vl_sift_detect(ptrSiftFilt);
        VlSiftFilt siftFilt = (VlSiftFilt)Marshal.PtrToStructure(ptrSiftFilt, typeof(VlSiftFilt));
        VlSiftKeypoint* pKeys = (VlSiftKeypoint*)siftFilt.keys.ToPointer();
        for (int i=0; i<siftFilt.nkeys; i++) {
            VlSiftKeypoint kp = *pKeys++;
            // 繪製、計算方向、計算描述子...
        }
        if (VlFeatInvoke.vl_sift_process_next_octave(ptrSiftFilt) == VL_ERR_EOF) break;
    }
}
VlFeatInvoke.vl_sift_delete(ptrSiftFilt);

.NET 封裝:SiftDetector 類別(續上文)

為簡化 .NET 開發,作者將 VLFeat SIFT 部分封裝為 SiftDetector,使用步驟:

  1. 建構函式初始化(可指定尺寸與金字塔參數)
  2. 呼叫 Process(Image<Gray,Single>, SiftDetectorResultType) 取得 List<SiftFeature>
  3. 呼叫 Dispose() 或等待 GC 釋放
SiftDetector detector = new SiftDetector(imageSource.Size, noctaves, nlevels, o_min);
List<SiftFeature> features = detector.Process(imageSourceSingle, SiftDetectorResultType.Extended);
// 繪製與顯示...
detector.Dispose();

VLFeat P/Invoke 完整定義

文末附上完整的 VlSiftKeypoint、VlSiftFilt 結構配置(StructLayout.Sequential)與 VlFeatInvoke 類別,涵蓋所有需匯入的 vl_sift_* 函式簽章(DllImport("vl.dll")),包含錯誤碼常數(VL_ERR_OK、VL_ERR_EOF 等)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved