Skip to content

Commit a1b8b30

Browse files
committed
ImageDisplayer on OpenCV
1 parent 177a980 commit a1b8b30

6 files changed

Lines changed: 100 additions & 82 deletions

File tree

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
@@ -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

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

347365
void ImageDisplayer::YUVToRGB(const float* yuv, float* rgb, int spacing) {
@@ -368,10 +386,8 @@ void ImageDisplayer::RGBToYUV(const float* rgb, float* yuv, int spacing) {
368386

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

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

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);
@@ -58,7 +54,6 @@ string GetTimeStamp();
5854
void TimestampModelFile(const string& src_file, const string& dest_file, const string& timestamp);
5955

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

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

68+
#include <opencv2/core/core.hpp>
69+
7370
class ImageDisplayer {
74-
public:
75-
ImageDisplayer();
71+
public:
7672
ImageDisplayer(int width, int height, int num_colors, bool show_separate, const string& name);
7773

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

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

89-
private:
90-
91-
CImgDisplay disp_;
9286
int width_, height_, num_colors_;
9387
bool show_separate_;
9488
string title_;
@@ -97,5 +91,4 @@ class ImageDisplayer {
9791
int num_fov_x_, num_fov_y_;
9892
};
9993

100-
10194
#endif

0 commit comments

Comments
 (0)