EmptyRegion에서의 CountObj 유의사항
할콘 HDevelop의 문서에 따르면, count_obj
는 매개변수 Objects
에 포함된 객체의 개수를 결정합니다. 이 문서만 보면 빈 아이코닉 변수의 경우, count_obj
가 0을 반환할 것이라고 예상할 수 있지만, 사실은 그렇지 않습니다.
Signature
1 | count_obj(Objects : : : Number) |
Description
The operator count_obj
determines for the object parameter Objects the number of objects it contains. In this connection it should be noted that object is not the same as connection component. For example, the number of objects of one region consisting of three parts that are not connected is 1.
For a short description of the iconic objects that are available in HALCON see the introduction of chapter Object.
CountObj 동작 비교
EmptyObject
, EmptyRegion
그리고 7 Connected Region
을 각각 count_obj
하여 반환되는 값을 비교해보도록 하겠습니다.
1 | read_image(Image, 'printer_chip/printer_chip_01') |
예상했던 것과 달리, EmptyRegion
의 count_obj
결과로 0이 아닌, 1을 반환 하였습니다. 이 결과를 이해하려면 gen_empty_obj
과 gen_empty_region
문서의 Description 부분을 참고하면 비로소 이해할 수 있게 됩니다.
Object | Result |
---|---|
Empty Object | 0 |
Empty Region | 1 |
7 Connected Region | 7 |
원인 파악 및 해설
아래의 설명을 요약하자면, gen_empty_obj
는 실제로 빈 튜플(the output parameter does not contain any objects)을 생성하기 떄문에 count_obj
의 결과로 0을 반환하고, gen_empty_region
의 경우는 빈 Region(ex: 0픽셀의 리전)을 생성 하는 것이기 때문에 반환된 파라미터는 object를 가지고 있다고 본다. 따라서, count_obj
의 결과로 빈 Region의 Object의 개수 1개를 반환하게 되는 것입니다. Halcon은 empty tuple과 empty region은 혼동하지 말아야 한다고 강조하고 있습니다.
약간의 말장난 같지만, 실제로 이것의 차이를 인지하고 있는것은 매우 중요하며, 이 작은 차이로 인해 예상하지 못한 동작이나 오류를 야기할 수 있으니 주의가 필요합니다.
Description of gen_empty_obj
The operator gen_empty_obj creates an empty tuple. This means that the output parameter does not contain any objects. Thus, the operator count_obj returns 0. However, clear_obj can be called for the output. It should be noted that no objects must not be confused with an empty region. In case of an empty region, i.e. a region with 0 pixels count_obj returns the value 1.
For a short description of the iconic objects that are available in HALCON see the introduction of chapter Object.
Description of gen_empty_region
The operator gen_empty_region creates an empty region. This means that the output parameter contains an object. Thus, count_obj returns 1. The area of the region is 0. Most of the shape features are undefined (0). It should be noted that an empty region must not be confused with the empty tuple.
실제 Region개수를 똑바로 세고싶다면?
아쉽게도, 할콘에서 Region의 개수를 세어주는 프로시저를 제공하고 있지 않는 것으로 보여, Region개수를 정확히 세고자 한다면 직접 추가적인 처리를 하여 안전하게 사용해야합니다.
위의 할콘 문서에서 Empty Region 은 “The area of the region is 0.”인 Object로 정의하고 있기 때문에 아래와 같이 IsEmptyRegion
이라는 Region의 area
가 0인지 확인해주는 메서드를 구현한 다음,
1 | public class Vision { |
Region 개수를 구하기 위해 CountObj를 사용하는 곳에서 그 object가 EmptyRegion인지 확인해주기만 하면 됩니다. 만약 이때 EmptyRegion이라면 CountObj
가 1을 반환하게 될것이므로 아래와 같이 직접 0을 반환하도록 하면 됩니다.
1 | Vision.IsEmpty(hobject) ? 0 : hobject.CountObj(); |