에 윤곽 안에 아이 윤곽의 수를 계산 나는이 소스 이미지OpenCV의
이 있고 난에 윤곽을 사용이
를 얻기 위해 바이너리 임계 값을 적용했습니다 어린이 윤곽을 가진 것과 그렇지 않은 것을 구별합니다. 결과 그림은
하지만 녹색 윤곽 각각에 포함 된 하위 윤곽 수는 어떻게 계산합니까? 이것은 내가 사용하고 코드입니다 : -
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);
편집 -1- 내가 마지막에 후맘의 코드를 추가 그것을 체크 아웃 :
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";
}
하지만 내가 가진 출력은 같았다 :
OpenCV documentation에서 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
죄송합니다. 그러나 원하는 결과를 얻지 못했습니다. –
"원하는 결과를 얻지 못했습니다"에 대한 설명을 좀 해 주실 수 있습니까? 코드에있는 오류를 편집했습니다. 다시 시도해 볼 수 있습니다. –
@ Humam-u 확인할 수 있습니까? –