Subversion Repositories OpenCV2-Cookbook

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 6 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#if !defined SOBELEDGES
19
#define SOBELEDGES
20
 
21
#define PI 3.1415926
22
 
23
#include <opencv2/core/core.hpp>
24
#include <opencv2/imgproc/imgproc.hpp>
25
 
26
class EdgeDetector {
27
 
28
  private:
29
 
30
          // original image
31
          cv::Mat img;
32
 
33
          // 16-bit signed int image
34
          cv::Mat sobel;
35
 
36
          // Aperture size of the Sobel kernel
37
          int aperture;
38
 
39
          // Sobel magnitude
40
          cv::Mat sobelMagnitude;
41
 
42
          // Sobel orientation
43
          cv::Mat sobelOrientation;
44
 
45
  public:
46
 
47
          EdgeDetector() : aperture(3) {}
48
 
49
          // Set the aperture size of the kernel
50
          void setAperture(int a) {
51
 
52
                  aperture= a;
53
          }
54
 
55
          // Get the aperture size of the kernel
56
          int getAperture() const {
57
 
58
                  return aperture;
59
          }
60
 
61
          // Compute the Sobel
62
          void computeSobel(const cv::Mat& image, cv::Mat &sobelX=cv::Mat(), cv::Mat &sobelY=cv::Mat()) {
63
 
64
                  // Compute Sobel
65
                  cv::Sobel(image,sobelX,CV_32F,1,0,aperture);
66
                  cv::Sobel(image,sobelY,CV_32F,0,1,aperture);
67
 
68
                  // Compute magnitude and orientation
69
                  cv::cartToPolar(sobelX,sobelY,sobelMagnitude,sobelOrientation);
70
          }
71
 
72
          // Get Sobel magnitude
73
          // Make a copy if called more than once
74
          cv::Mat getMagnitude() {
75
 
76
                  return sobelMagnitude;
77
          }
78
 
79
          // Get Sobel orientation
80
          // Make a copy if called more than once
81
          cv::Mat getOrientation() {
82
 
83
                  return sobelOrientation;
84
          }
85
 
86
          // Get a thresholded binary map
87
          cv::Mat getBinaryMap(double threshold) {
88
 
89
                  cv::Mat bin;           
90
                  cv::threshold(sobelMagnitude,bin,threshold,255,cv::THRESH_BINARY_INV);
91
 
92
                  return bin;
93
          }
94
 
95
          // Get a CV_8U image of the Sobel
96
          cv::Mat getSobelImage() {
97
 
98
                  cv::Mat bin;
99
 
100
                  double minval, maxval;
101
                  cv::minMaxLoc(sobelMagnitude,&minval,&maxval);
102
                  sobelMagnitude.convertTo(bin,CV_8U,255/maxval);
103
 
104
                  return bin;
105
          }
106
 
107
          // Get a CV_8U image of the Sobel orientation
108
          // 1 gray-level = 2 degrees
109
          cv::Mat getSobelOrientationImage() {
110
 
111
                  cv::Mat bin;
112
 
113
                  sobelOrientation.convertTo(bin,CV_8U,90/PI);
114
 
115
                  return bin;
116
          }
117
 
118
};
119
 
120
 
121
#endif