Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like MATLAB's colorbar for OpenCV?

Tags:

c++

opencv

matlab

I'm writing a C++ app using the OpenCV 3.0 library. I'm playing with the colormap feature, but I'd like to add a colorbar (see example below from MATLAB) to the image window. Even if it's on the side/external, this way the user has an idea of the range. Any suggestions? Thanks in advance!

Colorbar Example from MATLAB

like image 731
dgreene Avatar asked Mar 03 '15 06:03

dgreene


1 Answers

See here the output

cv::Mat out;
color_map(input_image, out, cv::COLORMAP_JET);

//Function to Draw a color map

void color_map(cv::Mat& input /*CV_32FC1*/, cv::Mat& dest, int color_map){

  int num_bar_w=30;
  int color_bar_w=10;
  int vline=10;

  cv::Mat win_mat(cv::Size(input.cols+num_bar_w+num_bar_w+vline, input.rows), CV_8UC3, cv::Scalar(255,255,255));

  //Input image to
  double Min, Max;
  cv::minMaxLoc(input, &Min, &Max);
  int max_int=ceil(Max);

  std::cout<<" Min "<< Min<<" Max "<< Max<<std::endl;

  input.convertTo(input,CV_8UC3,255.0/(Max-Min),-255.0*Min/(Max-Min));
  input.convertTo(input, CV_8UC3);

  cv::Mat M;
  cv::applyColorMap(input, M, color_map);

  M.copyTo(win_mat(cv::Rect(  0, 0, input.cols, input.rows)));

  //Scale
  cv::Mat num_window(cv::Size(num_bar_w, input.rows), CV_8UC3, cv::Scalar(255,255,255));
  for(int i=0; i<=max_int; i++){
      int j=i*input.rows/max_int;
      cv::putText(num_window, std::to_string(i), cv::Point(5, num_window.rows-j-5),cv::FONT_HERSHEY_SIMPLEX, 0.6 , cv::Scalar(0,0,0), 1 , 2 , false);
  }

  //color bar
  cv::Mat color_bar(cv::Size(color_bar_w, input.rows), CV_8UC3, cv::Scalar(255,255,255));
  cv::Mat cb;
  for(int i=0; i<color_bar.rows; i++){
    for(int j=0; j<color_bar_w; j++){
      int v=255-255*i/color_bar.rows;
      color_bar.at<cv::Vec3b>(i,j)=cv::Vec3b(v,v,v);
    }
  }

  color_bar.convertTo(color_bar, CV_8UC3);
  cv::applyColorMap(color_bar, cb, color_map);
  num_window.copyTo(win_mat(cv::Rect(input.cols+vline+color_bar_w, 0, num_bar_w, input.rows)));
  cb.copyTo(win_mat(cv::Rect(input.cols+vline, 0, color_bar_w, input.rows)));
  dest=win_mat.clone();
}
like image 53
Gayan Brahmanage Avatar answered Oct 04 '22 19:10

Gayan Brahmanage