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/or 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
124  drawHistogramWithRangeBin(cv::Mat& image,
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 boundingRectFromContours(const std::vector<cv::Point>& contours) {
165  double minx = contours.at(0).x;
166  double miny = contours.at(0).y;
167  double maxx = contours.at(0).x;
168  double maxy = contours.at(0).y;
169  for (int i = 1; i < contours.size(); ++i) {
170  if (maxx < contours.at(i).x) {
171  maxx = contours.at(i).x;
172  }
173  if (maxy < contours.at(i).y) {
174  maxy = contours.at(i).y;
175  }
176  if (minx > contours.at(i).x) {
177  minx = contours.at(i).x;
178  }
179  if (miny > contours.at(i).y) {
180  miny = contours.at(i).y;
181  }
182  }
183  return cv::Rect(minx, miny, maxx - minx, maxy - miny);
184  }
185 
186  cv::Rect boundingRectOfMaskImage(const cv::Mat& image)
187  {
188  int min_x = image.cols;
189  int min_y = image.rows;
190  int max_x = 0;
191  int max_y = 0;
192  for (int j = 0; j < image.rows; j++) {
193  for (int i = 0; i < image.cols; i++) {
194  if (image.at<uchar>(j, i) != 0) {
195  min_x = std::min(min_x, i);
196  min_y = std::min(min_y, j);
197  max_x = std::max(max_x, i);
198  max_y = std::max(max_y, j);
199  }
200  }
201  }
202  return cv::Rect(min_x, min_y, std::max(max_x - min_x, 0), std::max(max_y - min_y, 0));
203  }
204 
205  // Utility functions for inspecting an encoding string
206  bool isBGR(const std::string& encoding)
207  {
208  return encoding == enc::BGR8 || encoding == enc::BGR16;
209  }
210 
211  bool isRGB(const std::string& encoding)
212  {
213  return encoding == enc::RGB8 || encoding == enc::RGB16;
214  }
215 
216  bool isBGRA(const std::string& encoding)
217  {
218  return encoding == enc::BGRA8 || encoding == enc::BGRA16;
219  }
220 
221  bool isRGBA(const std::string& encoding)
222  {
223  return encoding == enc::RGBA8 || encoding == enc::RGBA16;
224  }
225 
226 }
sensor_msgs::image_encodings
jsk_recognition_utils::computeHistogram
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:76
jsk_recognition_utils::drawHistogramWithRangeBin
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:156
jsk_recognition_utils::boundingRectOfMaskImage
cv::Rect boundingRectOfMaskImage(const cv::Mat &image)
compute bounding rectangle of mask image.
Definition: cv_utils.cpp:218
image_encodings.h
i
int i
jsk_recognition_utils::cvMatNDToHistogramWithRangeBinArray
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:92
jsk_recognition_utils::topNHistogramWithRangeBins
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:132
jsk_recognition_utils
Definition: color_utils.h:41
sensor_msgs::image_encodings::RGB8
const std::string RGB8
jsk_recognition_utils::isBGRA
bool isBGRA(const std::string &encoding)
Definition: cv_utils.cpp:248
jsk_recognition_utils::compareHistogramWithRangeBin
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:120
sensor_msgs::image_encodings::BGR16
const std::string BGR16
jsk_recognition_utils::isRGB
bool isRGB(const std::string &encoding)
Definition: cv_utils.cpp:243
static_virtual_camera.width
width
Definition: static_virtual_camera.py:49
jsk_recognition_utils::sortHistogramWithRangeBinArray
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:126
jsk_recognition_utils::labelToRGB
void labelToRGB(const cv::Mat src, cv::Mat &dst)
convert label image to rgb one.
Definition: cv_utils.cpp:179
cv_utils.h
jsk_recognition_utils::isRGBA
bool isRGBA(const std::string &encoding)
Definition: cv_utils.cpp:253
jsk_recognition_utils::getRGBColor
cv::Vec3d getRGBColor(const int color)
get rgb color with enum.
Definition: rgb_colors.cpp:75
sensor_msgs::image_encodings::BGR8
const std::string BGR8
jsk_recognition_utils::isBGR
bool isBGR(const std::string &encoding)
Check encodings.
Definition: cv_utils.cpp:238
encoding
std::string encoding
static_virtual_camera.height
height
Definition: static_virtual_camera.py:49
jsk_recognition_utils::boundingRectFromContours
cv::Rect boundingRectFromContours(const std::vector< cv::Point > &contours)
Return Bounding Box from contours.
Definition: cv_utils.cpp:196
rgb_colors.h
jsk_recognition_utils::HistogramWithRangeBinArrayTocvMatND
cv::MatND HistogramWithRangeBinArrayTocvMatND(const std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &histogram)
convert jsk_recognition_msgs::HistogramimageWithRangeBin array to cv::MatND
Definition: cv_utils.cpp:109
sensor_msgs::image_encodings::RGB16
const std::string RGB16


jsk_recognition_utils
Author(s):
autogenerated on Tue Jan 7 2025 04:04:52