$search
00001 /* 00002 * Software License Agreement (Modified BSD License) 00003 * 00004 * Copyright (c) 2012, PAL Robotics, S.L. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of PAL Robotics, S.L. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00038 #include <ros/ros.h> 00039 00040 #include "histogram.h" 00041 00042 #include <boost/foreach.hpp> 00043 #include <opencv2/highgui/highgui.hpp> 00044 00045 namespace pal_vision_util 00046 { 00047 void calcHSVHist(const cv::Mat& src, cv::MatND& histogram) 00048 { 00049 cv::Mat hsv; 00050 cv::cvtColor(src, hsv, CV_BGR2HSV); 00051 // Quantize the hue to 30 levels 00052 // and the saturation to 32 levels 00053 int hbins = 30, sbins = 32; 00054 int histSize[] = {hbins, sbins}; 00055 // hue varies from 0 to 179, see cvtColor 00056 float hranges[] = { 0, 180 }; 00057 // saturation varies from 0 (black-gray-white) to 00058 // 255 (pure spectrum color) 00059 float sranges[] = { 0, 256 }; 00060 const float* ranges[] = { hranges, sranges }; 00061 00062 // we compute the histogram from the 0-th and 1-st channels 00063 int channels[] = {0, 1}; 00064 00065 cv::calcHist( &hsv, 1, channels, cv::Mat(), // do not use mask 00066 histogram, 2, histSize, ranges, 00067 true, // the histogram is uniform 00068 false ); 00069 } 00070 00071 void calcHSVHist(const std::vector<cv::Mat>& src, std::vector<cv::MatND>& histograms) 00072 { 00073 histograms.clear(); 00074 00075 BOOST_FOREACH(const cv::Mat& mat, src) 00076 { 00077 cv::MatND hist; 00078 calcHSVHist(mat, hist); 00079 histograms.push_back(hist.clone()); 00080 } 00081 } 00082 00083 void calcHSVHist(const std::vector<cv::Mat>& src, cv::MatND& histogram) 00084 { 00085 std::vector<cv::Mat> hsv; 00086 BOOST_FOREACH(const cv::Mat& mat, src) 00087 { 00088 cv::Mat hsvMat; 00089 cv::cvtColor(mat, hsvMat, CV_BGR2HSV); 00090 hsv.push_back(hsvMat); 00091 } 00092 // Quantize the hue to 30 levels 00093 // and the saturation to 32 levels 00094 int hbins = 30, sbins = 32; 00095 int histSize[] = {hbins, sbins}; 00096 // hue varies from 0 to 179, see cvtColor 00097 float hranges[] = { 0, 180 }; 00098 // saturation varies from 0 (black-gray-white) to 00099 // 255 (pure spectrum color) 00100 float sranges[] = { 0, 256 }; 00101 const float* ranges[] = { hranges, sranges }; 00102 00103 // we compute the histogram from the 0-th and 1-st channels 00104 int channels[] = {0, 1}; 00105 cv::calcHist( hsv.data(), hsv.size(), channels, cv::Mat(), // do not use mask 00106 histogram, 2, histSize, ranges, 00107 true, // the histogram is uniform 00108 false ); 00109 } 00110 00111 00112 cv::Mat histogramImage(const cv::MatND& hist) 00113 { 00114 // Quantize the hue to 30 levels 00115 // and the saturation to 32 levels 00116 int hbins = 30, sbins = 32; 00117 double maxVal; 00118 cv::minMaxLoc(hist, 0, &maxVal, 0, 0); 00119 00120 int scale = 10; 00121 cv::Mat histImg = cv::Mat::zeros(sbins*scale, hbins*10, CV_8UC3); 00122 00123 for( int h = 0; h < hbins; h++ ) 00124 for( int s = 0; s < sbins; s++ ) 00125 { 00126 float binVal = hist.at<float>(h, s); 00127 int intensity = cvRound(binVal*255/maxVal); 00128 cv::rectangle( histImg, cv::Point(h*scale, s*scale), 00129 cv::Point( (h+1)*scale - 1, (s+1)*scale - 1), 00130 cv::Scalar::all(intensity), 00131 CV_FILLED ); 00132 } 00133 return histImg; 00134 } 00135 00136 void showHist(const cv::MatND& hist) 00137 { 00138 cv::Mat histImg = histogramImage(hist); 00139 00140 cv::namedWindow( "H-S Histogram", 1 ); 00141 cv::imshow( "H-S Histogram", histImg ); 00142 cv::waitKey(); 00143 } 00144 00145 }