Contour Tracing

Image Recognition 2008. 5. 22. 22:51

이진 영상에서 전경의 경계를 추출하여 저장하는 함수이다. 전경의 최외곽 경계는 8-방향 연결성을 기준으로 추적하며(자세한 내용은 chain code 참조), 전경의 두께가 1픽셀인 경우에도 닫힌 윤곽선을 생성하도록 설계하였다. 이 동작은 필요에 따라 변경할 수 있다. 또한 서로 연결되지 않은 여러 개의 blob를 처리할 수 있도록 출력은 벡터(vector) 컨테이너를 사용하였다.

struct Cntr {
   int x, y ; //position;
   int idirn ;//chain-code;
   Cntr() {} 
   Cntr(int x_, int y_, int idirn_)  : x(x_), y(y_), idirn(idirn_){}
};
typedef std::vector<Cntr> CntrVector;
#define FGVAL   255
#define BGVAL   0
#define VISITED 33          /* pixel value on accepted contour */
int GetNextCntr (BYTE** image, int *x, int *y, int *idirn);

int ContourTrace (BYTE** image/*binary image(FGVAL,BGVAL)*/, int width, int height,
                 std::vector<CntrVector* > &CntList) {
    /* CAUTION:: one-pixel border should have BGVAL!!!!*/
    for (int x = width; x-->0;)  image[0][x] = image[height-1][x] = BGVAL;
    for (int y = height; y-->0;) image[y][0] = image[y][width-1] = BGVAL;
    
    for (int y = height-1; y-->1;) {
        for (int x = width-1; x-->1;) {
            if (image[y][x] == FGVAL && image[y][x - 1] == BGVAL) {
                CntrVector *pCntrVec = new CntrVector ;
                int idirn = 2;
                int xStart = x, yStart = y;
                pCntrVec->push_back(Cntr(x, y, idirn));
                do {
                    image[y][x] = VISITED;  /* set the default value to VISITED */
                    GetNextCntr (image, &x, &y, &idirn);
                    pCntrVec->push_back(Cntr(x, y, idirn));
                } while (!(x == xStart && y == yStart));
                CntList.push_back(pCntrVec);
            }
        }
    }
    return CntList.size();
};

/* 다음 픽셀을 검사. 체인 코드; */
int GetNextCntr (BYTE** image, int *x, int *y, int *idirn);

'Image Recognition' 카테고리의 다른 글

Gaussian Mixture Model  (2) 2008.06.07
Rasterizing Voronoi Diagram  (0) 2008.05.26
RANSAC Algorithm  (0) 2008.05.24
Gausssian Scale Space  (1) 2008.05.22
Watershed Algorithm 적용의 예  (2) 2008.05.21
,