@@ -308,51 +308,69 @@ void AddVectors(vector<float>& a, vector<float>& b) {
308308// ImageDisplayer
309309//
310310
311- void DrawRectange (CImg<float >& img, int xmin, int ymin, int xmax, int ymax, const float * color, int thickness) {
312- for (int i = 0 ; i < thickness; i++) {
313- img.draw_rectangle (xmin-i, ymin-i, xmax+i, ymax+i, color, 1.0 , ~0U );
314- }
315- }
311+ #include < opencv2/imgcodecs.hpp>
312+ #include < opencv2/imgproc.hpp>
313+ #include < opencv2/highgui/highgui.hpp>
316314
317- ImageDisplayer::ImageDisplayer (int width, int height, int num_colors, bool show_separate, const string& title) :
318- width_(width), height_(height), num_colors_(num_colors),
319- show_separate_(show_separate), title_(title) {
320- disp_.set_title (title_.c_str ());
315+ using namespace cv ;
316+
317+ inline void resizeOCV (Mat &img, unsigned int width, unsigned int height) {
318+ Mat out;
319+ resize (img, out, Size (width, height), 0 , 0 , INTER_LINEAR);
320+ img = out;
321321}
322322
323- ImageDisplayer::ImageDisplayer () :
324- width_(0 ), height_(0 ), num_colors_(3 ), show_separate_(false ), title_(" " ) {
323+ Mat image, image1, image2;
324+
325+ ImageDisplayer::ImageDisplayer (int width, int height, int num_colors, bool show_separate, const string& title) :
326+ width_(width),
327+ height_(height),
328+ num_colors_(num_colors),
329+ show_separate_(show_separate),
330+ title_(title) {
325331}
326332
327333void ImageDisplayer::DisplayImage (float * data, int num_images, int image_id) {
328- CImg< float > img ;
329- CreateImage (data, num_images, image_id, img );
330- disp_. set_title (title_.c_str ());
331- img. display (disp_ );
334+ CreateImage (data, num_images, image_id, image) ;
335+ namedWindow (title_. c_str (), WINDOW_AUTOSIZE );
336+ imshow (title_.c_str (), image );
337+ waitKey ( 1 );
332338}
333339
334- void ImageDisplayer::CreateImage (const float * data, int num_images, int image_id, CImg< float >& img ) {
340+ void ImageDisplayer::CreateImage (const float * data, int num_images, int image_id, Mat &image ) {
335341 int num_colors_width = (int )sqrt (num_colors_);
336342 int num_colors_height = (num_colors_ + num_colors_width - 1 ) / num_colors_width;
337- int display_width = show_separate_ ? width_ * num_colors_width: width_;
338- int display_height = show_separate_ ? height_ * num_colors_height: height_;
339- int display_colors = show_separate_ ? 1 : num_colors_;
340-
341- img.assign (display_width, display_height, 1 , display_colors);
342- img.fill (0 );
343- float val;
344- for (int k = 0 ; k < num_colors_; k++) {
345- for (int i = 0 ; i < height_; i++) {
346- for (int j = 0 ; j < width_; j++) {
347- val = data[image_id + num_images * (j + width_ * (i + k * height_))];
348- if (show_separate_) {
349- img (j + (k % num_colors_width) * width_, i + (k / num_colors_width) * height_, 0 , 0 ) = val;
350- } else {
351- img (j, i, 0 , k) = val;
352- }
343+ int display_width = show_separate_ ? width_ * num_colors_width : width_;
344+ int display_height = show_separate_ ? height_ * num_colors_height : height_;
345+ int display_colors_type = show_separate_ ? CV_32FC1 : CV_32FC3;
346+
347+ image.create (display_width, display_height, display_colors_type);
348+ for (int k=0 ; k<num_colors_; k++)
349+ {
350+ int off_color = 0 ;
351+ if (3 ==num_colors_)
352+ {
353+ off_color += 2 -k;
354+ }
355+ for (int i=0 ; i<height_; ++i)
356+ {
357+ int off_width = 0 ;
358+ int off_height = 0 ;
359+ if (show_separate_)
360+ {
361+ off_width = (k % num_colors_width) * width_;
362+ off_height = (k / num_colors_width) * height_;
363+ }
364+ float *im = image.ptr <float >(i + off_height);
365+
366+ for (int j=0 ; j<width_; ++j)
367+ {
368+ float val = data[image_id + num_images * (j + width_ * (i + k * height_))];
369+ im[num_colors_*(j + off_width) + off_color] = val;
353370 }
354371 }
355372 }
373+ normalize (image, image, 0 , 1 , NORM_MINMAX);
356374}
357375
358376void ImageDisplayer::YUVToRGB (const float * yuv, float * rgb, int spacing) {
@@ -379,10 +397,8 @@ void ImageDisplayer::RGBToYUV(const float* rgb, float* yuv, int spacing) {
379397
380398void ImageDisplayer::DisplayWeights (float * data, int size, int num_filters, int display_size, bool yuv) {
381399 int num_filters_w = int (sqrt (num_filters));
382- int num_filters_h = num_filters / num_filters_w + (((num_filters % num_filters_w) > 0 ) ? 1 : 0 );
400+ int num_filters_h = num_filters / num_filters_w + (((num_filters % num_filters_w) > 0 ) ? 1 : 0 );
383401 int data_pos, row, col;
384- CImg<float > img (size * num_filters_w, size * num_filters_h, 1 , 3 );
385- img.fill (0 );
386402 float norm = 0 ;
387403 if (yuv) YUVToRGB (data, data, num_filters * size * size);
388404 for (int f = 0 ; f < num_filters; f++) {
@@ -395,29 +411,38 @@ void ImageDisplayer::DisplayWeights(float* data, int size, int num_filters, int
395411 data[i * num_filters + f] /= norm;
396412 }
397413 }
414+
415+ image.create (size * num_filters_w, size * num_filters_h, CV_32FC3);
398416 for (int f = 0 ; f < num_filters; f++) {
399417 for (int k = 0 ; k < 3 ; k++) {
400418 for (int h = 0 ; h < size; h++) {
401419 for (int w = 0 ; w < size; w++) {
402420 data_pos = f + num_filters * (w + size * (h + size * k));
403421 col = w + size * (f % num_filters_w);
404422 row = h + size * (f / num_filters_w);
405- img (col, row, 0 , k) = data[data_pos];
423+
424+ float *im = image.ptr <float >(row);
425+ im[3 *col+(2 -k)] = data[data_pos];
406426 }
407427 }
408428 }
409429 }
410- const unsigned char color[] = {0 , 0 , 0 };
411- img.resize (display_size, display_size);
430+ normalize (image, image, 0 , 1 , NORM_MINMAX);
431+
432+ const Scalar color (0 , 0 , 0 );
433+ resizeOCV (image, display_size, display_size);
412434 for (int i = 0 ; i < num_filters_w; i++) {
413- int pos = (i * img. width () ) / num_filters_w;
414- img. draw_line ( pos, 0 , pos, img. height ( ), color);
435+ int pos = (i * image. cols / 3 ) / num_filters_w;
436+ line (image, Point ( pos, 0 ), Point ( pos, image. rows ), color);
415437 }
416438 for (int i = 0 ; i < num_filters_h; i++) {
417- int pos = (i * img. height () ) / num_filters_h;
418- img. draw_line ( 0 , pos, img. width () , pos, color);
439+ int pos = (i * image. rows ) / num_filters_h;
440+ line (image, Point ( 0 , pos), Point (image. cols / 3 , pos) , color);
419441 }
420- img.display (disp_);
442+
443+ namedWindow (title_.c_str (), WINDOW_AUTOSIZE);
444+ imshow (title_.c_str (), image);
445+ waitKey (1 );
421446}
422447
423448void ImageDisplayer::SetFOV (int size, int stride, int pad1, int pad2,
@@ -434,16 +459,15 @@ void ImageDisplayer::DisplayLocalization(float* data, float* preds, float* gt, i
434459 int image_id = 0 ;
435460
436461 int num_fovs = num_fov_y_ * num_fov_x_;
437-
438- CImg<float > img;
439- CreateImage (data, num_images, image_id, img);
462+
463+ CreateImage (data, num_images, image_id, image1);
440464 const int image_size = 250 ;
441- img. resize ( image_size, image_size);
465+ resizeOCV (image1, image_size, image_size);
442466
443- CImg< float > img2 = CImg< float >(img );
467+ image2 = image1. clone ( );
444468
445- const float green[] = { 0 , 1 , 0 } ;
446- const float blue[] = { 0 , 0 , 1 } ;
469+ const Scalar green ( 0 , 255 , 0 ) ;
470+ const Scalar blue ( 0 , 0 , 255 ) ;
447471
448472 float fov_x, fov_y;
449473 gt += image_id;
@@ -471,11 +495,14 @@ void ImageDisplayer::DisplayLocalization(float* data, float* preds, float* gt, i
471495 int xmax_preds2 = (int )((xmax_preds + fov_x) * image_size);
472496 int ymax_preds2 = (int )((ymax_preds + fov_y) * image_size);
473497
474- DrawRectange (img, xmin_gt2, ymin_gt2, xmax_gt2, ymax_gt2, green, 3 );
475- DrawRectange (img2, xmin_preds2, ymin_preds2, xmax_preds2, ymax_preds2, blue, 3 );
498+ rectangle (image1, Point ( xmin_gt2, ymin_gt2), Point ( xmax_gt2, ymax_gt2) , green, 3 );
499+ rectangle (image2, Point ( xmin_preds2, ymin_preds2), Point ( xmax_preds2, ymax_preds2) , blue, 3 );
476500 }
477501
478- CImgList<float > img_list (img, img2);
479- img_list.display (disp_);
480-
502+ namedWindow (" Localization1" , WINDOW_AUTOSIZE);
503+ imshow (" Localization1" , image1);
504+ namedWindow (" Localization2" , WINDOW_AUTOSIZE);
505+ imshow (" Localization2" , image2);
506+ waitKey (1 );
481507}
508+
0 commit comments