void GetAllTreesLine(cv::Mat Imgthin, std::vector< std::vector< cv::Point > > & Treelines) { // cv::Mat1b retImg = cv::Mat1b::zeros(cv::Size(1920, 1200)); // cv::Mat1b retImg(cv::Size(IMG_WIDTH, IMG_HEIGH), CV_8UC1); unsigned int fromx = 3, fromy = 3; unsigned int maxcol = 0; //int maxloopnum =10 ; //int loopnum =0 ; std::vector< StreeHeadTail > allTreesList; while (1) { StreeHeadTail roottailRet; int ret = createTreeformMap(Imgthin, fromy, fromx, maxcol, roottailRet); if (ret != 1) { break; } else { if ((maxcol >= IMG_WIDTH) || (roottailRet.TreeRoot == NULL) || (roottailRet.TreeTail == NULL)) { break; } allTreesList.push_back(roottailRet); if (allTreesList.size() > MAX_FRAGMENT) break; fromy = maxcol; } } if (allTreesList.size() <= 0) return; int index = 0; std::vector< cv::Point > points; //allTreeline.clear(); for (int ii = allTreesList.size() - 1; ii >= 0; ii--) { Treelines.push_back(points); getBranchLine(allTreesList[ii].TreeTail, Treelines[index ]); index++; } // for (unsigned int ii = 0; ii < allTreesList.size(); ii++) // { //lineNode * proot = allTreesList[ii].TreeRoot; //ClearTreeline(pexpendLeftRightroot); // ClearTreenodes(proot); //} deleteAllpointers(totalPointers); totalPointers.clear(); allTreesList.clear(); return; }
时间: 2023-02-18 07:49:55 浏览: 63
&trees)
以下是获取所有树线的函数:
void GetAllTreesLine(cv::Mat Imgthin, std::vector< std::vector< cv::Point > > &trees)
{
//查找所有轮廓
std::vector<std::vector<cv::Point>> contours;
cv::findContours(Imgthin, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
//遍历每一个轮廓
for (int i = 0; i < contours.size(); i++)
{
//拟合曲线
std::vector<cv::Point> approx;
cv::approxPolyDP(contours[i], approx, 0.01 * cv::arcLength(contours[i], true), true);
//检测是否为树线
if (approx.size() == 2)
{
trees.push_back(approx);
}
}
}
相关问题
opencv 中的fitLine(std::vector<cv::Point2f>{P1, P2}, L1, cv::DIST_L2, 0, 0.01, 0.01);
`cv::fitLine` 是 OpenCV 中用于拟合一组点集合成一条直线的函数。它的定义如下:
```c++
void cv::fitLine(
InputArray points, // 输入的点集,可以是 `Mat`、`std::vector<Point>` 或 `std::vector<Vec2f>`
OutputArray line, // 输出的直线参数,是一个 4 维向量 `Vec4f`,表示直线的一般式参数 (a, b, c, d)
int distType, // 距离类型,可以是 `cv::DIST_L2`、`cv::DIST_L1`、`cv::DIST_L12` 或 `cv::DIST_FAIR`
double param, // 距离类型的参数,当 `distType` 为 `cv::DIST_L12` 时表示 $\alpha$,当 `distType` 为 `cv::DIST_FAIR` 时表示 $\rho$
double reps, // 估计直线的精度,当估计出的直线与真实直线的距离小于 `reps` 时认为是正确的直线
double aeps // 估计直线的精度,当估计出的直线的方向余弦与真实直线的方向余弦夹角小于 `aeps` 时认为是正确的直线
);
```
其中,`points` 表示输入的点集,可以是 `Mat`、`std::vector<Point>` 或 `std::vector<Vec2f>`,`line` 表示输出的直线参数,是一个 4 维向量 `Vec4f`,表示直线的一般式参数 `(a, b, c, d)`。`distType` 表示距离类型,可以是 `cv::DIST_L2`、`cv::DIST_L1`、`cv::DIST_L12` 或 `cv::DIST_FAIR`。`param` 表示距离类型的参数,当 `distType` 为 `cv::DIST_L12` 时表示 $\alpha$,当 `distType` 为 `cv::DIST_FAIR` 时表示 $\rho$。`reps` 和 `aeps` 分别表示估计直线的精度,当估计出的直线与真实直线的距离小于 `reps` 时认为是正确的直线,当估计出的直线的方向余弦与真实直线的方向余弦夹角小于 `aeps` 时认为是正确的直线。
在你的问题中,`fitLine` 的调用代码如下:
```c++
cv::fitLine(std::vector<cv::Point2f>{P1, P2}, L1, cv::DIST_L2, 0, 0.01, 0.01);
```
其中,`std::vector<cv::Point2f>{P1, P2}` 表示一个包含两个点 `P1` 和 `P2` 的 `std::vector<cv::Point2f>` 类型的变量。`L1` 是一个 `cv::Vec4f` 类型的变量,用于存储拟合出的直线参数。`cv::DIST_L2` 表示距离类型为欧式距离,`0` 表示距离类型参数为默认值,`0.01` 和 `0.01` 分别表示估计直线的精度参数 `reps` 和 `aeps`。
CornerDetector(int n_rows = 8, int n_cols = 10, double detection_threshold = 40.0); ~CornerDetector() = default; void detect_features(const cv::Mat &image, std::vector<cv::Point2f> &features); void set_grid_position(const cv::Point2f &pos); void set_grid_size(int n_rows, int n_cols); int get_n_rows() const { return grid_n_rows_; } int get_n_cols() const { return grid_n_cols_; } float shiTomasiScore(const cv::Mat &img, int u, int v); int sub2ind(const cv::Point2f &sub);
这段代码定义了一个名为 `CornerDetector` 的类,该类具有以下成员函数:
- `CornerDetector(int n_rows = 8, int n_cols = 10, double detection_threshold = 40.0)`:类的构造函数,可以用来初始化占据网格的行数、列数和检测阈值。
- `~CornerDetector() = default`:类的析构函数。
- `void detect_features(const cv::Mat &image, std::vector<cv::Point2f> &features)`:检测输入图像中的角点,并将角点的坐标保存在 `features` 向量中。
- `void set_grid_position(const cv::Point2f &pos)`:设置占据网格的左上角位置。
- `void set_grid_size(int n_rows, int n_cols)`:设置占据网格的行数和列数。
- `int get_n_rows() const { return grid_n_rows_; }`:获取占据网格的行数。
- `int get_n_cols() const { return grid_n_cols_; }`:获取占据网格的列数。
- `float shiTomasiScore(const cv::Mat &img, int u, int v)`:计算图像中指定位置的 Shi-Tomasi 角点得分。
- `int sub2ind(const cv::Point2f &sub)`:将占据网格的行列坐标转换为索引。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)