opencv判断图像轮廓是否闭合
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
#include <iostream>
#include <vector>
#include "include/opencv2/opencv.hpp"
#include "include/opencv2/core.hpp"
using namespace std;
bool dContoursIsClose(cv::Mat &image)//输入二值化或灰度图像
{
vector<vector<cv::Point>> vContours;
vector<cv::Vec4i> hierarchy;
cv::findContours(image, vContours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE);
int fatherContoursNum = 0;
for (size_t i = 0; i < hierarchy.size(); i++) {
if (hierarchy[i][3] == -1) {//父轮廓
++fatherContoursNum;
if (hierarchy[i][2] == -1)//若有内嵌轮廓,此轮廓为闭合轮廓,若没有则不是闭合轮廓
{
return false;
}
}
else {//当前对象为子轮廓或内嵌轮廓
if (hierarchy[i][3] > hierarchy.size()) {//若索引越界
return false;
}
if (i == hierarchy[hierarchy[i][3]][2]) {}//判断内嵌轮廓的父轮廓是否存在
else {
return false;
}
}
}
if (fatherContoursNum != (hierarchy.size() / 2)) {//两层结构的轮廓均有父轮廓与嵌套轮廓
return false;
}
return true;
}




OpenCV: 开源计算机视觉库
最近提交(Master分支:28 天前 )
19c41c29
Improved blur #27822
* Perform row and column filter operations in a single pass.
* Temporary storage of intermediate results are avoided.
* Impacts 32F and 64F inputs for ksize <=5.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2 小时前
aeea7072
Rename fields in camera calibration results 3 小时前
更多推荐
所有评论(0)