1
我已經寫了一些優化的C++代碼FLANN匹配與SIFT功能(OpenCV),返回兩個圖像上找到的匹配數(int
)。當我通過將兩個圖像路徑(查詢和訓練圖像)作爲char*
傳遞時,我的代碼運行良好。我正在Python中編寫一個包裝類來處理這些函數。但是,我想將這兩個參數作爲圖像實例傳遞,而不是char*
或std::string
,即Python OpenCV綁定中cv2.imread(apath)
的結果對象。傳遞OpenCV圖像作爲函數參數與ctypes
我.cpp
源代碼:
//detectors.cpp
#include <stdio.h>
#include <iostream>
#include "string.h"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
///* (extern c) Get good matches using SIFT and FLANN Matcher * ///
extern "C" int get_matches_sift_flann(char* img1, char* img2)
{
Mat img_1 = imread(img1, CV_LOAD_IMAGE_GRAYSCALE);
Mat img_2 = imread(img2, CV_LOAD_IMAGE_GRAYSCALE);
//-- Step 1: Detect the keypoints using SIFT Detector
int minHessian = 400;
SiftFeatureDetector detector(minHessian);
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect(img_1, keypoints_1);
detector.detect(img_2, keypoints_2);
//-- Step 2: Calculate descriptors (feature vectors)
SiftDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;
extractor.compute(img_1, keypoints_1, descriptors_1);
extractor.compute(img_2, keypoints_2, descriptors_2);
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptors_1, descriptors_2, matches);
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for(int i = 0; i < descriptors_1.rows; i++)
{ double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
//-- or a small arbitary value (0.02) in the event that min_dist is very
//-- small)
//-- PS.- radiusMatch can also be used here.
vector<DMatch> good_matches;
for(int i = 0; i < descriptors_1.rows; i++)
{ if(matches[i].distance <= max(2*min_dist, 0.02))
{ good_matches.push_back(matches[i]); }
}
int n = (int) good_matches.size();
return n;
}
我的蟒蛇wrapper.py
模塊
#wrapper module for libdetectors.so
import os
import ctypes as c
libDETECTORS = c.cdll.LoadLibrary('./libdetectors.so')
class CExternalMatchesFunction:
def __init__(self, c_func):
self.c_func = c_func
self.c_func.argtypes = [c.c_char_p, c.c_char_p]
self.c_func.restype = c.c_int
def __call__(self, train_img_filename, query_img_filename):
r = self.c_func(c.c_char_p(train_img_filename), c.c_char_p(query_img_filename))
return r
#initialize wrapped functions
get_matches_sift_flann = CExternalMatchesFunction(libDETECTORS.get_matches_sift_flann)
所有的一切,我想改變CExternalMatchesFunction().c_func.argtypes
像這些圖像對象的列表:
import cv2
img1 = cv2.imread('foo.jpg')
img2 = cv2.imread('boo.jpg')
在此先感謝
是否可以接受將指針傳遞到一個圖象時再次將其丟至CPP對象,而不是使所述圖像數據嗎?我想象應該更容易,更快。 – 101 2014-09-04 12:23:32