45 float min_value,
float max_value,
46 const cv::Mat& mask_image)
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,
59 std::vector<jsk_recognition_msgs::HistogramWithRangeBin>
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;
69 bin.max_value = right;
70 bin.count = cv_hist.at<
float>(0, i);
78 const std::vector<jsk_recognition_msgs::HistogramWithRangeBin>& histogram)
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;
89 const jsk_recognition_msgs::HistogramWithRangeBin& right)
91 return left.count > right.count;
99 std::vector<jsk_recognition_msgs::HistogramWithRangeBin>
104 for (
size_t i = 0; i < bins.size(); i++) {
105 sum += bins[i].count;
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());
112 for (
size_t i = 0; i < bins.size(); i++) {
113 jsk_recognition_msgs::HistogramWithRangeBin bin = bins[i];
114 if (current_sum >= target_sum) {
117 top_n_bins.push_back(bin);
118 current_sum += bins[i].count;
125 const jsk_recognition_msgs::HistogramWithRangeBin& bin,
126 float min_width_value,
127 float max_width_value,
128 float max_height_value,
131 if (max_height_value == 0.0) {
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) {
143 cv::rectangle(image, cv::Point(left, height), cv::Point(right, height - top),
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);
154 dst.at<cv::Vec3b>(j, i) = cv::Vec3b(0, 0, 0);
158 dst.at<cv::Vec3b>(j, i) = cv::Vec3b(
int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255));
166 int min_x = image.cols;
167 int min_y = image.rows;
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);
180 return cv::Rect(min_x, min_y, std::max(max_x - min_x, 0), std::max(max_y - min_y, 0));
184 bool isBGR(
const std::string& encoding)
186 return encoding == enc::BGR8 || encoding == enc::BGR16;
189 bool isRGB(
const std::string& encoding)
191 return encoding == enc::RGB8 || encoding == enc::RGB16;
196 return encoding == enc::BGRA8 || encoding == enc::BGRA16;
201 return encoding == enc::RGBA8 || encoding == enc::RGBA16;
bool isRGBA(const std::string &encoding)
cv::Vec3d getRGBColor(const int color)
get rgb color with enum.
bool isBGRA(const std::string &encoding)
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.
cv::MatND HistogramWithRangeBinArrayTocvMatND(const std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &histogram)
convert jsk_recognition_msgs::HistogramimageWithRangeBin array to cv::MatND
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
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.
void labelToRGB(const cv::Mat src, cv::Mat &dst)
convert label image to rgb one.
void sortHistogramWithRangeBinArray(std::vector< jsk_recognition_msgs::HistogramWithRangeBin > &bins)
sort std::vector<jsk_recognition_msgs::HistogramWithRangeBin>. largest value will be at the first ele...
bool isRGB(const std::string &encoding)
cv::Rect boundingRectOfMaskImage(const cv::Mat &image)
compute bounding rectangle of mask image.
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
bool isBGR(const std::string &encoding)
Check encodings.
bool compareHistogramWithRangeBin(const jsk_recognition_msgs::HistogramWithRangeBin &left, const jsk_recognition_msgs::HistogramWithRangeBin &right)
return true if left.count is larger than right.count.