Skip to content

Commit 214184c

Browse files
committed
ImageDisplayer on OpenCV
1 parent 2cebf33 commit 214184c

File tree

6 files changed

+100
-82
lines changed

6 files changed

+100
-82
lines changed

INSTALL

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ Dependencies
2121
- OpenCV
2222
Download the source code https://github.com/Itseez/opencv/archive/3.0.0-alpha.zip
2323
To quick install -
24-
$ cmake-gui, set CMAKE_BUILD_TYPE to Release
24+
$ sudo apt-get install libgtk2.0-dev pkg-config # for opencv imshow
25+
$ cmake-gui, set CMAKE_BUILD_TYPE to Release, Configure & Generate
2526
$ cd build directory
2627
$ make -j7
2728
$ sudo make install
2829

29-
- libjpeg
30-
This is often present on standard operating systems since it is used by a lot of programs.
31-
It can be downloaded from http://libjpeg.sourceforge.net/
32-
3330
- This code uses C++11. Some features require gcc >= 4.6.
3431

3532
---------------------------

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ CUDA_LIB=$(CUDA_ROOT)/lib64
1717
CUDAMAT_DIR=$(CURDIR)/cudamat
1818
CXX = g++
1919
LIBFLAGS = -L$(LIB) -L$(CUDA_LIB) -L$(CUDAMAT_DIR)
20-
CPPFLAGS = -I$(INC) -I$(CUDA_INC) -I$(SRC) -Ideps
21-
LINKFLAGS = -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -lhdf5 -ljpeg -lX11 -lpthread -lprotobuf -lcublas -ldl -lgomp -lcudamat -lcudart -Wl,-rpath=$(CUDAMAT_DIR) -Wl,-rpath=$(LIB) -Wl,-rpath=$(CUDA_LIB)
20+
CPPFLAGS = -I$(INC) -I$(CUDA_INC) -I$(SRC)
21+
LINKFLAGS = -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -lopencv_highgui -lhdf5 -lpthread -lprotobuf -lcublas -ldl -lgomp -lcudamat -lcudart -Wl,-rpath=$(CUDAMAT_DIR) -Wl,-rpath=$(LIB) -Wl,-rpath=$(CUDA_LIB)
2222
CXXFLAGS = -O2 -std=c++0x -mtune=native -Wall -Wno-unused-result -Wno-sign-compare -fopenmp
2323

2424
ifeq ($(USE_MPI), yes)

src/convnet.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ void ConvNet::BuildNet() {
211211
if (image_size_y <= 0) image_size_y = model_.patch_size();
212212
if (image_size_x <= 0) image_size_x = model_.patch_size();
213213
if (image_size_t <= 0) image_size_t = 1;
214+
image_size_y_ = image_size_y;
215+
image_size_x_ = image_size_x;
216+
image_size_t_ = image_size_t;
214217
} else {
215218
image_size_y = l->incoming_edge_[0]->GetNumModulesY();
216219
image_size_x = l->incoming_edge_[0]->GetNumModulesX();
@@ -491,7 +494,7 @@ void ConvNet::SetupDataset(const string& train_data_config_file,
491494
train_dataset_ = new DataHandler(model_.train_dataset());
492495
if (localizer_) {
493496
train_dataset_->SetFOV(fov_size_, fov_stride_, fov_pad1_, fov_pad2_,
494-
model_.patch_size(), num_fov_x_, num_fov_y_);
497+
image_size_x_, num_fov_x_, num_fov_y_); // TODO: image_size_y_?
495498
}
496499
SetBatchsize(train_dataset_->GetBatchSize());
497500
int dataset_size = train_dataset_->GetDataSetSize();
@@ -501,7 +504,7 @@ void ConvNet::SetupDataset(const string& train_data_config_file,
501504
val_dataset_ = new DataHandler(model_.valid_dataset());
502505
if (localizer_) {
503506
val_dataset_->SetFOV(fov_size_, fov_stride_, fov_pad1_, fov_pad2_,
504-
model_.patch_size(), num_fov_x_, num_fov_y_);
507+
image_size_x_, num_fov_x_, num_fov_y_); // TODO: image_size_y_?
505508
}
506509
dataset_size = val_dataset_->GetDataSetSize();
507510
val_dataset_->AllocateMemory();
@@ -812,11 +815,9 @@ void ConvNet::TimestampModel() {
812815
}
813816

814817
void ConvNet::SetupLocalizationDisplay() {
815-
int image_size = model_.patch_size();
816-
localization_display_ = new ImageDisplayer(image_size, image_size, 3, false,
817-
"localization");
818+
localization_display_ = new ImageDisplayer(image_size_x_, image_size_y_, 3, false, "localization");
818819
localization_display_->SetFOV(fov_size_, fov_stride_, fov_pad1_, fov_pad2_,
819-
image_size, num_fov_x_, num_fov_y_);
820+
image_size_x_, num_fov_x_, num_fov_y_); // TODO: image_size_y_?
820821
}
821822

822823
void ConvNet::DisplayLocalization() {

src/convnet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ class ConvNet {
168168
int max_iter_, batch_size_, current_iter_, lr_reduce_counter_;
169169
DataHandler *train_dataset_, *val_dataset_;
170170
string checkpoint_dir_, output_file_, model_name_;
171-
ImageDisplayer displayer_;
172171
string model_filename_, timestamp_, log_file_, val_log_file_;
172+
int image_size_x_, image_size_y_, image_size_t_;
173173

174174
// Field of view.
175175
int fov_size_, fov_stride_, fov_pad1_, fov_pad2_;

src/util.cc

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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

327333
void 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

358376
void ImageDisplayer::YUVToRGB(const float* yuv, float* rgb, int spacing) {
@@ -379,10 +397,8 @@ void ImageDisplayer::RGBToYUV(const float* rgb, float* yuv, int spacing) {
379397

380398
void 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

423448
void 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+

src/util.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include "mpi.h"
1313
#endif
1414
#include <string>
15-
#define cimg_use_jpeg
16-
#define cimg_use_lapack
17-
#include "CImg/CImg.h"
1815
#include <stdio.h>
1916
#include <google/protobuf/text_format.h>
2017
#include "convnet_config.pb.h"
@@ -32,7 +29,6 @@
3229
#define MPITAG_WEIGHTGRAD 11
3330
#define MPITAG_TRAINERROR 12
3431

35-
using namespace cimg_library;
3632
using namespace std;
3733

3834
template<class T> void ReadPbtxt(const string& pbtxt_file, T& model);
@@ -59,7 +55,6 @@ string GetTimeStamp();
5955
void TimestampModelFile(const string& src_file, const string& dest_file, const string& timestamp);
6056

6157
bool ReadLines(const string& filename, vector<string>& lines);
62-
void DrawRectange(CImg<float>& img, int xmin, int ymin, int xmax, int ymax, const float* color, int thickness);
6358

6459
// Outputs a string that describes the err_code.
6560
string GetStringError(int err_code);
@@ -71,25 +66,24 @@ void AddVectors(vector<float>& a, vector<float>& b);
7166
// ImageDisplayer
7267
//
7368

69+
#include <opencv2/core/core.hpp>
70+
7471
class ImageDisplayer {
75-
public:
76-
ImageDisplayer();
72+
public:
7773
ImageDisplayer(int width, int height, int num_colors, bool show_separate, const string& name);
7874

7975
void SetTitle(const string& title) {title_ = title;}
8076
void DisplayImage(float* data, int spacing, int image_id);
81-
void CreateImage(const float* data, int num_images, int image_id, CImg<float>& img);
8277
void DisplayWeights(float* data, int size, int num_filters, int display_size, bool yuv = false);
8378
void DisplayLocalization(float* data, float* preds, float* gt, int num_images);
8479
void SetFOV(int size, int stride, int pad1, int pad2, int patch_size, int num_fov_x, int num_fov_y);
85-
80+
81+
private:
82+
void CreateImage(const float* data, int num_images, int image_id, cv::Mat &image);
8683

8784
static void YUVToRGB(const float* yuv, float* rgb, int spacing);
8885
static void RGBToYUV(const float* rgb, float* yuv, int spacing);
8986

90-
private:
91-
92-
CImgDisplay disp_;
9387
int width_, height_, num_colors_;
9488
bool show_separate_;
9589
string title_;
@@ -98,5 +92,4 @@ class ImageDisplayer {
9892
int num_fov_x_, num_fov_y_;
9993
};
10094

101-
10295
#endif

0 commit comments

Comments
 (0)