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

.2.0+dfsg_contrib_modules_ximgproc_tutorials_prediction

Introduction

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

Examples

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  if ( outFilename.size() == 0 )
63  {
64  namedWindow("edges", 1);
65  imshow("edges", edges);
66  waitKey(0);
67  }
68  else
69  imwrite(outFilename, 255*edges);
70 
71  return 0;
72 }

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::ximgproc::StructuredEdgeDetection::detectEdges
virtual void detectEdges(const Mat &src, Mat &dst) const =0
The function detects edges in src and draw them to dst.
cv::String::size
size_t size() const
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:107
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:390
cv::Mat::size
MatSize size
Definition: mat.hpp:1978
cv::Ptr
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:281
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::ximgproc
Definition: deriche_filter.hpp:44
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:741
cv::Mat::type
int type() const
Returns the type of a matrix element.
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:735
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
Definition: affine.hpp:52
cv::String
Definition: cvstd.hpp:478
utility.hpp