1

에 윤곽 안에 아이 윤곽의 수를 계산 나는이 소스 이미지OpenCV의

enter image description here

이 있고 난에 윤곽을 사용이

enter image description here

를 얻기 위해 바이너리 임계 값을 적용했습니다 어린이 윤곽을 가진 것과 그렇지 않은 것을 구별합니다. 결과 그림은

입니다. enter image description here

하지만 녹색 윤곽 각각에 포함 된 하위 윤곽 수는 어떻게 계산합니까? 이것은 내가 사용하고 코드입니다 : -

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 

답변

0

:

계층 - 이미지 토폴로지에 대한 정보가 포함 된 선택적 출력 벡터입니다. 윤곽선 수만큼 요소가 있습니다. 각각의 i 번째 등고선 등고선 [i]에 대해, 요소 계층 [i] [0], 하이라이어 i, hiearchy [i] [2] 및 hiearchy [i] [3]은 0 기반 인덱스 동일한 계층 수준, 첫 번째 자식 컨투어 및 부모 컨투어에서 각각 다음 및 이전 컨투어의 컨투어에 표시됩니다. 윤곽선 i에 다음 문자가 없으면 이전, 상위 또는 중첩 된 윤곽선은 hierarchy [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; 
    } 
} 
+0

죄송합니다. 그러나 원하는 결과를 얻지 못했습니다. –

+0

"원하는 결과를 얻지 못했습니다"에 대한 설명을 좀 해 주실 수 있습니까? 코드에있는 오류를 편집했습니다. 다시 시도해 볼 수 있습니다. –

+0

@ Humam-u 확인할 수 있습니까? –