image_histogram.cpp
Go to the documentation of this file.
00001 //
00002 //Written by Masaho Ishida (ishida@jsk.t.u-tokyo.ac.jp) 2011
00003 //
00004 
00005 #include <ros/ros.h>
00006 #include <rospack/rospack.h>
00007 #include <opencv/highgui.h>
00008 #include <cv_bridge/CvBridge.h>
00009 #include <sensor_msgs/Image.h>
00010 
00011 class magnitude_converter
00012 {
00013 private:
00014     ros::NodeHandle _n;
00015     ros::Subscriber _sub;
00016 
00017 public:
00018     magnitude_converter(){
00019         _sub = _n . subscribe("image", 1, &magnitude_converter::image_cb, this);
00020         cv::namedWindow("SrcImage", 0);
00021         cv::namedWindow("Histogram", 0);
00022     }
00023 
00024     virtual ~magnitude_converter(){
00025         _sub . shutdown();
00026     }
00027 
00028 
00029     void image_cb (const sensor_msgs::Image& msg){
00030         sensor_msgs::CvBridge bridge;
00031         bridge . fromImage (msg, "bgr8");
00032         IplImage* src_imgipl;
00033         src_imgipl = bridge . toIpl();
00034         cv::Mat src_img(src_imgipl);
00035 
00036         const int ch_width = 260;
00037         const int sch = src_img . channels();
00038         cv::Mat hist_img(cv::Size(ch_width * sch, 200), CV_8UC3, cv::Scalar::all(255));
00039 
00040         std::vector<cv::MatND> hist(3);
00041         const int hist_size = 256;
00042         const int hdims[] = {hist_size};
00043         const float hranges[] = {0,256};
00044         const float* ranges[] = {hranges};
00045         double max_val = .0;
00046 
00047         if(sch==1) {
00048 
00049             cv::calcHist(&src_img, 1, 0, cv::Mat(), hist[0], 1, hdims, ranges, true, false);
00050             cv::minMaxLoc(hist[0], 0, &max_val);
00051         } else {
00052 
00053             for(int i=0; i<sch; ++i) {
00054                 cv::calcHist(&src_img, 1, &i, cv::Mat(), hist[i], 1, hdims, ranges, true, false);
00055                 double tmp_val;
00056                 cv::minMaxLoc(hist[i], 0, &tmp_val);
00057                 max_val = max_val < tmp_val ? tmp_val : max_val;
00058             }
00059         }
00060 
00061         cv::Scalar color = cv::Scalar::all(100);
00062         for(int i=0; i<sch; i++) {
00063             if(sch==3)
00064                 color = cv::Scalar((0xaa<<i*8)&0x0000ff,(0xaa<<i*8)&0x00ff00,(0xaa<<i*8)&0xff0000, 0);
00065             hist[i] . convertTo(hist[i], hist[i] . type(), max_val ? 200./max_val : 0.,0);
00066             //      hist[i].convertTo(hist[i], hist[i].type(), 1,0);
00067             for(int j=0; j<hist_size; ++j) {
00068                 int bin_w = cv::saturate_cast<int>((double)ch_width/hist_size);
00069                 cv::rectangle(hist_img,
00070                               cv::Point(j*bin_w+(i*ch_width), hist_img . rows),
00071                               cv::Point((j+1)*bin_w+(i*ch_width), hist_img . rows-cv::saturate_cast<int>(hist[i] . at<float>(j))),
00072                               color, -1);
00073             }
00074         }
00075 
00076         cv::imshow("SrcImage", src_img);
00077         cv::imshow("Histogram", hist_img);
00078         cv::waitKey(10);
00079     }
00080 };
00081 
00082 int main (int argc, char **argv){
00083     ros::init (argc, argv, "magnitude_converter");
00084 
00085     magnitude_converter mc;
00086 
00087     ros::spin();
00088 
00089     return 0;
00090 }


opencv_ros_bridge_tutorial
Author(s): Kei Okada
autogenerated on Mon Oct 6 2014 12:08:15