edgedetector.h
Go to the documentation of this file.
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 // #include <boost/shared_ptr.hpp>
26 //
27 // class Lines
28 // {
29 // public:
30 // vector<Vec4i> end_points;
31 // vector<double> rhos;
32 // vector<double> thetas;
33 //
34 // void calculatePolarCoodinates(void)
35 // {
36 // for(uint i=0;i<end_points.size();i++)
37 // {
38 // rhos[i]=0;
39 // thetas[i]=0;
40 // rhos[i]=0;
41 //
42 // }
43 // }
44 //
45 // void extractInterval(double rho_max,double rho_min,double theta_max,double theta_min)
46 // {
47 // vector<Vec4i> end_points_local;
48 // vector<double> rhos_local;
49 // vector<double> thetas_local;
50 //
51 //
52 // for(uint i=0;i<end_points.size();i++)
53 // {
54 // if(rhos[i]>rho_min && rhos[i]<rho_max)
55 // {
56 // if(thetas[i]>theta_min && thetas[i]<theta_max)
57 // {
58 // end_points_local.push_back(end_points[i]);
59 // rhos_local.push_back(rhos[i])
60 // thetas_local.push_back(thetas[i])
61 // }
62 // }
63 // }
64 //
65 // end_points=end_points_local;
66 // rhos=rhos_local;
67 // thetas=thetas_local;
68 // }
69 // };
70 
71 class EdgeDetector {
72 
73  private:
74 
75  // original image
76  cv::Mat img;
77 
78  // 16-bit signed int image
79  cv::Mat sobel;
80 
81  // Aperture size of the Sobel kernel
82  int aperture;
83 
84  // Sobel magnitude
85  cv::Mat sobelMagnitude;
86 
87  // Sobel orientation
89 
90  public:
91 
93 
94  // Set the aperture size of the kernel
95  void setAperture(int a) {
96 
97  aperture= a;
98  }
99 
100  // Get the aperture size of the kernel
101  int getAperture() const {
102 
103  return aperture;
104  }
105 
106  // Compute the Sobel
107  void computeSobel(const cv::Mat image, cv::Mat sobelX=cv::Mat(), cv::Mat sobelY=cv::Mat()) {
108 
109  // Compute Sobel
110  cv::Sobel(image,sobelX,CV_32F,1,0,aperture);
111  cv::Sobel(image,sobelY,CV_32F,0,1,aperture);
112 
113  // Compute magnitude and orientation
114  cv::cartToPolar(sobelX,sobelY,sobelMagnitude,sobelOrientation);
115  }
116 
117  // Get Sobel magnitude
118  // Make a copy if called more than once
119  cv::Mat getMagnitude() {
120 
121  return sobelMagnitude;
122  }
123 
124  // Get Sobel orientation
125  // Make a copy if called more than once
126  cv::Mat getOrientation() {
127 
128  return sobelOrientation;
129  }
130 
131  // Get a thresholded binary map
132  cv::Mat getBinaryMap(double threshold) {
133 
134  cv::Mat bin;
135  cv::threshold(sobelMagnitude,bin,threshold,255,cv::THRESH_BINARY_INV);
136 
137  return bin;
138  }
139 
140  // Get a CV_8U image of the Sobel
141  cv::Mat getSobelImage() {
142 
143  cv::Mat bin;
144 
145  double minval, maxval;
146  cv::minMaxLoc(sobelMagnitude,&minval,&maxval);
147  sobelMagnitude.convertTo(bin,CV_8U,255/maxval);
148 
149  return bin;
150  }
151 
152  // Get a CV_8U image of the Sobel orientation
153  // 1 gray-level = 2 degrees
155 
156  cv::Mat bin;
157 
158  sobelOrientation.convertTo(bin,CV_8U,90/PI);
159 
160  return bin;
161  }
162 
163 };
164 
165 
166 #endif
cv::Mat img
Definition: edgedetector.h:76
cv::Mat sobelOrientation
Definition: edgedetector.h:88
void setAperture(int a)
Definition: edgedetector.h:95
cv::Mat sobelMagnitude
Definition: edgedetector.h:85
cv::Mat getSobelOrientationImage()
Definition: edgedetector.h:154
cv::Mat getMagnitude()
Definition: edgedetector.h:119
#define PI
Definition: edgedetector.h:21
cv::Mat getOrientation()
Definition: edgedetector.h:126
int getAperture() const
Definition: edgedetector.h:101
cv::Mat getSobelImage()
Definition: edgedetector.h:141
void computeSobel(const cv::Mat image, cv::Mat sobelX=cv::Mat(), cv::Mat sobelY=cv::Mat())
Definition: edgedetector.h:107
cv::Mat getBinaryMap(double threshold)
Definition: edgedetector.h:132
cv::Mat sobel
Definition: edgedetector.h:79


road_recognition
Author(s): Ricardo Morais
autogenerated on Mon Mar 2 2015 01:32:51