我有这个源图像
我已应用二进制阈值来获取此
我使用轮廓来区分具有子轮廓的轮廓和不具有轮廓的轮廓。所得到的图片是
但是,我如何计算每个绿色轮廓包含的儿童轮廓的数量?这是我用过的代码: -
Mat binMask = lung;// the thresholded image
Mat lung_src = imread("source.tiff");// the source image
//imshow("bin mask", binMask);
vector<std::vector<cv::Point>> contours;
vector<cv::Vec4i> hierarchy;
int count = 0, j;
double largest_area = 0;
int largest_contour_index = 0;
findContours(binMask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a>largest_area)
{
largest_area = a;
largest_contour_index = i;
}
for (j = 0; j <= i; j++)
{
if (hierarchy[j][2] != -1) // means it has child contour
{
drawContours(lung_src, contours, j, Scalar(0, 255, 0), 1, 8, hierarchy, 0, Point());
}
else // means it doesn't have any child contour
{
drawContours(lung_src, contours, j, Scalar(0, 0, 255), 1, 8, hierarchy, 0, Point());
}
}
}
drawContours(lung_src, contours, largest_contour_index, Scalar(255, 0, 0), 1, 8, hierarchy, 0, Point());
imshow("lung-mapped", lung_src);
EDIT-1-我在最后添加了Humam的代码来检查它:
std::vector<int> number_of_inner_contours(contours.size(), -1);
int number_of_childs = 0;
for (size_t i = 0; i < contours.size(); i++)
{
int first_child_index = hierarchy[i][2];
if (first_child_index >= 0)
{
int next_child_index = hierarchy[first_child_index][0];
if (number_of_inner_contours[next_child_index]<0)
{
number_of_childs = number_of_inner_contours[next_child_index];
}
else
{
while (next_child_index >= 0)
{
next_child_index = hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours[i] = number_of_childs;
}
}
else
{
number_of_inner_contours[i] = 0;
}
cout << "\nThe contour[" << i << "] has " << number_of_inner_contours[i] << "child contours";
}
但我得到的输出是:
The contour[456 ] has 0 child contours
The contour[457 ] has 0 child contours
The contour[458 ] has 0 child contours
The contour[459 ] has -1 child contours
答案 0 :(得分:1)
层次结构 - 可选输出向量,包含有关的信息 图像拓扑。它具有与轮廓数量一样多的元素。对于 每个第i个轮廓轮廓[i],元素层次[i] [0], hiearchy i,hiearchy [i] [2]和hiearchy [i] [3]设置为 基于0的指数在下一轮和前一轮的轮廓中 相同的层次级别,第一个子轮廓和父级 轮廓,分别。如果轮廓i没有下一个, previous,parent或嵌套轮廓,相应的元素 等级[i]将是否定的。
这是用于完成工作的未经测试的代码:
std::vector<size_t> number_of_inner_contours;
number_of_inner_contours.reserve(contours.size());
for (size_t i = 0; i < contours.size(); i++){
size_t number_of_childs = 0;
auto first_child_index=hierarchy[i][2];
if(first_child_index>=0){
auto next_child_index=hierarchy[first_child_index][0];
while (next_child_index>=0){
next_child_index=hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours.emplace_back(number_of_childs);
}
else{
number_of_inner_contours.emplace_back(0);
}
}
通过使用动态编程的概念,可以更好地完成此代码。这是第一次尝试:
std::vector<int> number_of_inner_contours(contours.size(),-1);
for (size_t i = 0; i < contours.size(); i++){
auto number_of_childs = 0;
auto first_child_index=hierarchy[i][2];
if(first_child_index>=0){
auto next_child_index=hierarchy[first_child_index][0];
if(number_of_inner_contours[next_child_index]<0){
number_of_childs=number_of_inner_contours[next_child_index];
}
else{
while (next_child_index>=0){
next_child_index=hierarchy[next_child_index][0];
++number_of_childs;
}
number_of_inner_contours[i]=number_of_childs;
}
}
else{
number_of_inner_contours[i]=0;
}
}