Skip to content

Commit 287a5e5

Browse files
iindovinaalalek
authored andcommitted
Merge pull request #1994 from iindovina:expose_line_descriptor_python
Expose line descriptor python (#1994) * Created wrappers for other line_descriptor functions/classes * Changes to expose LSD and EDLines params in Python and make line descriptor available in Python
1 parent 320e633 commit 287a5e5

File tree

2 files changed

+85
-39
lines changed

2 files changed

+85
-39
lines changed

modules/line_descriptor/include/opencv2/line_descriptor/descriptor.hpp

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Class' interface is mainly based on the ones of classical detectors and extracto
177177
Feature2d's @ref features2d_main and @ref features2d_match. Retrieved information about lines is
178178
stored in line_descriptor::KeyLine objects.
179179
*/
180-
class CV_EXPORTS BinaryDescriptor : public Algorithm
180+
class CV_EXPORTS_W BinaryDescriptor : public Algorithm
181181
{
182182

183183
public:
@@ -221,33 +221,33 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
221221
/** @brief Create a BinaryDescriptor object with default parameters (or with the ones provided)
222222
and return a smart pointer to it
223223
*/
224-
static Ptr<BinaryDescriptor> createBinaryDescriptor();
224+
CV_WRAP static Ptr<BinaryDescriptor> createBinaryDescriptor();
225225
static Ptr<BinaryDescriptor> createBinaryDescriptor( Params parameters );
226226

227227
/** destructor */
228228
~BinaryDescriptor();
229229

230230
/** @brief Get current number of octaves
231231
*/
232-
int getNumOfOctaves();/*CV_WRAP*/
232+
CV_WRAP int getNumOfOctaves();
233233
/** @brief Set number of octaves
234234
@param octaves number of octaves
235235
*/
236-
void setNumOfOctaves( int octaves );/*CV_WRAP*/
236+
CV_WRAP void setNumOfOctaves( int octaves );
237237
/** @brief Get current width of bands
238238
*/
239-
int getWidthOfBand();/*CV_WRAP*/
239+
CV_WRAP int getWidthOfBand();
240240
/** @brief Set width of bands
241241
@param width width of bands
242242
*/
243-
void setWidthOfBand( int width );/*CV_WRAP*/
243+
CV_WRAP void setWidthOfBand( int width );
244244
/** @brief Get current reduction ratio (used in Gaussian pyramids)
245245
*/
246-
int getReductionRatio();/*CV_WRAP*/
246+
CV_WRAP int getReductionRatio();
247247
/** @brief Set reduction ratio (used in Gaussian pyramids)
248248
@param rRatio reduction ratio
249249
*/
250-
void setReductionRatio( int rRatio );
250+
CV_WRAP void setReductionRatio( int rRatio );
251251

252252
/** @brief Read parameters from a FileNode object and store them
253253
@@ -267,7 +267,7 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
267267
@param keypoints vector that will store extracted lines for one or more images
268268
@param mask mask matrix to detect only KeyLines of interest
269269
*/
270-
void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, const Mat& mask = Mat() );
270+
CV_WRAP void detect( const Mat& image, CV_OUT std::vector<KeyLine>& keypoints, const Mat& mask = Mat() );
271271

272272
/** @overload
273273
@@ -285,7 +285,7 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
285285
@param descriptors
286286
@param returnFloatDescr flag (when set to true, original non-binary descriptors are returned)
287287
*/
288-
void compute( const Mat& image, CV_OUT CV_IN_OUT std::vector<KeyLine>& keylines, CV_OUT Mat& descriptors, bool returnFloatDescr = false ) const;
288+
CV_WRAP void compute( const Mat& image, CV_OUT CV_IN_OUT std::vector<KeyLine>& keylines, CV_OUT Mat& descriptors, bool returnFloatDescr = false ) const;
289289

290290
/** @overload
291291
@@ -432,15 +432,15 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
432432

433433
typedef std::list<Pixel> PixelChain; //each edge is a pixel chain
434434

435-
struct EDLineParam
435+
struct CV_EXPORTS_W_SIMPLE EDLineParam
436436
{
437-
int ksize;
438-
float sigma;
439-
float gradientThreshold;
440-
float anchorThreshold;
441-
int scanIntervals;
442-
int minLineLen;
443-
double lineFitErrThreshold;
437+
CV_PROP_RW int ksize;
438+
CV_PROP_RW float sigma;
439+
CV_PROP_RW float gradientThreshold;
440+
CV_PROP_RW float anchorThreshold;
441+
CV_PROP_RW int scanIntervals;
442+
CV_PROP_RW int minLineLen;
443+
CV_PROP_RW double lineFitErrThreshold;
444444
};
445445

446446
#define RELATIVE_ERROR_FACTOR 100.0
@@ -455,13 +455,19 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
455455
* PS: The linking step of edge detection has a little bit difference with the Edge drawing algorithm
456456
* described in the paper. The edge chain doesn't stop when the pixel direction is changed.
457457
*/
458-
class EDLineDetector
458+
class CV_EXPORTS_W EDLineDetector
459459
{
460460
public:
461-
EDLineDetector();
462-
EDLineDetector( EDLineParam param );
461+
CV_WRAP EDLineDetector();
462+
CV_WRAP_AS(EDLineDetectorWithParams) EDLineDetector( EDLineParam param );
463463
~EDLineDetector();
464464

465+
/** @brief Creates an EDLineDetector object, using smart pointers.
466+
*/
467+
CV_WRAP static Ptr<EDLineDetector> createEDLineDetector();
468+
469+
470+
CV_WRAP_AS(createEDLineDetectorWithParams) static Ptr<EDLineDetector> createEDLineDetector(EDLineParam params);
465471
/*extract edges from image
466472
*image: In, gray image;
467473
*edges: Out, store the edges, each edge is a pixel chain
@@ -477,7 +483,7 @@ class CV_EXPORTS BinaryDescriptor : public Algorithm
477483
int EDline( cv::Mat &image, LineChains &lines );
478484

479485
/** extract line from image, and store them */
480-
int EDline( cv::Mat &image );
486+
CV_WRAP int EDline( cv::Mat &image );
481487

482488
cv::Mat dxImg_; //store the dxImg;
483489

@@ -892,13 +898,38 @@ the one used in *BinaryDescriptor* class, data associated to a line's extremes i
892898
in octave it was extracted from, coincide. KeyLine's field *class_id* is used as an index to
893899
indicate the order of extraction of a line inside a single octave.
894900
*/
901+
struct CV_EXPORTS_W_SIMPLE LSDParam
902+
{
903+
CV_PROP_RW double scale ;
904+
CV_PROP_RW double sigma_scale;
905+
CV_PROP_RW double quant;
906+
CV_PROP_RW double ang_th;
907+
CV_PROP_RW double log_eps;
908+
CV_PROP_RW double density_th ;
909+
CV_PROP_RW int n_bins ;
910+
911+
912+
CV_WRAP LSDParam():scale(0.8),
913+
sigma_scale(0.6),
914+
quant(2.0),
915+
ang_th(22.5),
916+
log_eps(0),
917+
density_th(0.7),
918+
n_bins(1024){}
919+
920+
};
921+
895922
class CV_EXPORTS_W LSDDetector : public Algorithm
896923
{
897924
public:
898925

899926
/* constructor */
900-
/*CV_WRAP*/
901-
LSDDetector()
927+
CV_WRAP LSDDetector() : params()
928+
{
929+
}
930+
;
931+
932+
CV_WRAP_AS(LSDDetectorWithParams) LSDDetector(LSDParam _params) : params(_params)
902933
{
903934
}
904935
;
@@ -907,6 +938,10 @@ LSDDetector()
907938
*/
908939
CV_WRAP static Ptr<LSDDetector> createLSDDetector();
909940

941+
942+
CV_WRAP_AS(createLSDDetectorWithParams) static Ptr<LSDDetector> createLSDDetector(LSDParam params);
943+
944+
910945
/** @brief Detect lines inside an image.
911946
912947
@param image input image
@@ -936,6 +971,9 @@ void detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, int numOct
936971

937972
/* matrices for Gaussian pyramids */
938973
std::vector<cv::Mat> gaussianPyrs;
974+
975+
/* parameters */
976+
LSDParam params;
939977
};
940978

941979
/** @brief furnishes all functionalities for querying a dataset provided by user or internal to
@@ -976,7 +1014,7 @@ candidates \f$\mathcal{N}_i(\mathbf{q})\f$ is obtained. The union of sets
9761014
of **q**. Then, last step of algorithm is computing the Hamming distance between **q** and each
9771015
element in \f$\mathcal{N}(\mathbf{q})\f$, deleting the codes that are distant more that *r* from **q**.
9781016
*/
979-
class CV_EXPORTS BinaryDescriptorMatcher : public Algorithm
1017+
class CV_EXPORTS_W BinaryDescriptorMatcher : public Algorithm
9801018
{
9811019

9821020
public:
@@ -988,7 +1026,7 @@ or from the one internal to class
9881026
@param matches vector to host retrieved matches
9891027
@param mask mask to select which input descriptors must be matched to one in dataset
9901028
*/
991-
void match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
1029+
CV_WRAP void match( const Mat& queryDescriptors, const Mat& trainDescriptors, CV_OUT std::vector<DMatch>& matches, const Mat& mask = Mat() ) const;
9921030

9931031
/** @overload
9941032
@param queryDescriptors query descriptors
@@ -997,7 +1035,7 @@ void match( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vecto
9971035
(the *i*-th mask in vector indicates whether each input query can be matched with descriptors in
9981036
dataset relative to *i*-th image)
9991037
*/
1000-
void match( const Mat& queryDescriptors, std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
1038+
CV_WRAP_AS(matchQuery) void match( const Mat& queryDescriptors, CV_OUT std::vector<DMatch>& matches, const std::vector<Mat>& masks = std::vector<Mat>() );
10011039

10021040
/** @brief For every input query descriptor, retrieve the best *k* matching ones from a dataset provided from
10031041
user or from the one internal to class
@@ -1010,7 +1048,7 @@ user or from the one internal to class
10101048
@param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
10111049
matches for a given query is not inserted in final result)
10121050
*/
1013-
void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
1051+
CV_WRAP void knnMatch( const Mat& queryDescriptors, const Mat& trainDescriptors, CV_OUT std::vector<std::vector<DMatch> >& matches, int k, const Mat& mask = Mat(),
10141052
bool compactResult = false ) const;
10151053

10161054
/** @overload
@@ -1023,7 +1061,7 @@ dataset relative to *i*-th image)
10231061
@param compactResult flag to obtain a compact result (if true, a vector that doesn't contain any
10241062
matches for a given query is not inserted in final result)
10251063
*/
1026-
void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
1064+
CV_WRAP_AS(knnMatchQuery) void knnMatch( const Mat& queryDescriptors, std::vector<std::vector<DMatch> >& matches, int k, const std::vector<Mat>& masks = std::vector<Mat>(),
10271065
bool compactResult = false );
10281066

10291067
/** @brief For every input query descriptor, retrieve, from a dataset provided from user or from the one
@@ -1082,7 +1120,7 @@ void clear() CV_OVERRIDE;
10821120
10831121
The BinaryDescriptorMatcher constructed is able to store and manage 256-bits long entries.
10841122
*/
1085-
BinaryDescriptorMatcher();
1123+
CV_WRAP BinaryDescriptorMatcher();
10861124

10871125
/** destructor */
10881126
~BinaryDescriptorMatcher()
@@ -1314,9 +1352,9 @@ int descrInDS;
13141352
-------------------------------------------------------------------------------------------- */
13151353

13161354
/** struct for drawing options */
1317-
struct CV_EXPORTS DrawLinesMatchesFlags
1355+
struct CV_EXPORTS_W_SIMPLE DrawLinesMatchesFlags
13181356
{
1319-
enum
1357+
CV_PROP_RW enum
13201358
{
13211359
DEFAULT = 0, //!< Output image matrix will be created (Mat::create),
13221360
//!< i.e. existing memory of output image may be reused.
@@ -1345,10 +1383,10 @@ NOT_DRAW_SINGLE_LINES = 2//!< Single keylines will not be drawn.
13451383
@note If both *matchColor* and *singleLineColor* are set to their default values, function draws
13461384
matched lines and line connecting them with same color
13471385
*/
1348-
CV_EXPORTS void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
1349-
const std::vector<DMatch>& matches1to2, Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
1350-
const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
1351-
int flags = DrawLinesMatchesFlags::DEFAULT );
1386+
CV_EXPORTS_W void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& keylines1, const Mat& img2, const std::vector<KeyLine>& keylines2,
1387+
const std::vector<DMatch>& matches1to2, CV_OUT Mat& outImg, const Scalar& matchColor = Scalar::all( -1 ),
1388+
const Scalar& singleLineColor = Scalar::all( -1 ), const std::vector<char>& matchesMask = std::vector<char>(),
1389+
int flags = DrawLinesMatchesFlags::DEFAULT );
13521390

13531391
/** @brief Draws keylines.
13541392
@@ -1358,8 +1396,8 @@ CV_EXPORTS void drawLineMatches( const Mat& img1, const std::vector<KeyLine>& ke
13581396
@param color color of lines to be drawn (if set to defaul value, color is chosen randomly)
13591397
@param flags drawing flags
13601398
*/
1361-
CV_EXPORTS void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, Mat& outImage, const Scalar& color = Scalar::all( -1 ),
1362-
int flags = DrawLinesMatchesFlags::DEFAULT );
1399+
CV_EXPORTS_W void drawKeylines( const Mat& image, const std::vector<KeyLine>& keylines, CV_OUT Mat& outImage, const Scalar& color = Scalar::all( -1 ),
1400+
int flags = DrawLinesMatchesFlags::DEFAULT );
13631401

13641402
//! @}
13651403

modules/line_descriptor/src/LSDDetector.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ Ptr<LSDDetector> LSDDetector::createLSDDetector()
5151
return Ptr<LSDDetector>( new LSDDetector() );
5252
}
5353

54+
Ptr<LSDDetector> LSDDetector::createLSDDetector(LSDParam params)
55+
{
56+
return Ptr<LSDDetector>( new LSDDetector(params) );
57+
}
58+
5459
/* compute Gaussian pyramid of input image */
5560
void LSDDetector::computeGaussianPyramid( const Mat& image, int numOctaves, int scale )
5661
{
@@ -145,7 +150,10 @@ void LSDDetector::detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keyline
145150
lsd->computeGaussianPyramid( image, numOctaves, scale );
146151

147152
/* create an LSD extractor */
148-
cv::Ptr<cv::LineSegmentDetector> ls = cv::createLineSegmentDetector( cv::LSD_REFINE_ADV );
153+
cv::Ptr<cv::LineSegmentDetector> ls = cv::createLineSegmentDetector(
154+
cv::LSD_REFINE_ADV, params.scale, params.sigma_scale,
155+
params.quant, params.ang_th, params.log_eps,
156+
params.density_th, params.n_bins);
149157

150158
/* prepare a vector to host extracted segments */
151159
std::vector<std::vector<cv::Vec4f> > lines_lsd;

0 commit comments

Comments
 (0)