2014-09-04 167 views
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') 

在此先感谢

+0

是否可以接受将指针传递到一个图象时再次将其丢至CPP对象,而不是使所述图像数据吗?我想象应该更容易,更快。 – 101 2014-09-04 12:23:32

回答

0

SOLUTION:返回void*并通过