00001
00024 #include <vector>
00025 #include <opencv/cv.h>
00026 #include <opencv/highgui.h>
00027 #include "Drawing.h"
00028
00029 using namespace std;
00030 using namespace DUtilsCV;
00031
00032
00033
00034 void Drawing::drawKeyPoints(cv::Mat &image,
00035 const std::vector<cv::KeyPoint> &keypoints)
00036 {
00037 CvScalar color = cvScalar(255, 255, 255);
00038
00039 const float PI = 3.14159265;
00040
00041 vector<cv::KeyPoint>::const_iterator it;
00042
00043 for(it = keypoints.begin(); it != keypoints.end(); ++it)
00044 {
00045 float s = ((9.0f/1.2f) * it->size/10.0f) / 3.0f;
00046 float o = it->angle * PI / 360.f;
00047 if(s < 3.f) s = 3.f;
00048
00049 int r1 = (int)it->pt.y;
00050 int c1 = (int)it->pt.x;
00051
00052 int c2 = (int)(s * cos(o)) + c1;
00053 int r2 = (int)(s * sin(o)) + r1;
00054
00055 cv::line(image, cvPoint(c1, r1), cvPoint(c2, r2), color);
00056 cv::circle(image, cvPoint(c1, r1), (int)s, color, 1);
00057 }
00058 }
00059
00060
00061
00062 void Drawing::saveKeyPointImage(const std::string &filename,
00063 const cv::Mat &image, const std::vector<cv::KeyPoint> &keypoints)
00064 {
00065 cv::Mat im = image.clone();
00066 Drawing::drawKeyPoints(im, keypoints);
00067 cv::imwrite(filename, im);
00068 }
00069
00070
00071
00072 void Drawing::saveCorrespondenceImage(const std::string &filename,
00073 const cv::Mat &im1,
00074 const cv::Mat &im2, const std::vector<cv::KeyPoint> &kp1,
00075 const std::vector<cv::KeyPoint> &kp2,
00076 const std::vector<int> &c1, const std::vector<int> &c2)
00077 {
00078 cv::Mat image;
00079 Drawing::drawCorrespondences(image, im1, im2, kp1, kp2, c1, c2);
00080 cv::imwrite(filename, image);
00081 }
00082
00083
00084
00085 void Drawing::drawCorrespondences(cv::Mat &image, const cv::Mat &img1,
00086 const cv::Mat &img2, const std::vector<cv::KeyPoint> &kp1,
00087 const std::vector<cv::KeyPoint> &kp2,
00088 const std::vector<int> &c1, const std::vector<int> &c2)
00089 {
00090 int rows = img1.rows + img2.rows;
00091 int cols = (img1.cols > img2.cols ? img1.cols : img2.cols);
00092
00093 cv::Mat aux1, aux2;
00094 if(img1.channels() > 1)
00095 cv::cvtColor(img1, aux1, CV_RGB2GRAY);
00096 else
00097 aux1 = img1.clone();
00098
00099 if(img2.channels() > 1)
00100 cv::cvtColor(img2, aux2, CV_RGB2GRAY);
00101 else
00102 aux2 = img2.clone();
00103
00104 Drawing::drawKeyPoints(aux1, kp1);
00105 Drawing::drawKeyPoints(aux2, kp2);
00106
00107 cv::Mat im = cv::Mat::zeros(rows, cols, CV_8UC1);
00108 IplImage ipl_im = IplImage(im);
00109 IplImage* ipl_ret = &ipl_im;
00110
00111 CvRect roi;
00112 roi.x = 0;
00113 roi.y = 0;
00114 roi.width = img1.cols;
00115 roi.height = img1.rows;
00116
00117 cvSetImageROI(ipl_ret, roi);
00118 IplImage ipl_aux1 = IplImage(aux1);
00119 cvCopyImage(&ipl_aux1, ipl_ret);
00120
00121 roi.x = 0;
00122 roi.y = img1.rows;
00123 roi.width = img2.cols;
00124 roi.height = img2.rows;
00125
00126 cvSetImageROI(ipl_ret, roi);
00127 IplImage ipl_aux2 = IplImage(aux2);
00128 cvCopyImage(&ipl_aux2, ipl_ret);
00129
00130 cvResetImageROI(ipl_ret);
00131
00132
00133 cv::cvtColor(im, image, CV_GRAY2RGB);
00134
00135 for(unsigned int i = 0; i < c1.size(); ++i)
00136 {
00137 int mx = (int)kp1[ c1[i] ].pt.x;
00138 int my = (int)kp1[ c1[i] ].pt.y;
00139 int px = (int)kp2[ c2[i] ].pt.x;
00140 int py = (int)kp2[ c2[i] ].pt.y;
00141
00142 py += img1.rows;
00143
00144 CvScalar color = cvScalar(
00145 int(((double)rand()/((double)RAND_MAX + 1.0)) * 256.0),
00146 int(((double)rand()/((double)RAND_MAX + 1.0)) * 256.0),
00147 int(((double)rand()/((double)RAND_MAX + 1.0)) * 256.0));
00148
00149 cv::line(image, cvPoint(mx, my), cvPoint(px, py), color, 1);
00150 }
00151 }
00152
00153
00154
00155 void Drawing::drawReferenceSystem(cv::Mat &image, const cv::Mat &cTo,
00156 const cv::Mat &A, const cv::Mat &K, float length)
00157 {
00158 const cv::Mat cRo(cTo, cv::Range(0,3), cv::Range(0,3));
00159 cv::Mat cto = cTo(cv::Range(0,3), cv::Range(3,4)).clone();
00160
00161 if(cTo.type() == CV_32F)
00162 {
00163 cto.at<float>(0,0) /= cTo.at<float>(3,3);
00164 cto.at<float>(1,0) /= cTo.at<float>(3,3);
00165 cto.at<float>(2,0) /= cTo.at<float>(3,3);
00166 }
00167 else
00168 {
00169 cto.at<double>(0,0) /= cTo.at<double>(3,3);
00170 cto.at<double>(1,0) /= cTo.at<double>(3,3);
00171 cto.at<double>(2,0) /= cTo.at<double>(3,3);
00172 }
00173
00174 Drawing::drawReferenceSystem(image, cRo, cto, A, K, length);
00175 }
00176
00177
00178
00179 void Drawing::drawReferenceSystem(cv::Mat &image, const cv::Mat &cRo,
00180 const cv::Mat &cto, const cv::Mat &A, const cv::Mat &K,
00181 float length)
00182 {
00183 cv::Mat k;
00184 if(K.empty())
00185 k = cv::Mat::zeros(4,1, cRo.type());
00186 else
00187 k = K;
00188
00189 std::vector<cv::Point3f> oP;
00190 oP.push_back(cv::Point3f(0,0,0));
00191 oP.push_back(cv::Point3f(length,0,0));
00192 oP.push_back(cv::Point3f(0,length,0));
00193 oP.push_back(cv::Point3f(0,0,length));
00194
00195 vector<cv::Point2f> points2d;
00196 cv::projectPoints(cv::Mat(oP), cRo, cto, A, k, points2d);
00197
00198
00199 CvScalar bluez, greeny, redx;
00200
00201 if(image.channels() == 3 )
00202 {
00203 bluez = cvScalar(255,0,0);
00204 greeny = cvScalar(0,255,0);
00205 redx = cvScalar(0,0,255);
00206 }
00207 else
00208 {
00209 bluez = cvScalar(18,18,18);
00210 greeny = cvScalar(182,182,182);
00211 redx = cvScalar(120,120,120);
00212 }
00213
00214 cv::line(image, points2d[0], points2d[1], redx, 2);
00215 cv::line(image, points2d[0], points2d[2], greeny, 2);
00216 cv::line(image, points2d[0], points2d[3], bluez, 2);
00217 }
00218
00219
00220
00221 void Drawing::drawBox(cv::Mat &image, const cv::Mat &cRo,
00222 const cv::Mat &cto, float width, float height,
00223 const cv::Mat &A, const cv::Mat &K)
00224 {
00225 cv::Mat k;
00226 if(K.empty())
00227 k = cv::Mat::zeros(4,1, cRo.type());
00228 else
00229 k = K;
00230
00231 const float w = width / 2.f;
00232 const float h = height / 2.f;
00233 cv::Mat oBox = (cv::Mat_<float>(4,3) <<
00234 -w, -h, 0,
00235 w, -h, 0,
00236 w, h, 0,
00237 -w, h, 0);
00238
00239 vector<cv::Point2f> box;
00240 cv::projectPoints(oBox, cRo, cto, A, k, box);
00241
00242 CvScalar color = cvScalar(0,0,255);
00243
00244 cv::line(image, box[0], box[1], color, 2);
00245 cv::line(image, box[1], box[2], color, 2);
00246 cv::line(image, box[2], box[3], color, 2);
00247 cv::line(image, box[3], box[0], color, 2);
00248 }
00249
00250
00251
00252 void Drawing::drawBox(cv::Mat &image, const cv::Mat &sHb, int cols, int rows)
00253 {
00254 cv::Mat P;
00255 vector<cv::Point2f> box(4);
00256
00257 if(sHb.type() == CV_32F)
00258 {
00259 P = sHb * (cv::Mat_<float>(3,4) <<
00260 0, cols, cols, 0,
00261 0, 0, rows, rows,
00262 1, 1, 1, 1);
00263
00264 for(short i = 0; i < 4; ++i)
00265 {
00266 box[i].x = P.at<float>(0,i) / P.at<float>(2,i);
00267 box[i].y = P.at<float>(1,i) / P.at<float>(2,i);
00268 }
00269 }
00270 else
00271 {
00272 P = sHb * (cv::Mat_<double>(3,4) <<
00273 0, cols, cols, 0,
00274 0, 0, rows, rows,
00275 1, 1, 1, 1);
00276
00277 for(short i = 0; i < 4; ++i)
00278 {
00279 box[i].x = P.at<double>(0,i) / P.at<double>(2,i);
00280 box[i].y = P.at<double>(1,i) / P.at<double>(2,i);
00281 }
00282 }
00283
00284 CvScalar color = cvScalar(0,0,255);
00285
00286 cv::line(image, box[0], box[1], color, 2);
00287 cv::line(image, box[1], box[2], color, 2);
00288 cv::line(image, box[2], box[3], color, 2);
00289 cv::line(image, box[3], box[0], color, 2);
00290
00291 }
00292
00293
00294