for (auto it : m_pts) { /*int rect_x = it.x() - RECT_SIZE/4 ; int rect_y = it.y() - RECT_SIZE /4; cv::Rect rect(rect_x, rect_y, RECT_SIZE, RECT_SIZE); rect = rect & cv::Rect(0, 0, ImgBin.cols, ImgBin.rows); for (int nRow = rect_y - rect.height; nRow < rect_y + rect.height; nRow++) { for (int nCol = rect_x - rect.width; nCol < rect_x + rect.width; nCol++) {*/ ImgBin.row(it.y()).data[it.x()] = 255; } /* }在画出图片边缘1到时候,会进入断点
时间: 2024-03-29 18:35:01 浏览: 111
这段代码的作用是将m_pts中的点在ImgBin图像上标记为白色(像素值为255),从而画出图片的边缘。代码中使用了一个矩形rect来确定需要标记的区域,如果该区域超出了ImgBin图像的边界,则会进行裁剪。注意到在代码中有一段注释掉的双重循环,可能是之前的实现方式,但现在不再使用。至于为什么会进入断点,需要更多的上下文信息才能判断原因。
相关问题
在qgraphicsview中用此函数遍历多个点集for (auto it : m_pts) { /*int rect_x = it.x() - RECT_SIZE/4 ; int rect_y = it.y() - RECT_SIZE /4; cv::Rect rect(rect_x, rect_y, RECT_SIZE, RECT_SIZE); rect = rect & cv::Rect(0, 0, ImgBin.cols, ImgBin.rows); for (int nRow = rect_y - rect.height; nRow < rect_y + rect.height; nRow++) { for (int nCol = rect_x - rect.width; nCol < rect_x + rect.width; nCol++) {*/ ImgBin.row(it.y()).data[it.x()] = 255; }在超出图片的地方会进入断点
根据您提供的代码,可能是由于循环中的 rect 超出了图像的范围,导致在超出图片的地方进入了断点。
您可以在循环前加入判断,确保 rect 不会超出图像的范围。例如:
```cpp
for (auto it : m_pts) {
int rect_x = it.x() - RECT_SIZE / 4;
int rect_y = it.y() - RECT_SIZE / 4;
cv::Rect rect(rect_x, rect_y, RECT_SIZE, RECT_SIZE);
rect = rect & cv::Rect(0, 0, ImgBin.cols, ImgBin.rows);
for (int nRow = rect.y; nRow < rect.y + rect.height; nRow++) {
for (int nCol = rect.x; nCol < rect.x + rect.width; nCol++) {
ImgBin.row(nRow).data[nCol] = 255;
}
}
}
```
这里,使用 `cv::Rect` 的 `&` 运算符来确保 `rect` 不会超出图像的范围。同时,在循环中使用 `rect.x` 和 `rect.y` 作为起始值,确保不会越界。
mid = RectWidth[sum / 2 - 1]; float meanangle = 0.0; for (int i = 0; i< sum; i++) { RotatedRect rect = rt[i]; /*Point2f pts[4]; rect.points(pts);*/ //过滤矩形宽度超过平均宽度20%的矩形 if (rect.size.width >(mid + 10) || rect.size.width < (mid - 10)) { continue; } RectCenterlist[index].x = rect.center.x; RectCenterlist[index].y = rect.center.y; RectWidth[index] = rect.size.width; RectHeight[index] = rect.size.height; s1 += rect.angle; index++; }此段代码有什么用
这段代码的作用是从一个包含多个旋转矩形的数组中筛选出宽度接近平均宽度的矩形,并将这些矩形的中心坐标、宽度、高度和角度进行存储。
具体步骤如下:
1. 定义变量 mid,表示数组中所有矩形宽度的中位数。
2. 初始化平均角度 meanangle 为0。
3. 遍历旋转矩形数组,使用变量 i 作为索引。
4. 对于每个旋转矩形 rect:
- 检查矩形宽度是否超过平均宽度的上下限(mid + 10 和 mid - 10),如果超过则跳过继续下一个矩形。
- 将满足条件的矩形的中心坐标存储到 RectCenterlist 数组中的对应索引位置。
- 将满足条件的矩形的宽度存储到 RectWidth 数组中的对应索引位置。
- 将满足条件的矩形的高度存储到 RectHeight 数组中的对应索引位置。
- 将满足条件的矩形的角度累加到变量 s1 中。
- 将索引值 index 自增,用于下一个满足条件的矩形。
最终,这段代码将满足宽度接近平均宽度的旋转矩形的相关信息存储到对应的数组中,并计算了这些矩形的角度总和。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)