#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;
}
04-29
1546