segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
histogram.cpp
1 
4 
5 /*
6  * Copyright (C) 2015 Department of iCub Facility - Istituto Italiano di Tecnologia
7  * Author: Vadim Tikhanoff
8  * email: vadim.tikhanoff@iit.it
9  * Permission is granted to copy, distribute, and/or modify this program
10  * under the terms of the GNU General Public License, version 2 or any
11  * later version published by the Free Software Foundation.
12  *
13  * A copy of the license can be found at
14  * http://www.robotcub.org/icub/license/gpl.txt
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19  * Public License for more details
20  */
21 
22 #include <opencv2/core/core_c.h>
23 #include "histogram.h"
24 #include <vector>
25 
26 /************************************************************************/
27 template <typename _Tp>
28 void lbp::histogram_(const cv::Mat& src, cv::Mat& hist, int numPatterns) {
29  hist = cv::Mat::zeros(1, numPatterns, CV_32SC1);
30  for(int i = 0; i < src.rows; i++) {
31  for(int j = 0; j < src.cols; j++) {
32  int bin = src.at<_Tp>(i,j);
33  hist.at<int>(0,bin) += 1;
34  }
35  }
36 }
37 
38 /************************************************************************/
39 template <typename _Tp>
40 double lbp::chi_square_(const cv::Mat& histogram0, const cv::Mat& histogram1) {
41  if(histogram0.type() != histogram1.type())
42  CV_Error(CV_StsBadArg, "Histograms must be of equal type.");
43  if(histogram0.rows != 1 || histogram0.rows != histogram1.rows || histogram0.cols != histogram1.cols)
44  CV_Error(CV_StsBadArg, "Histograms must be of equal dimension.");
45  double result = 0.0;
46  for(int i=0; i < histogram0.cols; i++) {
47  double a = histogram0.at<_Tp>(0,i) - histogram1.at<_Tp>(0,i);
48  double b = histogram0.at<_Tp>(0,i) + histogram1.at<_Tp>(0,i);
49  if(std::abs(b) > std::numeric_limits<double>::epsilon()) {
50  result+=(a*a)/b;
51  }
52  }
53  return result;
54 }
55 
56 /************************************************************************/
57 void lbp::spatial_histogram(const cv::Mat& src, cv::Mat& hist, int numPatterns, const cv::Size& window, int overlap) {
58  int width = src.cols;
59  int height = src.rows;
60  std::vector<cv::Mat> histograms;
61  for(int x=0; x < width - window.width; x+=(window.width-overlap)) {
62  for(int y=0; y < height-window.height; y+=(window.height-overlap)) {
63  cv::Mat cell = cv::Mat(src, cv::Rect(x,y,window.width, window.height));
64  histograms.push_back(histogram(cell, numPatterns));
65  }
66  }
67  hist.create(1, histograms.size()*numPatterns, CV_32SC1);
68  // i know this is a bit lame now... feel free to make this a bit more efficient...
69  for(size_t histIdx=0; histIdx < histograms.size(); histIdx++) {
70  for(int valIdx = 0; valIdx < numPatterns; valIdx++) {
71  int y = histIdx*numPatterns+valIdx;
72  hist.at<int>(0,y) = histograms[histIdx].at<int>(valIdx);
73  }
74  }
75 }
76 
77 /************************************************************************/
78 void lbp::histogram(const cv::Mat& src, cv::Mat& hist, int numPatterns) {
79  switch(src.type()) {
80  case CV_8SC1: histogram_<char>(src, hist, numPatterns); break;
81  case CV_8UC1: histogram_<unsigned char>(src, hist, numPatterns); break;
82  case CV_16SC1: histogram_<short>(src, hist, numPatterns); break;
83  case CV_16UC1: histogram_<unsigned short>(src, hist, numPatterns); break;
84  case CV_32SC1: histogram_<int>(src, hist, numPatterns); break;
85  }
86 }
87 
88 /************************************************************************/
89 double lbp::chi_square(const cv::Mat& histogram0, const cv::Mat& histogram1) {
90  switch(histogram0.type()) {
91  case CV_8SC1: return chi_square_<char>(histogram0,histogram1); break;
92  case CV_8UC1: return chi_square_<unsigned char>(histogram0,histogram1); break;
93  case CV_16SC1: return chi_square_<short>(histogram0, histogram1); break;
94  case CV_16UC1: return chi_square_<unsigned short>(histogram0,histogram1); break;
95  case CV_32SC1: return chi_square_<int>(histogram0,histogram1); break;
96  }
97 }
98 
99 /************************************************************************/
100 void lbp::spatial_histogram(const cv::Mat& src, cv::Mat& dst, int numPatterns, int gridx, int gridy, int overlap) {
101  int width = static_cast<int>(floor(src.cols/gridx));
102  int height = static_cast<int>(floor(src.rows / gridy));
103  spatial_histogram(src, dst, numPatterns, cv::Size_<int>(width, height), overlap);
104 }
105 
106 /************************************************************************/
107 cv::Mat lbp::histogram(const cv::Mat& src, int numPatterns) {
108  cv::Mat hist;
109  histogram(src, hist, numPatterns);
110  return hist;
111 }
112 
113 /************************************************************************/
114 cv::Mat lbp::spatial_histogram(const cv::Mat& src, int numPatterns, const cv::Size& window, int overlap) {
115  cv::Mat hist;
116  spatial_histogram(src, hist, numPatterns, window, overlap);
117  return hist;
118 }
119 
120 /************************************************************************/
121 cv::Mat lbp::spatial_histogram(const cv::Mat& src, int numPatterns, int gridx, int gridy, int overlap) {
122  cv::Mat hist;
123  spatial_histogram(src, hist, numPatterns, gridx, gridy);
124  return hist;
125 }
void histogram_(const cv::Mat &src, cv::Mat &hist, int numPatterns)
Original code by philipp <bytefish[at]gmx[dot]de>
Definition: histogram.cpp:28