使用ctypes将OpenCV Image作为函数参数传递

时间:2014-09-04 11:35:05

标签: python c++ opencv ctypes

我已经编写了一些优化的C ++代码,用于FLANN与SIFT功能匹配(OpenCV),它返回在两个图像上找到的良好匹配(int)的数量。当我通过char*将两个图像路径(查询和训练图像)作为ctypes传递时,我的代码运行良好。我正在用Python编写一个包装类来处理这些函数。但是,我想将两个参数作为图像实例传递,而不是char*std::string,即Python OpenCV绑定中cv2.imread(apath)结果的对象。

我的.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;
}

我的python 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')

提前致谢

1 个答案:

答案 0 :(得分:0)

解决方案:返回void*并在传递

时再次将其强制转换为cpp对象