Python binding stitcher class

asked 2018-11-18 10:26:56 -0600

LBerger gravatar image

updated 2018-11-18 13:46:13 -0600

My problem is translate this C++ code in python ... I want to generate python binding for :

struct CV_EXPORTS ImageFeatures
{
    int img_idx;
    Size img_size;
    std::vector<KeyPoint> keypoints;
    UMat descriptors;
};

I have done :

struct CV_EXPORTS_W_SIMPLE ImageFeatures
{
    CV_PROP_RW int img_idx;
    CV_PROP_RW Size img_size;
    std::vector<KeyPoint> keypoints;
    CV_PROP_RW UMat descriptors;
    CV_WRAP std::vector<KeyPoint> getKeypoints() { return keypoints; };
};

and it works :

import numpy as np
import cv2 as cv
finder= cv.xfeatures2d_SURF.create()
imgListe=[]
img = cv.imread('f:/images/pano/pano1.jpg')
imgListe.append(img)
img = cv.imread('f:/images/pano/pano2.jpg')
imgListe.append(img)
img = cv.imread('f:/images/pano/pano3.jpg')
imgListe.append(img)
features=[]
for img in imgListe:
    imgFea= cv.detail.computeImageFeatures2(finder,img)
    features.append(imgFea)
matcher=cv.detail.BestOf2NearestMatcher_create(False,0.3,6,6)
p=matcher.apply2(features)
indices=cv.detail.leaveBiggestComponent(features,p,0.3)

My problem is when I add CV_PROP_RW before std::vector<keypoint> keypoints; I have got a compilation error :

10>G:\Lib\opencv\modules\python\src2\cv2.cpp(24): error C2039: 'to': is not a member of 'PyOpenCV_Converter<T,void>'
10>        with
10>        [
10>            T=vector_KeyPoint
10>        ]
10>G:\Lib\opencv\modules\python\src2\cv2.cpp(24): note: see declaration of 'PyOpenCV_Converter<T,void>'
10>        with
10>        [
10>            T=vector_KeyPoint
10>        ]
10>G:\Lib\build\opencv\modules\python_bindings_generator\pyopencv_generated_types.h(72119): note: see reference to function template instantiation 'bool pyopencv_to<vector_KeyPoint>(PyObject *,T &,const char *)' being compiled
10>        with
10>        [
10>            T=vector_KeyPoint
10>        ]
10>G:\Lib\opencv\modules\python\src2\cv2.cpp(24): error C3861: 'to': identifier not found
10>Done building project "opencv_python3.vcxproj" -- FAILED.

Then i add a method to get vector<keypoints> and removed CV_WRAP_RW. Is it only way to get value of c++ vector in python ?

private message : @berak any idea?

edit retag flag offensive close merge delete

Comments

lol, i already promise: you won't get happy with the idea. the solution for #13187 might be:

CV_WRAP_AS(apply) void operator()(some_args)

(but you obviously have found out on your own, i guess ;)

but i'm puzzled. shouldn't the current problem with to() be covered from here ?

can you add the resp. c++ wrapper from build/modules/python_bindings_generator/pyopencv_???.h

also, isn't everything in ImageFeatures "read-only" ?

berak gravatar imageberak ( 2018-11-18 10:45:40 -0600 )edit

hmm, (alternative idea) -- what would be the c++ use-case of the functionality, you're trying to wrap here ?

sometimes, it might be easier, to add a new c++ function (with an "easier to wrap" interface), that does it all under the hood (without having to expose "problematic types"). related: https://github.com/opencv/opencv_cont...

berak gravatar imageberak ( 2018-11-18 10:47:53 -0600 )edit

CV_WRAP_AS(apply) thanks I didn't find it : I was looking how to generate python doc in VS 2017. (update cmake and add BeautifulSoup and -DPYTHON_EXECUTABLE="C:/Program Files (x86)/Microsoft Visual Studio/Shared/Python36_64/python.exe"

I pushed on https://github.com/LaurentBerger/open... Is it enough?

I want to write stitching_detailed in python : Brute force method

LBerger gravatar imageLBerger ( 2018-11-18 11:04:47 -0600 )edit

what do you think of the "alternative" idea? would not that be far easier (and more clean) ?

berak gravatar imageberak ( 2018-11-18 11:36:19 -0600 )edit
1
LBerger gravatar imageLBerger ( 2018-11-18 11:42:17 -0600 )edit
1

"what do you think of the "alternative" idea?" use c++ class and simply wrap as here https://github.com/opencv/opencv_cont...

LBerger gravatar imageLBerger ( 2018-11-18 11:55:56 -0600 )edit
2

Change this:

import numpy as cv

to

import numpy as np
supra56 gravatar imagesupra56 ( 2018-11-18 12:15:38 -0600 )edit

@LBerger, can you add the c++ code for your pipeline to the question ?

(again, imho, wrapping that (with already known types would be far more useful,than having to extend the python / java ??? wrappers, based on your original idea.)

berak gravatar imageberak ( 2018-11-18 12:28:53 -0600 )edit
1

Problem : CV_WRAP_AS(apply) operator() does not generate any python documentation

LBerger gravatar imageLBerger ( 2018-11-18 13:45:11 -0600 )edit
1

work in progress

h=cv.detail_HomographyBasedEstimator()
b, c =h.apply(features,p,None)
ba=cv.detail_BundleAdjusterRay()
ba.setConfThresh(0.3)
rm=np.zeros((3,3),np.uint8)
rm[0,:]=1
rm[1,1:3]=1
for cam in c:
    cam.R=cam.R.astype(np.float32)
ba.setRefinementMask(rm)
b,c = ba.apply(features,p,c) # https://github.com/opencv/opencv/blob/master/samples/cpp/stitching_detailed.cpp#L541
LBerger gravatar imageLBerger ( 2018-11-19 08:42:04 -0600 )edit