2012-04-25 78 views
1
OpenCV的features2d

我想在iOS上通過OpenCV的使用特徵檢測和我遇到了一個難題:使用iPhone上

features2d依靠highgui highgui無法在iOS內置(或至少不是我能弄清楚)。

這使我相信:features2d只是不能在iOS上使用,而不重寫模塊以去除對cvSaveImage()和cvLoadImage()的調用。這是錯的嗎?任何人遇到這個並解決它?

回答

4

您正在採取錯誤的措辭,您不需要highgui,因爲該庫只是爲了讓您更輕鬆地處理處理結果,您可以簡單地手動執行這些步驟。

例如,考慮這個HOG例如:

#include <iostream> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/objdetect/objdetect.hpp> 
#include <opencv2/highgui/highgui.hpp> 

int 
main(int argc, char *argv[]) 
{ 
    const char *imagename = argc > 1 ? argv[1] : "../../image/pedestrian.png"; 
    cv::Mat img = cv::imread(imagename, 1); 
    if(img.empty()) return -1; 

    cv::HOGDescriptor hog; 
    hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector()); 

    std::vector<cv::Rect> found; 
    // 畫像,検出結果,閾値(SVMのhyper-planeとの距離), 
    // 探索窓の移動距離(Block移動距離の倍數), 
    // 畫像外にはみ出た対象を探すためのpadding, 
    // 探索窓のスケール変化係數,グルーピング係數 
    hog.detectMultiScale(img, found, 0.2, cv::Size(8,8), cv::Size(16,16), 1.05, 2); 

    std::vector<cv::Rect>::const_iterator it = found.begin(); 
    std::cout << "found:" << found.size() << std::endl; 
    for(; it!=found.end(); ++it) { 
    cv::Rect r = *it; 
    // 描畫に際して,検出矩形を若干小さくする 
    r.x += cvRound(r.width*0.1); 
    r.width = cvRound(r.width*0.8); 
    r.y += cvRound(r.height*0.07); 
    r.height = cvRound(r.height*0.8); 
    cv::rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); 
    } 

    // 結果の描畫 
    cv::namedWindow("result", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO); 
    cv::imshow("result", img);  
    cv::waitKey(0); 
} 

它是一個非iOS的環境做,但是你可以簡單地更換所有highgui要求 機iOS的東西。

你可以得到一個很好的形象從這裏處理的OpenCV庫:

http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

所以你應該真正關心的代碼是什麼只是這一部分:

cv::HOGDescriptor hog; 
    hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector()); 

    std::vector<cv::Rect> found; 
    // 畫像,検出結果,閾値(SVMのhyper-planeとの距離), 
    // 探索窓の移動距離(Block移動距離の倍數), 
    // 畫像外にはみ出た対象を探すためのpadding, 
    // 探索窓のスケール変化係數,グルーピング係數 
    hog.detectMultiScale(img, found, 0.2, cv::Size(8,8), cv::Size(16,16), 1.05, 2); 

    std::vector<cv::Rect>::const_iterator it = found.begin(); 
    std::cout << "found:" << found.size() << std::endl; 
    for(; it!=found.end(); ++it) { 
    cv::Rect r = *it; 
    // 描畫に際して,検出矩形を若干小さくする 
    r.x += cvRound(r.width*0.1); 
    r.width = cvRound(r.width*0.8); 
    r.y += cvRound(r.height*0.07); 
    r.height = cvRound(r.height*0.8); 
    cv::rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); 
    } 

對於一個簡介:

// You get your img into a cv mat from the uiimage or whatever. 

    cv::Mat gray_img; 
    cv::cvtColor(img, gray_img, CV_BGR2GRAY); 
    cv::normalize(gray_img, gray_img, 0, 255, cv::NORM_MINMAX); 

    std::vector<cv::KeyPoint> keypoints; 
    std::vector<cv::KeyPoint>::iterator itk; 
    cv::Mat descriptors; 

    // 
    // threshold=0.05, edgeThreshold=10.0 
    cv::SiftFeatureDetector detector(0.05,10.0); 
    detector.detect(gray_img, keypoints); 
    // Brief に基づくディスクリプタ抽出器 
    cv::BriefDescriptorExtractor extractor; 
    cv::Scalar color(50,50,155); 
    extractor.compute(gray_img, keypoints, descriptors); 

    // 32次元の特徴量 x keypoint數 
    for(int i=0; i<descriptors.rows; ++i) { 
    cv::Mat d(descriptors, cv::Rect(0,i,descriptors.cols,1)); 
    std::cout << i << ": " << d << std::endl; 
    } 

而你有你的結果。

+0

感謝您的回答。這對HOGDescriptor和這個功能非常有用,但是如果我想要使用功能檢測,即FAST和BRIEF以及功能匹配,那麼這些功能都是在features2d中。如果我不能使用features2d,那麼我可能只是使用planarDetector,這非常緩慢且不準確。 – 2012-04-25 00:44:42

+0

我不明白你爲什麼不能使用這些,我已經在iPhone上使用簡短和快速。沒有必要使用highgui方便的方法,這就是爲什麼你無法編譯它並不重要,檢查答案上的編輯。 – Pochi 2012-04-25 00:56:01

+0

@JoshuaNoble使用我發佈的鏈接。按照他們的方法作爲基礎,並將圖像處理部分替換爲您想要的任何特徵檢測。如果我記得我正確地使用它的ORB和圖像流,但我也測試了簡短和快速。 – Pochi 2012-04-25 01:01:52