Skip to content

Commit 1f8ccc1

Browse files
lhelontravpisarev
authored andcommitted
added edgeboxes algorithm (#1215)
samples added fix edgeboxes_demo fix edgeboxes_demo added edgeboxes bib fix edgeboxes_demo small fixes fix edgeboxes_demo fix warnings fix warnings small fixes detectEdges needs rgb image instead bgr image. Removed unnecessary protection small fixes
1 parent a44a2ba commit 1f8ccc1

File tree

7 files changed

+1313
-0
lines changed

7 files changed

+1313
-0
lines changed

modules/ximgproc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Extended Image Processing
22
=========================
33

44
- Structured Forests
5+
- Edge Boxes
56
- Domain Transform Filter
67
- Guided Filter
78
- Adaptive Manifold Filter

modules/ximgproc/doc/ximgproc.bib

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
@inproceedings{ZitnickECCV14edgeBoxes,
2+
author = {C. Lawrence Zitnick and Piotr Doll{\'a}r},
3+
title = {Edge Boxes: Locating Object Proposals from Edges},
4+
booktitle = {ECCV},
5+
year = {2014},
6+
}
7+
18
@inproceedings{Dollar2013,
29
title={Structured forests for fast edge detection},
310
author={Doll{\'a}r, Piotr and Zitnick, C Lawrence},

modules/ximgproc/include/opencv2/ximgproc.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "ximgproc/disparity_filter.hpp"
4242
#include "ximgproc/sparse_match_interpolator.hpp"
4343
#include "ximgproc/structured_edge_detection.hpp"
44+
#include "ximgproc/edgeboxes.hpp"
4445
#include "ximgproc/seeds.hpp"
4546
#include "ximgproc/segmentation.hpp"
4647
#include "ximgproc/fast_hough_transform.hpp"
@@ -62,6 +63,8 @@
6263
This module contains implementations of modern structured edge detection algorithms,
6364
i.e. algorithms which somehow takes into account pixel affinities in natural images.
6465
66+
@defgroup ximgproc_edgeboxes EdgeBoxes
67+
6568
@defgroup ximgproc_filters Filters
6669
6770
@defgroup ximgproc_superpixel Superpixels
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*M///////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4+
//
5+
// By downloading, copying, installing or using the software you agree to this license.
6+
// If you do not agree to this license, do not download, install,
7+
// copy or use the software.
8+
//
9+
//
10+
// License Agreement
11+
// For Open Source Computer Vision Library
12+
//
13+
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14+
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15+
// Third party copyrights are property of their respective owners.
16+
//
17+
// Redistribution and use in source and binary forms, with or without modification,
18+
// are permitted provided that the following conditions are met:
19+
//
20+
// * Redistribution's of source code must retain the above copyright notice,
21+
// this list of conditions and the following disclaimer.
22+
//
23+
// * Redistribution's in binary form must reproduce the above copyright notice,
24+
// this list of conditions and the following disclaimer in the documentation
25+
// and/or other materials provided with the distribution.
26+
//
27+
// * The name of the copyright holders may not be used to endorse or promote products
28+
// derived from this software without specific prior written permission.
29+
//
30+
// This software is provided by the copyright holders and contributors "as is" and
31+
// any express or implied warranties, including, but not limited to, the implied
32+
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33+
// In no event shall the Intel Corporation or contributors be liable for any direct,
34+
// indirect, incidental, special, exemplary, or consequential damages
35+
// (including, but not limited to, procurement of substitute goods or services;
36+
// loss of use, data, or profits; or business interruption) however caused
37+
// and on any theory of liability, whether in contract, strict liability,
38+
// or tort (including negligence or otherwise) arising in any way out of
39+
// the use of this software, even if advised of the possibility of such damage.
40+
//
41+
//M*/
42+
43+
#ifndef __OPENCV_EDGEBOXES_HPP__
44+
#define __OPENCV_EDGEBOXES_HPP__
45+
46+
#include <opencv2/core.hpp>
47+
48+
namespace cv
49+
{
50+
namespace ximgproc
51+
{
52+
53+
//! @addtogroup ximgproc_edgeboxes
54+
//! @{
55+
56+
// bounding box data structures
57+
typedef struct
58+
{
59+
int x, y, w, h;
60+
float score;
61+
} Box;
62+
63+
typedef std::vector<Box> Boxes;
64+
65+
/** @brief Class implementing EdgeBoxes algorithm from @cite ZitnickECCV14edgeBoxes :
66+
*/
67+
class CV_EXPORTS_W EdgeBoxes : public Algorithm
68+
{
69+
70+
public:
71+
72+
/** @brief Returns array containing proposal boxes.
73+
74+
@param edge_map edge image.
75+
@param orientation_map orientation map.
76+
@param boxes proposal boxes.
77+
*/
78+
CV_WRAP virtual void getBoundingBoxes(InputArray edge_map, InputArray orientation_map, CV_OUT std::vector<Rect> &boxes) = 0;
79+
80+
/** @brief Returns the step size of sliding window search.
81+
*/
82+
CV_WRAP virtual float getAlpha() const = 0;
83+
/** @brief Sets the step size of sliding window search.
84+
*/
85+
CV_WRAP virtual void setAlpha(float value) = 0;
86+
87+
/** @brief Returns the nms threshold for object proposals.
88+
*/
89+
CV_WRAP virtual float getBeta() const = 0;
90+
/** @brief Sets the nms threshold for object proposals.
91+
*/
92+
CV_WRAP virtual void setBeta(float value) = 0;
93+
94+
/** @brief Returns adaptation rate for nms threshold.
95+
*/
96+
CV_WRAP virtual float getEta() const = 0;
97+
/** @brief Sets the adaptation rate for nms threshold.
98+
*/
99+
CV_WRAP virtual void setEta(float value) = 0;
100+
101+
/** @brief Returns the min score of boxes to detect.
102+
*/
103+
CV_WRAP virtual float getMinScore() const = 0;
104+
/** @brief Sets the min score of boxes to detect.
105+
*/
106+
CV_WRAP virtual void setMinScore(float value) = 0;
107+
108+
/** @brief Returns the max number of boxes to detect.
109+
*/
110+
CV_WRAP virtual int getMaxBoxes() const = 0;
111+
/** @brief Sets max number of boxes to detect.
112+
*/
113+
CV_WRAP virtual void setMaxBoxes(int value) = 0;
114+
115+
/** @brief Returns the edge min magnitude.
116+
*/
117+
CV_WRAP virtual float getEdgeMinMag() const = 0;
118+
/** @brief Sets the edge min magnitude.
119+
*/
120+
CV_WRAP virtual void setEdgeMinMag(float value) = 0;
121+
122+
/** @brief Returns the edge merge threshold.
123+
*/
124+
CV_WRAP virtual float getEdgeMergeThr() const = 0;
125+
/** @brief Sets the edge merge threshold.
126+
*/
127+
CV_WRAP virtual void setEdgeMergeThr(float value) = 0;
128+
129+
/** @brief Returns the cluster min magnitude.
130+
*/
131+
CV_WRAP virtual float getClusterMinMag() const = 0;
132+
/** @brief Sets the cluster min magnitude.
133+
*/
134+
CV_WRAP virtual void setClusterMinMag(float value) = 0;
135+
136+
/** @brief Returns the max aspect ratio of boxes.
137+
*/
138+
CV_WRAP virtual float getMaxAspectRatio() const = 0;
139+
/** @brief Sets the max aspect ratio of boxes.
140+
*/
141+
CV_WRAP virtual void setMaxAspectRatio(float value) = 0;
142+
143+
/** @brief Returns the minimum area of boxes.
144+
*/
145+
CV_WRAP virtual float getMinBoxArea() const = 0;
146+
/** @brief Sets the minimum area of boxes.
147+
*/
148+
CV_WRAP virtual void setMinBoxArea(float value) = 0;
149+
150+
/** @brief Returns the affinity sensitivity.
151+
*/
152+
CV_WRAP virtual float getGamma() const = 0;
153+
/** @brief Sets the affinity sensitivity
154+
*/
155+
CV_WRAP virtual void setGamma(float value) = 0;
156+
157+
/** @brief Returns the scale sensitivity.
158+
*/
159+
CV_WRAP virtual float getKappa() const = 0;
160+
/** @brief Sets the scale sensitivity.
161+
*/
162+
CV_WRAP virtual void setKappa(float value) = 0;
163+
164+
};
165+
166+
/** @brief Creates a Edgeboxes
167+
168+
@param alpha step size of sliding window search.
169+
@param beta nms threshold for object proposals.
170+
@param eta adaptation rate for nms threshold.
171+
@param minScore min score of boxes to detect.
172+
@param maxBoxes max number of boxes to detect.
173+
@param edgeMinMag edge min magnitude. Increase to trade off accuracy for speed.
174+
@param edgeMergeThr edge merge threshold. Increase to trade off accuracy for speed.
175+
@param clusterMinMag cluster min magnitude. Increase to trade off accuracy for speed.
176+
@param maxAspectRatio max aspect ratio of boxes.
177+
@param minBoxArea minimum area of boxes.
178+
@param gamma affinity sensitivity.
179+
@param kappa scale sensitivity.
180+
*/
181+
CV_EXPORTS_W Ptr<EdgeBoxes>
182+
createEdgeBoxes(float alpha=0.65f,
183+
float beta=0.75f,
184+
float eta=1,
185+
float minScore=0.01f,
186+
int maxBoxes=10000,
187+
float edgeMinMag=0.1f,
188+
float edgeMergeThr=0.5f,
189+
float clusterMinMag=0.5f,
190+
float maxAspectRatio=3,
191+
float minBoxArea=1000,
192+
float gamma=2,
193+
float kappa=1.5f);
194+
195+
//! @}
196+
197+
}
198+
}
199+
200+
#endif /* __OPENCV_EDGEBOXES_HPP__ */
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
By downloading, copying, installing or using the software you agree to this
3+
license. If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
License Agreement
6+
For Open Source Computer Vision Library
7+
(3-clause BSD License)
8+
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
9+
Third party copyrights are property of their respective owners.
10+
Redistribution and use in source and binary forms, with or without modification,
11+
are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright notice,
13+
this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright notice,
15+
this list of conditions and the following disclaimer in the documentation
16+
and/or other materials provided with the distribution.
17+
* Neither the names of the copyright holders nor the names of the contributors
18+
may be used to endorse or promote products derived from this software
19+
without specific prior written permission.
20+
This software is provided by the copyright holders and contributors "as is" and
21+
any express or implied warranties, including, but not limited to, the implied
22+
warranties of merchantability and fitness for a particular purpose are
23+
disclaimed. In no event shall copyright holders or contributors be liable for
24+
any direct, indirect, incidental, special, exemplary, or consequential damages
25+
(including, but not limited to, procurement of substitute goods or services;
26+
loss of use, data, or profits; or business interruption) however caused
27+
and on any theory of liability, whether in contract, strict liability,
28+
or tort (including negligence or otherwise) arising in any way out of
29+
the use of this software, even if advised of the possibility of such damage.
30+
*/
31+
32+
#include "opencv2/ximgproc.hpp"
33+
#include "opencv2/highgui.hpp"
34+
#include "opencv2/imgproc.hpp"
35+
#include <iostream>
36+
37+
using namespace std;
38+
using namespace cv;
39+
using namespace cv::ximgproc;
40+
41+
static void help()
42+
{
43+
std::cout << std::endl <<
44+
"This sample demonstrates structured edge detection and edgeboxes." << std::endl <<
45+
"Usage:" << std::endl <<
46+
"./edgeboxes_demo [<model>] [<input_image>]" << std::endl;
47+
}
48+
49+
int main(int argc, char **argv)
50+
{
51+
52+
if (argc < 3)
53+
{
54+
help();
55+
return -1;
56+
}
57+
58+
Ptr<StructuredEdgeDetection> pDollar = createStructuredEdgeDetection(argv[1]);
59+
60+
Mat im;
61+
im = imread(argv[2]);
62+
63+
Mat rgb_im;
64+
cvtColor(im, rgb_im, COLOR_BGR2RGB);
65+
rgb_im.convertTo(rgb_im, CV_32F, 1.0 / 255.0f);
66+
67+
Mat edge_im;
68+
pDollar->detectEdges(rgb_im, edge_im);
69+
70+
// computes orientation from edge map
71+
Mat O;
72+
pDollar->computeOrientation(edge_im, O);
73+
74+
// apply edge nms
75+
Mat edge_nms;
76+
pDollar->edgesNms(edge_im, O, edge_nms, 2, 0, 1, true);
77+
78+
std::vector<Rect> boxes;
79+
Ptr<EdgeBoxes> edgeboxes = createEdgeBoxes();
80+
edgeboxes->setMaxBoxes(30);
81+
edgeboxes->getBoundingBoxes(edge_nms, O, boxes);
82+
83+
for(int i = 0; i < (int)boxes.size(); i++)
84+
{
85+
Point p1(boxes[i].x, boxes[i].y), p2(boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height);
86+
Scalar color(0, 255, 0);
87+
rectangle(im, p1, p2, color, 1);
88+
}
89+
90+
imshow("im", im);
91+
waitKey(0);
92+
93+
return 0;
94+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
'''
5+
This sample demonstrates structured edge detection and edgeboxes.
6+
Usage:
7+
edgeboxes_demo.py [<model>] [<input_image>]
8+
'''
9+
10+
import cv2
11+
import numpy as np
12+
import sys
13+
14+
if __name__ == '__main__':
15+
print(__doc__)
16+
17+
model = sys.argv[1]
18+
im = cv2.imread(sys.argv[2])
19+
20+
edge_detection = cv2.ximgproc.createStructuredEdgeDetection(model)
21+
rgb_im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
22+
edges = edge_detection.detectEdges(np.float32(rgb_im) / 255.0)
23+
24+
orimap = edge_detection.computeOrientation(edges)
25+
edges = edge_detection.edgesNms(edges, orimap)
26+
27+
edge_boxes = cv2.ximgproc.createEdgeBoxes()
28+
edge_boxes.setMaxBoxes(30)
29+
boxes = edge_boxes.getBoundingBoxes(edges, orimap)
30+
31+
for b in boxes:
32+
x, y, w, h = b
33+
cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 1, cv2.LINE_AA)
34+
35+
cv2.imshow("edges", edges)
36+
cv2.imshow("edgeboxes", im)
37+
cv2.waitKey(0)
38+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)