00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033 #include <opencv2/core/core.hpp>
00034 #include <opencv2/imgproc/imgproc.hpp>
00035 #include <boost/shared_ptr.hpp>
00036 #include <Eigen/Dense>
00037
00038 using namespace std;
00039 using namespace cv;
00040 using namespace Eigen;
00041 class Lines
00042 {
00043 public:
00044 vector<Vec4i> end_points;
00045 vector<double> rhos;
00046 vector<double> thetas;
00047 MatrixXi extreme_points;
00048
00049
00050 void calculatePolarCoodinates(void)
00051 {
00052 rhos.clear();
00053 thetas.clear();
00054
00055 for(uint i=0 ; i<end_points.size() ; i++)
00056 {
00057 double x1 = end_points[i][0];
00058 double y1 = end_points[i][1];
00059 double x2 = end_points[i][2];
00060 double y2 = end_points[i][3];
00061
00062 double th = atan2(x1-x2,y2-y1);
00063 double rho = x1*cos(th) + y1*sin(th);
00064
00065
00066 rhos.push_back(rho);
00067 thetas.push_back(th);
00068 }
00069 }
00070
00071
00072 void extractInterval(double rho_max,double rho_min,double theta_max,double theta_min)
00073 {
00074 vector<Vec4i> end_points_local;
00075 vector<double> rhos_local;
00076 vector<double> thetas_local;
00077
00078
00079 for(uint i=0;i<end_points.size();i++)
00080 {
00081 if(thetas[i]>theta_min && thetas[i]<theta_max)
00082 {
00083 end_points_local.push_back(end_points[i]);
00084 rhos_local.push_back(rhos[i]);
00085 thetas_local.push_back(thetas[i]);
00086 }
00087
00088 }
00089
00090 end_points=end_points_local;
00091 rhos=rhos_local;
00092 thetas=thetas_local;
00093 }
00094
00095
00096 MatrixXi extremePoints(int rows , int cols)
00097 {
00098 VectorXi P1(2);
00099 VectorXi P2(2);
00100 extreme_points.resize(20,4);
00101 extreme_points.setOnes();
00102
00103 for (uint i=0; i<20 ; i++)
00104 extreme_points(i,3) = 2;
00105
00106 for (uint i=0 ; i<end_points.size() ; i++)
00107 {
00108
00109 P1 << end_points[i][0] , end_points[i][1];
00110 P2 << end_points[i][2] , end_points[i][3];
00111
00112
00113 double Px,Py;
00114 long double k;
00115
00116
00117 VectorXi l1(2);
00118 l1(0) = 0;
00119 l1(1) = 0;
00120
00121 VectorXi l2(2);
00122 l2(0) = 0;
00123 l2(1) = cols;
00124
00125 VectorXi l3(2);
00126 l3(0) = cols;
00127 l3(1) = rows;
00128
00129 VectorXi l4(2);
00130 l4(0) = 0;
00131 l4(1) = rows;
00132
00133
00134
00135 if (thetas[i]!=0 && thetas[i]!=PI)
00136 {
00137
00138 Py = rows;
00139 k = ( (double)Py-(double)P1(1) )/( (double)P2(1)-(double)P1(1) );
00140 Px = P1(0) + k*(P2(0)-P1(0));
00141
00142 if (Px<=cols && Px>=0)
00143 {
00144 extreme_points(i,0) = Px;
00145 extreme_points(i,1) = Py;
00146 }
00147
00148
00149 Py = 1;
00150 k = ( (double)Py-(double)P1(1) )/( (double)P2(1)-(double)P1(1) );
00151 Px = P1(0) + k*(P2(0)-P1(0));
00152
00153 if (Px<=cols && Px>=0)
00154 {
00155 extreme_points(i,0) = Px;
00156 extreme_points(i,1) = Py;
00157 }
00158 }
00159
00160
00161 if (thetas[i]!=(PI/2) && thetas[i]!=(-PI/2) )
00162 {
00163
00164 Px = 1;
00165 k = ( (double)Px-(double)P1(0) )/( (double)P2(0)-(double)P1(0) );
00166 Py = P1(1) + k*(P2(1)-P1(1));
00167 if (Py>0 && Py<rows)
00168 {
00169 extreme_points(i,2) = Px;
00170 extreme_points(i,3) = Py;
00171 }
00172
00173 Px = cols;
00174 k = ( (double)Px-(double)P1(0) )/( (double)P2(0)-(double)P1(0) );
00175 Py = P1(1) + k*(P2(1)-P1(1));
00176
00177 if (Py>0 && Py<rows)
00178 {
00179 extreme_points(i,2) = Px;
00180 extreme_points(i,3) = Py;
00181 }
00182 }
00183
00184 }
00185
00186
00187 return extreme_points;
00188 }
00189
00190 };