10#ifndef COMMON_PROJECTION_HPP
11#define COMMON_PROJECTION_HPP
14#include <opencv2/core.hpp>
71 size_t** hdir_prefix_sum;
72 size_t** vdir_prefix_sum;
74 void Reset(
size_t height,
size_t width,
bool is_copy_data);
103 size_t Width()
const;
112 template <
typename INDEXED>
114 template <
typename INDEXED>
140 hdir_prefix_sum = vdir_prefix_sum =
nullptr;
183 hdir_prefix_sum = vdir_prefix_sum =
nullptr;
186 this->
Assign(binary_image);
257inline void Projection::Free() {
259 if (rows && hdir_prefix_sum) {
260 for (
int i = 0;
i < rows; ++
i)
261 delete[] hdir_prefix_sum[
i];
264 delete[] hdir_prefix_sum;
265 hdir_prefix_sum =
nullptr;
269 if (cols && vdir_prefix_sum) {
270 for (
int i = 0; i < cols; ++i)
271 delete[] vdir_prefix_sum[i];
273 delete[] vdir_prefix_sum;
274 vdir_prefix_sum =
nullptr;
292inline void Projection::Reset(
size_t rows,
size_t cols,
bool is_copy_data) {
294 if (rows <= this->rows && cols <= this->cols)
298 size_t** new_hdir_prefix_and =
new size_t*[rows]();
299 for (
int i = 0; i < rows; ++i)
300 new_hdir_prefix_and[i] =
new size_t[cols + 1]();
303 size_t** new_vdir_prefix_and =
new size_t*[cols]();
304 for (
int i = 0; i < cols; ++i)
305 new_vdir_prefix_and[i] =
new size_t[rows + 1]();
310 for (
int i = 0; i < this->rows; ++i)
311 memcpy(new_hdir_prefix_and[i], hdir_prefix_sum[i], (this->cols + 1) *
sizeof(
size_t));
314 for (
int i = 0; i < this->cols; ++i)
315 memcpy(new_vdir_prefix_and[i], vdir_prefix_sum[i], (this->rows + 1) *
sizeof(
size_t));
320 hdir_prefix_sum = new_hdir_prefix_and;
321 vdir_prefix_sum = new_vdir_prefix_and;
343 this->Reset(height, width,
true);
366 this->Reset(height, width,
true);
368 this->height = height;
399 for (
int i = 0;
i < rows; ++
i)
400 memcpy(hdir_prefix_sum[
i],
projection.hdir_prefix_sum[
i], (cols + 1) *
sizeof(
size_t));
403 for (
int i = 0;
i < cols; ++
i)
404 memcpy(vdir_prefix_sum[
i],
projection.vdir_prefix_sum[
i], (rows + 1) *
sizeof(
size_t));
426 Cm_Assert(
binary_image.channels() == 1,
"only single-channel image (grayscale image) can be projected!");
436 this->Reset(height, width,
false);
439 for (
int i = 0;
i < height; ++
i) {
443 for (
int j = 0;
j < width; ++
j)
444 hdir_prefix_sum[
i][
j + 1] = hdir_prefix_sum[
i][
j] +
row_ptr[
j];
449 for (
int i = 0;
i < width; ++
i)
450 for (
int j = 0;
j < height; ++
j)
451 vdir_prefix_sum[
i][
j + 1] = vdir_prefix_sum[
i][
j] +
image_ptr[
i +
j * width];
473 std::swap(hdir_prefix_sum,
projection.hdir_prefix_sum);
474 std::swap(vdir_prefix_sum,
projection.vdir_prefix_sum);
490 if (width && height) {
492 for (
int i = 0;
i < height; ++
i)
493 memset(hdir_prefix_sum[
i], 0, (width + 1) *
sizeof(
size_t));
495 for (
int i = 0;
i < width; ++
i)
496 memset(vdir_prefix_sum[
i], 0, (height + 1) *
sizeof(
size_t));
612 Cm_Assert(index < height,
"the index is out of bounds!");
617 return (hdir_prefix_sum[index][
col_range.end] - hdir_prefix_sum[index][
col_range.start]) / 255;
634 Cm_Assert(index >= 0 && index < width,
"the index is out of bounds!");
639 return (vdir_prefix_sum[index][
row_range.end] - vdir_prefix_sum[index][
row_range.start]) / 255;
688 for (
int i = 0;
i < width; ++
i)
700 for (
int i = 0;
i < height; ++
i)
853template <
typename INDEXED>
890template <
typename INDEXED>
static Interval All()
获取表示全范围的区间
Point< T > & Translate(T dx, T dy)
点的坐标平移操作
Projection & operator=(const Projection &projection)
投影类的拷贝赋值函数
Projection & Clear()
清空投影类所有数据
Intervals ExtractTrough(Direction direction, size_t max_proj_value, size_t min_trough_span, Interval row_range=Interval::All(), Interval col_range=Interval::All(), bool is_absolute_coord=true) const
根据指定的行区间和列区间投影抽取波谷
size_t Row(size_t index, Interval col_range=Interval::All()) const
获取横向投影中指定行的投影值
Projection & Assign(const Projection &projection)
重新赋值一份投影
size_t Height() const
获取投影图像区域高度
List< size_t > Value(Direction direction, Interval row_range=Interval::All(), Interval col_range=Interval::All()) const
获取指定行区间和列区间的投影值
Projection & Reserve(size_t height, size_t width)
重置投影图像所对应投影数据空间大小
Intervals ExtractPeak(Direction direction, size_t min_proj_value, size_t min_peak_span, Interval row_range=Interval::All(), Interval col_range=Interval::All(), bool is_absolute_coord=true) const
根据指定的行区间和列区间投影抽取波峰
size_t Col(size_t index, Interval row_range=Interval::All()) const
获取纵向投影中指定列的投影值
Projection & Resize(size_t height, size_t width)
重置投影图对象的大小
size_t Width() const
获取投影图像区域宽度
Rect Boundary(double hdir_threshold=0.005, double vdir_threshold=0.005, int margin=20) const
根据投影获取图像的边界
cv::Mat Image(Direction direction, Interval row_range=Interval::All(), Interval col_range=Interval::All()) const
获取指定行区间和列区间的投影图像图像
Projection & Swap(Projection &projection)
交换投影
#define Cm_Assert(expr, message)
断言宏
unsigned int size_t
表示内存中对象的大小,经常用于表示数组长度、内存分配等涉及到大小的地方。