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),
147 void labelToRGB(
const cv::Mat src, cv::Mat& dst)
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));
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;
173 if (maxy < contours.at(i).y) {
174 maxy = contours.at(i).y;
176 if (minx > contours.at(i).x) {
177 minx = contours.at(i).x;
179 if (miny > contours.at(i).y) {
180 miny = contours.at(
i).y;
183 return cv::Rect(minx, miny, maxx - minx, maxy - miny);
188 int min_x = image.cols;
189 int min_y = image.rows;
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);
202 return cv::Rect(min_x, min_y, std::max(max_x - min_x, 0), std::max(max_y - min_y, 0));
206 bool isBGR(
const std::string& encoding)
211 bool isRGB(
const std::string& encoding)
216 bool isBGRA(
const std::string& encoding)
221 bool isRGBA(
const std::string& encoding)