cv_utils.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
39 
41 
42 namespace jsk_recognition_utils
43 {
44  cv::MatND computeHistogram(const cv::Mat& input_image, int bin_size,
45  float min_value, float max_value,
46  const cv::Mat& mask_image)
47  {
48  int channels[] = {0};
49  cv::MatND hist;
50  int hist_size[] = {bin_size};
51  float range[] = {min_value, max_value};
52  const float* ranges[] = {range};
53  cv::calcHist(&input_image, 1, channels, mask_image,
54  hist, 1, hist_size,
55  ranges, true, false);
56  return hist;
57  }
58 
59  std::vector<jsk_recognition_msgs::HistogramWithRangeBin>
60  cvMatNDToHistogramWithRangeBinArray(const cv::MatND& cv_hist, float min_value, float max_value)
61  {
62  std::vector<jsk_recognition_msgs::HistogramWithRangeBin> bins(cv_hist.total());
63  const float bin_width = (max_value - min_value) / cv_hist.total();
64  for (size_t i = 0; i < cv_hist.total(); i++) {
65  const float left = i * bin_width + min_value;
66  const float right = (i + 1) * bin_width + min_value;
67  jsk_recognition_msgs::HistogramWithRangeBin bin;
68  bin.min_value = left;
69  bin.max_value = right;
70  bin.count = cv_hist.at<float>(0, i);
71  bins[i] = bin;
72  }
73  return bins;
74  }
75 
76  cv::MatND
78  const std::vector<jsk_recognition_msgs::HistogramWithRangeBin>& histogram)
79  {
80  cv::MatND ret(1, histogram.size(), CV_32F);
81  for (size_t i = 0; i < histogram.size(); i++) {
82  jsk_recognition_msgs::HistogramWithRangeBin bin = histogram[i];
83  ret.at<float>(0, i) = bin.count;
84  }
85  return ret;
86  }
87 
88  bool compareHistogramWithRangeBin(const jsk_recognition_msgs::HistogramWithRangeBin& left,
89  const jsk_recognition_msgs::HistogramWithRangeBin& right)
90  {
91  return left.count > right.count;
92  }
93 
94  void sortHistogramWithRangeBinArray(std::vector<jsk_recognition_msgs::HistogramWithRangeBin>& bins)
95  {
96  std::sort(bins.begin(), bins.end(), compareHistogramWithRangeBin);
97  }
98 
99  std::vector<jsk_recognition_msgs::HistogramWithRangeBin>
100  topNHistogramWithRangeBins(const std::vector<jsk_recognition_msgs::HistogramWithRangeBin>& bins,
101  double top_n_rate)
102  {
103  int sum = 0;
104  for (size_t i = 0; i < bins.size(); i++) {
105  sum += bins[i].count;
106  }
107  const int target_sum = sum * top_n_rate;
108  std::vector<jsk_recognition_msgs::HistogramWithRangeBin> top_n_bins;
109  top_n_bins.reserve(bins.size());
110 
111  int current_sum = 0;
112  for (size_t i = 0; i < bins.size(); i++) {
113  jsk_recognition_msgs::HistogramWithRangeBin bin = bins[i];
114  if (current_sum >= target_sum) {
115  return top_n_bins;
116  }
117  top_n_bins.push_back(bin);
118  current_sum += bins[i].count;
119  }
120  return top_n_bins;
121  }
122 
123  void
125  const jsk_recognition_msgs::HistogramWithRangeBin& bin,
126  float min_width_value,
127  float max_width_value,
128  float max_height_value,
129  cv::Scalar color)
130  {
131  if (max_height_value == 0.0) {
132  return;
133  }
134  const int height = image.rows;
135  const int width = image.cols;
136  const int left = (bin.min_value - min_width_value) / (max_width_value - min_width_value) * width;
137  const int right = (bin.max_value - min_width_value) / (max_width_value - min_width_value) * width;
138  const int top = bin.count / max_height_value * height;
139  if (bin.count == 0 || top == 0 || left == right || left < 0 || right >= width || top > height) {
140  return;
141  }
142 
143  cv::rectangle(image, cv::Point(left, height), cv::Point(right, height - top),
144  color, CV_FILLED);
145  }
146 
147  void labelToRGB(const cv::Mat src, cv::Mat& dst)
148  {
149  dst = cv::Mat::zeros(src.rows, src.cols, CV_8UC3);
150  for (size_t j = 0; j < src.rows; ++j) {
151  for (size_t i = 0; i < src.cols; ++i) {
152  int label = src.at<int>(j, i);
153  if (label == 0) { // background label
154  dst.at<cv::Vec3b>(j, i) = cv::Vec3b(0, 0, 0);
155  }
156  else {
157  cv::Vec3d rgb = jsk_recognition_utils::getRGBColor(label);
158  dst.at<cv::Vec3b>(j, i) = cv::Vec3b(int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255));
159  }
160  }
161  }
162  }
163 
164  cv::Rect boundingRectOfMaskImage(const cv::Mat& image)
165  {
166  int min_x = image.cols;
167  int min_y = image.rows;
168  int max_x = 0;
169  int max_y = 0;
170  for (int j = 0; j < image.rows; j++) {
171  for (int i = 0; i < image.cols; i++) {
172  if (image.at<uchar>(j, i) != 0) {
173  min_x = std::min(min_x, i);
174  min_y = std::min(min_y, j);
175  max_x = std::max(max_x, i);
176  max_y = std::max(max_y, j);
177  }
178  }
179  }
180  return cv::Rect(min_x, min_y, std::max(max_x - min_x, 0), std::max(max_y - min_y, 0));
181  }
182 
183  // Utility functions for inspecting an encoding string
184  bool isBGR(const std::string& encoding)
185  {
186  return encoding == enc::BGR8 || encoding == enc::BGR16;
187  }
188 
189  bool isRGB(const std::string& encoding)
190  {
191  return encoding == enc::RGB8 || encoding == enc::RGB16;
192  }
193 
194  bool isBGRA(const std::string& encoding)
195  {
196  return encoding == enc::BGRA8 || encoding == enc::BGRA16;
197  }
198 
199  bool isRGBA(const std::string& encoding)
200  {
201  return encoding == enc::RGBA8 || encoding == enc::RGBA16;
202  }
203 
204 }
bool isRGBA(const std::string &encoding)
Definition: cv_utils.cpp:199
cv::Vec3d getRGBColor(const int color)
get rgb color with enum.
Definition: rgb_colors.cpp:43
bool isBGRA(const std::string &encoding)
Definition: cv_utils.cpp:194
cv::MatND computeHistogram(const cv::Mat &input_image, int bin_size, float min_value, float max_value, const cv::Mat &mask_image)
simple wrapper for cv::calcHist.
Definition: cv_utils.cpp:44
cv::MatND HistogramWithRangeBinArrayTocvMatND(const std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &histogram)
convert jsk_recognition_msgs::HistogramimageWithRangeBin array to cv::MatND
Definition: cv_utils.cpp:77
std::vector< jsk_recognition_msgs::HistogramWithRangeBin > cvMatNDToHistogramWithRangeBinArray(const cv::MatND &cv_hist, float min_value, float max_value)
convert cv::MatND to jsk_recognition_msgs::HistogramimageWithRangeBin array
Definition: cv_utils.cpp:60
std::vector< jsk_recognition_msgs::HistogramWithRangeBin > topNHistogramWithRangeBins(const std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &bins, double top_n_rate)
extract top-N histograms. bins should be sorted. top_n_rate should be 0-1.
Definition: cv_utils.cpp:100
void labelToRGB(const cv::Mat src, cv::Mat &dst)
convert label image to rgb one.
Definition: cv_utils.cpp:147
void sortHistogramWithRangeBinArray(std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &bins)
sort std::vector<jsk_recognition_msgs::HistogramWithRangeBin>. largest value will be at the first ele...
Definition: cv_utils.cpp:94
bool isRGB(const std::string &encoding)
Definition: cv_utils.cpp:189
cv::Rect boundingRectOfMaskImage(const cv::Mat &image)
compute bounding rectangle of mask image.
Definition: cv_utils.cpp:164
void drawHistogramWithRangeBin(cv::Mat &image, const jsk_recognition_msgs::HistogramWithRangeBin &bin, float min_width_value, float max_width_value, float max_height_value, cv::Scalar color)
draw bin to cv::Mat
Definition: cv_utils.cpp:124
bool isBGR(const std::string &encoding)
Check encodings.
Definition: cv_utils.cpp:184
bool compareHistogramWithRangeBin(const jsk_recognition_msgs::HistogramWithRangeBin &left, const jsk_recognition_msgs::HistogramWithRangeBin &right)
return true if left.count is larger than right.count.
Definition: cv_utils.cpp:88


jsk_recognition_utils
Author(s):
autogenerated on Mon May 3 2021 03:03:03