Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / legacy / src / features2d.cpp
1 /* Original code has been submitted by Liu Liu. Here is the copyright.
2 ----------------------------------------------------------------------------------
3  * An OpenCV Implementation of SURF
4  * Further Information Refer to "SURF: Speed-Up Robust Feature"
5  * Author: Liu Liu
6  * liuliu.1987+opencv@gmail.com
7  *
8  * There are still serveral lacks for this experimental implementation:
9  * 1.The interpolation of sub-pixel mentioned in article was not implemented yet;
10  * 2.A comparision with original libSurf.so shows that the hessian detector is not a 100% match to their implementation;
11  * 3.Due to above reasons, I recommanded the original one for study and reuse;
12  *
13  * However, the speed of this implementation is something comparable to original one.
14  *
15  * Copyright© 2008, Liu Liu All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or
18  * without modification, are permitted provided that the following
19  * conditions are met:
20  *  Redistributions of source code must retain the above
21  *  copyright notice, this list of conditions and the following
22  *  disclaimer.
23  *  Redistributions in binary form must reproduce the above
24  *  copyright notice, this list of conditions and the following
25  *  disclaimer in the documentation and/or other materials
26  *  provided with the distribution.
27  *  The name of Contributor may not be used to endorse or
28  *  promote products derived from this software without
29  *  specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
41  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
43  * OF SUCH DAMAGE.
44  */
45
46 #include "precomp.hpp"
47
48 using namespace cv;
49
50
51
52 CV_IMPL CvSURFParams cvSURFParams(double threshold, int extended)
53 {
54     CvSURFParams params;
55     params.hessianThreshold = threshold;
56     params.extended = extended;
57     params.upright = 0;
58     params.nOctaves = 4;
59     params.nOctaveLayers = 2;
60     return params;
61 }
62
63 CV_IMPL void
64 cvExtractSURF( const CvArr* _img, const CvArr* _mask,
65                CvSeq** _keypoints, CvSeq** _descriptors,
66                CvMemStorage* storage, CvSURFParams params,
67                int useProvidedKeyPts)
68 {
69     Mat img = cvarrToMat(_img), mask;
70     if(_mask)
71         mask = cvarrToMat(_mask);
72     std::vector<KeyPoint> kpt;
73     Mat descr;
74
75     Ptr<Feature2D> surf = Algorithm::create<Feature2D>("Feature2D.SURF");
76     if( !surf )
77         CV_Error(CV_StsNotImplemented, "OpenCV was built without SURF support");
78
79     surf->set("hessianThreshold", params.hessianThreshold);
80     surf->set("nOctaves", params.nOctaves);
81     surf->set("nOctaveLayers", params.nOctaveLayers);
82     surf->set("upright", params.upright != 0);
83     surf->set("extended", params.extended != 0);
84
85     surf->operator()(img, mask, kpt, _descriptors ? _OutputArray(descr) : (OutputArray)noArray(),
86                      useProvidedKeyPts != 0);
87
88     if( _keypoints )
89         *_keypoints = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvSURFPoint), storage);
90
91     if( _descriptors )
92         *_descriptors = cvCreateSeq(0, sizeof(CvSeq), surf->descriptorSize() * CV_ELEM_SIZE(surf->descriptorType()), storage);
93
94     for( size_t i = 0; i < kpt.size(); i++ )
95     {
96         if( _keypoints )
97         {
98             CvSURFPoint pt = cvSURFPoint(kpt[i].pt, kpt[i].class_id, cvRound(kpt[i].size), kpt[i].angle, kpt[i].response);
99             cvSeqPush(*_keypoints, &pt);
100         }
101         if( _descriptors )
102             cvSeqPush(*_descriptors, descr.ptr((int)i));
103     }
104 }
105
106 CV_IMPL CvSeq*
107 cvGetStarKeypoints( const CvArr* _img, CvMemStorage* storage,
108                     CvStarDetectorParams params )
109 {
110     Ptr<StarDetector> star(new StarDetector(params.maxSize, params.responseThreshold,
111                                             params.lineThresholdProjected,
112                                             params.lineThresholdBinarized,
113                                             params.suppressNonmaxSize));
114     std::vector<KeyPoint> kpts;
115     star->detect(cvarrToMat(_img), kpts, Mat());
116
117     CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvStarKeypoint), storage);
118     for( size_t i = 0; i < kpts.size(); i++ )
119     {
120         CvStarKeypoint kpt = cvStarKeypoint(kpts[i].pt, cvRound(kpts[i].size), kpts[i].response);
121         cvSeqPush(seq, &kpt);
122     }
123     return seq;
124 }