OpenCV  3.0.0
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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

01.jpg
image
02.jpg
image
03.jpg
image
04.jpg
image
05.jpg
image
06.jpg
image
07.jpg
image
08.jpg
image
09.jpg
image
10.jpg
image
11.jpg
image
12.jpg
image
Note
binarization techniques like Canny edge detector are applicable to edges produced by both algorithms (Sobel and StructuredEdgeDetection::detectEdges).

Source Code

1 #include <opencv2/ximgproc.hpp>
2 #include "opencv2/highgui.hpp"
4 
5 using namespace cv;
6 using namespace cv::ximgproc;
7 
8 const char* keys =
9 {
10  "{i || input image name}"
11  "{m || model name}"
12  "{o || output image name}"
13 };
14 
15 int main( int argc, const char** argv )
16 {
17  bool printHelp = ( argc == 1 );
18  printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "--help" );
19  printHelp = printHelp || ( argc == 2 && std::string(argv[1]) == "-h" );
20 
21  if ( printHelp )
22  {
23  printf("\nThis sample demonstrates structured forests for fast edge detection\n"
24  "Call:\n"
25  " structured_edge_detection -i=in_image_name -m=model_name [-o=out_image_name]\n\n");
26  return 0;
27  }
28 
29  cv::CommandLineParser parser(argc, argv, keys);
30  if ( !parser.check() )
31  {
32  parser.printErrors();
33  return -1;
34  }
35 
36  std::string modelFilename = parser.get<std::string>("m");
37  std::string inFilename = parser.get<std::string>("i");
38  std::string outFilename = parser.get<std::string>("o");
39 
40  cv::Mat image = cv::imread(inFilename, 1);
41  if ( image.empty() )
42  {
43  printf("Cannot read image file: %s\n", inFilename.c_str());
44  return -1;
45  }
46 
47  image.convertTo(image, cv::DataType<float>::type, 1/255.0);
48 
49  cv::Mat edges(image.size(), image.type());
50 
52  createStructuredEdgeDetection(modelFilename);
53  pDollar->detectEdges(image, edges);
54 
55  if ( outFilename == "" )
56  {
57  cv::namedWindow("edges", 1);
58  cv::imshow("edges", edges);
59 
60  cv::waitKey(0);
61  }
62  else
63  cv::imwrite(outFilename, 255*edges);
64 
65  return 0;
66 }
bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Designed for command line parsing.
Definition: utility.hpp:612
Ptr< StructuredEdgeDetection > createStructuredEdgeDetection(const String &model, Ptr< const RFFeatureGetter > howToGetFeatures=Ptr< RFFeatureGetter >())
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:283
Template "trait" class for OpenCV primitive data types.
Definition: traits.hpp:106
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
n-dimensional dense array class
Definition: mat.hpp:730
int waitKey(int delay=0)
Waits for a pressed key.

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 : [31] [77]