00001 /*------------------------------------------------------------------------------------------*\ 00002 This file contains material supporting chapter 6 of the cookbook: 00003 Computer Vision Programming using the OpenCV Library. 00004 by Robert Laganiere, Packt Publishing, 2011. 00005 00006 This program is free software; permission is hereby granted to use, copy, modify, 00007 and distribute this source code, or portions thereof, for any purpose, without fee, 00008 subject to the restriction that the copyright notice may not be removed 00009 or altered from any source or altered source distribution. 00010 The software is released on an as-is basis and without any warranties of any kind. 00011 In particular, the software is not guaranteed to be fault-tolerant or free from failure. 00012 The author disclaims all warranties with regard to this software, any use, 00013 and any consequent failure, is purely the responsibility of the user. 00014 00015 Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name 00016 \*------------------------------------------------------------------------------------------*/ 00017 00018 #if !defined SOBELEDGES 00019 #define SOBELEDGES 00020 00021 #define PI 3.1415926 00022 00023 #include <opencv2/core/core.hpp> 00024 #include <opencv2/imgproc/imgproc.hpp> 00025 // #include <boost/shared_ptr.hpp> 00026 // 00027 // class Lines 00028 // { 00029 // public: 00030 // vector<Vec4i> end_points; 00031 // vector<double> rhos; 00032 // vector<double> thetas; 00033 // 00034 // void calculatePolarCoodinates(void) 00035 // { 00036 // for(uint i=0;i<end_points.size();i++) 00037 // { 00038 // rhos[i]=0; 00039 // thetas[i]=0; 00040 // rhos[i]=0; 00041 // 00042 // } 00043 // } 00044 // 00045 // void extractInterval(double rho_max,double rho_min,double theta_max,double theta_min) 00046 // { 00047 // vector<Vec4i> end_points_local; 00048 // vector<double> rhos_local; 00049 // vector<double> thetas_local; 00050 // 00051 // 00052 // for(uint i=0;i<end_points.size();i++) 00053 // { 00054 // if(rhos[i]>rho_min && rhos[i]<rho_max) 00055 // { 00056 // if(thetas[i]>theta_min && thetas[i]<theta_max) 00057 // { 00058 // end_points_local.push_back(end_points[i]); 00059 // rhos_local.push_back(rhos[i]) 00060 // thetas_local.push_back(thetas[i]) 00061 // } 00062 // } 00063 // } 00064 // 00065 // end_points=end_points_local; 00066 // rhos=rhos_local; 00067 // thetas=thetas_local; 00068 // } 00069 // }; 00070 00071 class EdgeDetector { 00072 00073 private: 00074 00075 // original image 00076 cv::Mat img; 00077 00078 // 16-bit signed int image 00079 cv::Mat sobel; 00080 00081 // Aperture size of the Sobel kernel 00082 int aperture; 00083 00084 // Sobel magnitude 00085 cv::Mat sobelMagnitude; 00086 00087 // Sobel orientation 00088 cv::Mat sobelOrientation; 00089 00090 public: 00091 00092 EdgeDetector() : aperture(3) {} 00093 00094 // Set the aperture size of the kernel 00095 void setAperture(int a) { 00096 00097 aperture= a; 00098 } 00099 00100 // Get the aperture size of the kernel 00101 int getAperture() const { 00102 00103 return aperture; 00104 } 00105 00106 // Compute the Sobel 00107 void computeSobel(const cv::Mat image, cv::Mat sobelX=cv::Mat(), cv::Mat sobelY=cv::Mat()) { 00108 00109 // Compute Sobel 00110 cv::Sobel(image,sobelX,CV_32F,1,0,aperture); 00111 cv::Sobel(image,sobelY,CV_32F,0,1,aperture); 00112 00113 // Compute magnitude and orientation 00114 cv::cartToPolar(sobelX,sobelY,sobelMagnitude,sobelOrientation); 00115 } 00116 00117 // Get Sobel magnitude 00118 // Make a copy if called more than once 00119 cv::Mat getMagnitude() { 00120 00121 return sobelMagnitude; 00122 } 00123 00124 // Get Sobel orientation 00125 // Make a copy if called more than once 00126 cv::Mat getOrientation() { 00127 00128 return sobelOrientation; 00129 } 00130 00131 // Get a thresholded binary map 00132 cv::Mat getBinaryMap(double threshold) { 00133 00134 cv::Mat bin; 00135 cv::threshold(sobelMagnitude,bin,threshold,255,cv::THRESH_BINARY_INV); 00136 00137 return bin; 00138 } 00139 00140 // Get a CV_8U image of the Sobel 00141 cv::Mat getSobelImage() { 00142 00143 cv::Mat bin; 00144 00145 double minval, maxval; 00146 cv::minMaxLoc(sobelMagnitude,&minval,&maxval); 00147 sobelMagnitude.convertTo(bin,CV_8U,255/maxval); 00148 00149 return bin; 00150 } 00151 00152 // Get a CV_8U image of the Sobel orientation 00153 // 1 gray-level = 2 degrees 00154 cv::Mat getSobelOrientationImage() { 00155 00156 cv::Mat bin; 00157 00158 sobelOrientation.convertTo(bin,CV_8U,90/PI); 00159 00160 return bin; 00161 } 00162 00163 }; 00164 00165 00166 #endif