change source and recompile

asked 2015-02-22 04:59:47 -0600

jeremyRutman gravatar image

Hi I'd like to change the opencv source a bit and recompile (I'd like access to the hough transform 'accumulator' image, which I see in the modules/imgproc/hough.cpp source but isn't exposed). So what I did was make a copy of the function cv::HoughLines in hough.cpp , called it cv::HoughLinesWithAccumulator and made the corresponding definition in the imgproc.hpp file, then did a cmake which seemed to go ok (although there is some sort of download step, which I hope isn't somehow avoiding my changed code).
Hoping for a miracle (actually hoping for automatic wrapper-creation) I tried calling cv2.HoughLinesWithAccumulator instead of the working cv2.HoughLines call from my python code (which is where I've been using opencv for abt 1 yr) and as expected no such function exists in the opencv module. So I'm downloading a C++ IDE and will try to call my new func from there; if that works, then I'll try to figure out how the python wrappers get made, and add my new function there. Does this sound reasonable, are there some pitfalls and/or easier ways?

edit retag flag offensive close merge delete

Comments

1

take a look here

did you prefix your new method declaration with a proper CV_EXPORTS_W ?

and yes, you will have to rebuild the opencv libs (and re-install cv2.pyd) to make it happen, though i doubt, that you need a c++ ide for this.

berak gravatar imageberak ( 2015-02-22 05:16:24 -0600 )edit

Thanks for your help first off, so far no dice: I had a declaration in include/opencv2/imgproc.hpp, monkeying the declaration that was already there for houghlines, for my version which i called houghlineswithaccumulator (in meantime keeping same arguments)

CV_EXPORTS_W void HoughLinesWithAccumulator( InputArray image, OutputArray lines,
                              double rho, double theta, int threshold,
                              double srn = 0, double stn = 0,
                              double min_theta = 0, double max_theta = CV_PI

I read the link, thx I'm not sure how to resintall cv2.pyd , I tried find -name cv2.pyd and locate cv2.pyd but didn't find it. The C++ ide was just to check whether i can first of all call my new function from C++, next step pyt

jeremyRutman gravatar imagejeremyRutman ( 2015-02-24 05:00:41 -0600 )edit

the usual steps are:

  • cmake
  • make
  • make install (this will copy your new cv2.pyd to the site-packages folder)

then start python, import cv2, and do a help(cv2.HoughLinesWithAccumulator)to see, if it got in.

berak gravatar imageberak ( 2015-02-24 05:06:12 -0600 )edit
1

whoops it looks like i shoudlve realized from your answer that i needed to copy the newly generated cv2.so to /usr/lib/python2.7/dist-package . Having done so I can now call my newly minted function, great! Now I will try changing arguments etc to actually get the output i need.

Just saw your above answer, yes that wouldve taken care of it. Thanks again for help, it looks like i am in right direction for the time being

jeremyRutman gravatar imagejeremyRutman ( 2015-02-24 05:34:30 -0600 )edit

a few birthing pains. I tried adding an output array to my function

void cv::HoughLinesWithAccumulator( InputArray _image, OutputArray _lines, OutputArray _accumulator, double rho, double theta, int threshold, double srn, double stn, double min_theta, double max_theta )

along with the corresponding declaration in the .hpp header. This suffices to break my opencv install - i get the following ImportError: /usr/lib/python2.7/dist-packages/cv2.so: undefined symbol: _ZN2cv25HoughLinesWithAccumulatorERKNS_11_InputArrayERKNS_12_OutputArrayEddidddd

I don't think its related to allocating an array for the accumulator , i tried a few things such as Mat accumulator; accumulator.copyTo(_accumulator) or _accumulator = _image but no dice

jeremyRutman gravatar imagejeremyRutman ( 2015-02-25 07:45:59 -0600 )edit
1

make sure, your declaration and definition match exactly. seems, you compiled something different, than you showed to the python wrapper

"I don't think its related to allocating an array for the accumulator" - don't think so , too. if you do something wrong there, it will either lead to an empty Mat returned, or a runtime crash, not a linker error as above.

berak gravatar imageberak ( 2015-02-25 08:04:12 -0600 )edit

I can't seem to figure it - I added exactly one argument to the .cpp function and same to the .hpp, in the same place, with same type - .hpp entry below , in /opencv_src/modules/imgproc/include/opencv2/imgproc.hpp (and not in the .../include/opencv2/imgproc/imgproc.hpp which it looks like is not the right one):

CV_EXPORTS_W void HoughLinesWithAccumulator( InputArray image, OutputArray lines, OutputArray accumulator,
double rho, double theta, int threshold,
double srn = 0, double stn = 0,
double min_theta = 0, double max_theta = CV_PI );
jeremyRutman gravatar imagejeremyRutman ( 2015-02-25 08:36:36 -0600 )edit
1

ok, seems as tho the cv2.so wasn't getting regenerated after the cmake, make -j2 , and make install. So I wiped out the build directory and am trying again .

jeremyRutman gravatar imagejeremyRutman ( 2015-02-25 11:45:11 -0600 )edit

Now I get a working cv2.so (which i have to copy by hand into /usr/lib/python2.7/dist-packages , make install doesn't seem to do this). But I don't seem to have access to the extra output argument : when i try

lines = cv2.HoughLinesWithAccumulator(edges,1,np.pi/180,200)
lines, arr = cv2.HoughLinesWithAccumulator(edges,1,np.pi/180,200)

the first line is ok while the second gives a 'too many values to unpack' so i guess my OutputArray is not getting picked up by the gen2 and/or hdr_parser scripts. I'll see if I can find output of those guys somewhere.

jeremyRutman gravatar imagejeremyRutman ( 2015-02-25 13:07:55 -0600 )edit
1

another small step forward (i think) - now i get an error 139 in python indicating bad memory access - so I'll try something besides the test I was attempting namely

_image.copyTo(_accumulator);

which was an attempt to get the input image back as output image. I'll try

Mat accumulator;
_accumulator = accumulator;
jeremyRutman gravatar imagejeremyRutman ( 2015-02-26 04:34:52 -0600 )edit