@@ -297,51 +297,69 @@ void AddVectors(vector<float>& a, vector<float>& b) {
297297// ImageDisplayer
298298//
299299
300- void DrawRectange (CImg<float >& img, int xmin, int ymin, int xmax, int ymax, const float * color, int thickness) {
301- for (int i = 0 ; i < thickness; i++) {
302- img.draw_rectangle (xmin-i, ymin-i, xmax+i, ymax+i, color, 1.0 , ~0U );
303- }
304- }
300+ #include < opencv2/imgcodecs.hpp>
301+ #include < opencv2/imgproc.hpp>
302+ #include < opencv2/highgui/highgui.hpp>
305303
306- ImageDisplayer::ImageDisplayer (int width, int height, int num_colors, bool show_separate, const string& title) :
307- width_(width), height_(height), num_colors_(num_colors),
308- show_separate_(show_separate), title_(title) {
309- disp_.set_title (title_.c_str ());
304+ using namespace cv ;
305+
306+ inline void resizeOCV (Mat &img, unsigned int width, unsigned int height) {
307+ Mat out;
308+ resize (img, out, Size (width, height), 0 , 0 , INTER_LINEAR);
309+ img = out;
310310}
311311
312- ImageDisplayer::ImageDisplayer () :
313- width_(0 ), height_(0 ), num_colors_(3 ), show_separate_(false ), title_(" " ) {
312+ Mat image, image1, image2;
313+
314+ ImageDisplayer::ImageDisplayer (int width, int height, int num_colors, bool show_separate, const string& title) :
315+ width_(width),
316+ height_(height),
317+ num_colors_(num_colors),
318+ show_separate_(show_separate),
319+ title_(title) {
314320}
315321
316322void ImageDisplayer::DisplayImage (float * data, int num_images, int image_id) {
317- CImg< float > img ;
318- CreateImage (data, num_images, image_id, img );
319- disp_. set_title (title_.c_str ());
320- img. display (disp_ );
323+ CreateImage (data, num_images, image_id, image) ;
324+ namedWindow (title_. c_str (), WINDOW_AUTOSIZE );
325+ imshow (title_.c_str (), image );
326+ waitKey ( 1 );
321327}
322328
323- void ImageDisplayer::CreateImage (const float * data, int num_images, int image_id, CImg< float >& img ) {
329+ void ImageDisplayer::CreateImage (const float * data, int num_images, int image_id, Mat &image ) {
324330 int num_colors_width = (int )sqrt (num_colors_);
325331 int num_colors_height = (num_colors_ + num_colors_width - 1 ) / num_colors_width;
326- int display_width = show_separate_ ? width_ * num_colors_width: width_;
327- int display_height = show_separate_ ? height_ * num_colors_height: height_;
328- int display_colors = show_separate_ ? 1 : num_colors_;
329-
330- img.assign (display_width, display_height, 1 , display_colors);
331- img.fill (0 );
332- float val;
333- for (int k = 0 ; k < num_colors_; k++) {
334- for (int i = 0 ; i < height_; i++) {
335- for (int j = 0 ; j < width_; j++) {
336- val = data[image_id + num_images * (j + width_ * (i + k * height_))];
337- if (show_separate_) {
338- img (j + (k % num_colors_width) * width_, i + (k / num_colors_width) * height_, 0 , 0 ) = val;
339- } else {
340- img (j, i, 0 , k) = val;
341- }
332+ int display_width = show_separate_ ? width_ * num_colors_width : width_;
333+ int display_height = show_separate_ ? height_ * num_colors_height : height_;
334+ int display_colors_type = show_separate_ ? CV_32FC1 : CV_32FC3;
335+
336+ image.create (display_width, display_height, display_colors_type);
337+ for (int k=0 ; k<num_colors_; k++)
338+ {
339+ int off_color = 0 ;
340+ if (3 ==num_colors_)
341+ {
342+ off_color += 2 -k;
343+ }
344+ for (int i=0 ; i<height_; ++i)
345+ {
346+ int off_width = 0 ;
347+ int off_height = 0 ;
348+ if (show_separate_)
349+ {
350+ off_width = (k % num_colors_width) * width_;
351+ off_height = (k / num_colors_width) * height_;
352+ }
353+ float *im = image.ptr <float >(i + off_height);
354+
355+ for (int j=0 ; j<width_; ++j)
356+ {
357+ float val = data[image_id + num_images * (j + width_ * (i + k * height_))];
358+ im[num_colors_*(j + off_width) + off_color] = val;
342359 }
343360 }
344361 }
362+ normalize (image, image, 0 , 1 , NORM_MINMAX);
345363}
346364
347365void ImageDisplayer::YUVToRGB (const float * yuv, float * rgb, int spacing) {
@@ -368,10 +386,8 @@ void ImageDisplayer::RGBToYUV(const float* rgb, float* yuv, int spacing) {
368386
369387void ImageDisplayer::DisplayWeights (float * data, int size, int num_filters, int display_size, bool yuv) {
370388 int num_filters_w = int (sqrt (num_filters));
371- int num_filters_h = num_filters / num_filters_w + (((num_filters % num_filters_w) > 0 ) ? 1 : 0 );
389+ int num_filters_h = num_filters / num_filters_w + (((num_filters % num_filters_w) > 0 ) ? 1 : 0 );
372390 int data_pos, row, col;
373- CImg<float > img (size * num_filters_w, size * num_filters_h, 1 , 3 );
374- img.fill (0 );
375391 float norm = 0 ;
376392 if (yuv) YUVToRGB (data, data, num_filters * size * size);
377393 for (int f = 0 ; f < num_filters; f++) {
@@ -384,29 +400,38 @@ void ImageDisplayer::DisplayWeights(float* data, int size, int num_filters, int
384400 data[i * num_filters + f] /= norm;
385401 }
386402 }
403+
404+ image.create (size * num_filters_w, size * num_filters_h, CV_32FC3);
387405 for (int f = 0 ; f < num_filters; f++) {
388406 for (int k = 0 ; k < 3 ; k++) {
389407 for (int h = 0 ; h < size; h++) {
390408 for (int w = 0 ; w < size; w++) {
391409 data_pos = f + num_filters * (w + size * (h + size * k));
392410 col = w + size * (f % num_filters_w);
393411 row = h + size * (f / num_filters_w);
394- img (col, row, 0 , k) = data[data_pos];
412+
413+ float *im = image.ptr <float >(row);
414+ im[3 *col+(2 -k)] = data[data_pos];
395415 }
396416 }
397417 }
398418 }
399- const unsigned char color[] = {0 , 0 , 0 };
400- img.resize (display_size, display_size);
419+ normalize (image, image, 0 , 1 , NORM_MINMAX);
420+
421+ const Scalar color (0 , 0 , 0 );
422+ resizeOCV (image, display_size, display_size);
401423 for (int i = 0 ; i < num_filters_w; i++) {
402- int pos = (i * img. width () ) / num_filters_w;
403- img. draw_line ( pos, 0 , pos, img. height ( ), color);
424+ int pos = (i * image. cols / 3 ) / num_filters_w;
425+ line (image, Point ( pos, 0 ), Point ( pos, image. rows ), color);
404426 }
405427 for (int i = 0 ; i < num_filters_h; i++) {
406- int pos = (i * img. height () ) / num_filters_h;
407- img. draw_line ( 0 , pos, img. width () , pos, color);
428+ int pos = (i * image. rows ) / num_filters_h;
429+ line (image, Point ( 0 , pos), Point (image. cols / 3 , pos) , color);
408430 }
409- img.display (disp_);
431+
432+ namedWindow (title_.c_str (), WINDOW_AUTOSIZE);
433+ imshow (title_.c_str (), image);
434+ waitKey (1 );
410435}
411436
412437void ImageDisplayer::SetFOV (int size, int stride, int pad1, int pad2,
@@ -423,16 +448,15 @@ void ImageDisplayer::DisplayLocalization(float* data, float* preds, float* gt, i
423448 int image_id = 0 ;
424449
425450 int num_fovs = num_fov_y_ * num_fov_x_;
426-
427- CImg<float > img;
428- CreateImage (data, num_images, image_id, img);
451+
452+ CreateImage (data, num_images, image_id, image1);
429453 const int image_size = 250 ;
430- img. resize ( image_size, image_size);
454+ resizeOCV (image1, image_size, image_size);
431455
432- CImg< float > img2 = CImg< float >(img );
456+ image2 = image1. clone ( );
433457
434- const float green[] = { 0 , 1 , 0 } ;
435- const float blue[] = { 0 , 0 , 1 } ;
458+ const Scalar green ( 0 , 255 , 0 ) ;
459+ const Scalar blue ( 0 , 0 , 255 ) ;
436460
437461 float fov_x, fov_y;
438462 gt += image_id;
@@ -460,11 +484,14 @@ void ImageDisplayer::DisplayLocalization(float* data, float* preds, float* gt, i
460484 int xmax_preds2 = (int )((xmax_preds + fov_x) * image_size);
461485 int ymax_preds2 = (int )((ymax_preds + fov_y) * image_size);
462486
463- DrawRectange (img, xmin_gt2, ymin_gt2, xmax_gt2, ymax_gt2, green, 3 );
464- DrawRectange (img2, xmin_preds2, ymin_preds2, xmax_preds2, ymax_preds2, blue, 3 );
487+ rectangle (image1, Point ( xmin_gt2, ymin_gt2), Point ( xmax_gt2, ymax_gt2) , green, 3 );
488+ rectangle (image2, Point ( xmin_preds2, ymin_preds2), Point ( xmax_preds2, ymax_preds2) , blue, 3 );
465489 }
466490
467- CImgList<float > img_list (img, img2);
468- img_list.display (disp_);
469-
491+ namedWindow (" Localization1" , WINDOW_AUTOSIZE);
492+ imshow (" Localization1" , image1);
493+ namedWindow (" Localization2" , WINDOW_AUTOSIZE);
494+ imshow (" Localization2" , image2);
495+ waitKey (1 );
470496}
497+
0 commit comments