OpenCV  4.2.0
Open Source Computer Vision
Structured forests for fast edge detection

Introduction

In this tutorial you will learn how to use structured forests for the purpose of edge detection in an image.

Examples

image
image
image
image
image
image
image
image
image
image
image
image
Note
binarization techniques like Canny edge detector are applicable to edges produced by both algorithms (Sobel and StructuredEdgeDetection::detectEdges).

Source Code

1 /**************************************************************************************
2 The structered edge demo requires you to provide a model.
3 This model can be found at the opencv_extra repository on Github on the following link:
4 https://github.com/opencv/opencv_extra/blob/master/testdata/cv/ximgproc/model.yml.gz
5 ***************************************************************************************/
6 
7 #include <opencv2/ximgproc.hpp>
8 #include "opencv2/highgui.hpp"
10 #include <iostream>
11 
12 using namespace cv;
13 using namespace cv::ximgproc;
14 
15 const char* keys =
16 {
17  "{i || input image name}"
18  "{m || model name}"
19  "{o || output image name}"
20 };
21 
22 int main( int argc, const char** argv )
23 {
24  bool printHelp = ( argc == 1 );
25  printHelp = printHelp || ( argc == 2 && String(argv[1]) == "--help" );
26  printHelp = printHelp || ( argc == 2 && String(argv[1]) == "-h" );
27 
28  if ( printHelp )
29  {
30  std::cout << "\nThis sample demonstrates structured forests for fast edge detection\n"
31  "Call:\n"
32  " structured_edge_detection -i=in_image_name -m=model_name [-o=out_image_name]\n\n";
33  return 0;
34  }
35 
36  CommandLineParser parser(argc, argv, keys);
37  if ( !parser.check() )
38  {
39  parser.printErrors();
40  return -1;
41  }
42 
43  String modelFilename = parser.get<String>("m");
44  String inFilename = parser.get<String>("i");
45  String outFilename = parser.get<String>("o");
46 
47  Mat image = imread(inFilename, 1);
48  if ( image.empty() )
49  CV_Error(Error::StsError, String("Cannot read image file: ") + inFilename);
50 
51  if ( modelFilename.size() == 0)
52  CV_Error(Error::StsError, String("Empty model name"));
53 
54  image.convertTo(image, DataType<float>::type, 1/255.0);
55 
56  Mat edges(image.size(), image.type());
57 
59  createStructuredEdgeDetection(modelFilename);
60  pDollar->detectEdges(image, edges);
61 
62  // computes orientation from edge map
63  Mat orientation_map;
64  pDollar->computeOrientation(edges, orientation_map);
65 
66  // suppress edges
67  Mat edge_nms;
68  pDollar->edgesNms(edges, orientation_map, edge_nms, 2, 0, 1, true);
69 
70  if ( outFilename.size() == 0 )
71  {
72  namedWindow("edges", 1);
73  imshow("edges", edges);
74  namedWindow("edges nms", 1);
75  imshow("edges nms", edge_nms);
76  waitKey(0);
77  }
78  else
79  imwrite(outFilename, 255*edges);
80 
81  return 0;
82 }

Explanation

  1. Load source color image
    cv::Mat image = cv::imread(inFilename, 1);
    if ( image.empty() )
    {
    printf("Cannot read image file: %s\n", inFilename.c_str());
    return -1;
    }
  2. Convert source image to [0;1] range
    image.convertTo(image, cv::DataType<float>::type, 1/255.0);
  3. Run main algorithm
    cv::Mat edges(image.size(), image.type());
    pDollar->detectEdges(image, edges);
  4. Show results
    if ( outFilename == "" )
    {
    cv::namedWindow("edges", 1);
    cv::imshow("edges", edges);
    }
    else
    cv::imwrite(outFilename, 255*edges);

Literature

For more information, refer to the following papers : [Dollar2013] [Lim2013]

cv::String
std::string String
Definition: cvstd.hpp:150
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::Error::StsError
@ StsError
unknown /unspecified error
Definition: base.hpp:71
cv::Mat::convertTo
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
cv::DataType
Template "trait" class for OpenCV primitive data types.
Definition: traits.hpp:112
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::ximgproc::createStructuredEdgeDetection
Ptr< StructuredEdgeDetection > createStructuredEdgeDetection(const String &model, Ptr< const RFFeatureGetter > howToGetFeatures=Ptr< RFFeatureGetter >())
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
ximgproc.hpp
CV_Error
#define CV_Error(code, msg)
Call the error handler.
Definition: base.hpp:320
cv::Mat::size
MatSize size
Definition: mat.hpp:2108
cv::Ptr
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
cv::ximgproc::StructuredEdgeDetection::detectEdges
virtual void detectEdges(cv::InputArray _src, cv::OutputArray _dst) const =0
The function detects edges in src and draw them to dst.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::ximgproc
Definition: brightedges.hpp:47
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:791
cv::Mat::type
int type() const
Returns the type of a matrix element.
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:796
cv::ximgproc::StructuredEdgeDetection::edgesNms
virtual void edgesNms(cv::InputArray edge_image, cv::InputArray orientation_image, cv::OutputArray _dst, int r=2, int s=0, float m=1, bool isParallel=true) const =0
The function edgenms in edge image and suppress edges where edge is stronger in orthogonal direction.
cv::imwrite
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
cv::ximgproc::StructuredEdgeDetection::computeOrientation
virtual void computeOrientation(cv::InputArray _src, cv::OutputArray _dst) const =0
The function computes orientation from edge image.
utility.hpp