⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cntlabel.c

📁 图像处理软件,功能比较基础
💻 C
字号:
/* The program is to label a segmented image with the method tracing the contour  */
/* writed by Zuo Zhengrong  on 3/28/2001                                          */
/* Copyright reserved                                                             */

#include "stdlib.h"
#include "math.h"

#define LZERO -1
#define RZERO 129
#define ONE   230
#define ONE1   240
#define MAXLEVEL 255
#define AeraThreshold  50

#ifndef _COORDINATE_
#define _COORDINATE_
typedef struct
{
  short   x;
  short   y;
}COORDINATE;
#endif

void TraceLabel(unsigned char **SegImg, int Row, int Col);
void OneTraceBWImg(unsigned char **SegImg, int Row, int Col, int StartX, int StartY,
        int *LeftX, int *UpY, int *RightX, int *DownY, int *Aera, int *lx, int *rx);
int NextCheckPoint(int Row, int Col, int SearchDirection, int CurrentX, int CurrentY, int * NextX, int * NextY);

void TraceLabel(unsigned char **segimg, unsigned char **labelimg, int Row, int Col)
{
  int i,j;
  int StartX,StartY;
  


void OneTraceBWImg(unsigned char **SegImg, int Row, int Col, int StartX, int StartY,
        int *LeftX, int *UpY, int *RightX, int *DownY, int *Aera, int *lx, int *rx)
{
   static int CheckPointX;
   static int CheckPointY;
   static int i,j;
   static int NextPointX, NextPointY;
   static int SearchDirection,NextDirection;           //寻找方向
   static int First;
   static int Found;
   static int changedirect;
   static int HasNextPoint;
   *LeftX = 0;
   *RightX = 0;
   *UpY = 0;
   *DownY = 0;
   *Aera  = 0;

   for(i=0; i<SROW; i++)
   {
      lx[i] = RZERO;
      rx[i] = LZERO;
   }

   CheckPointX = StartX;
   CheckPointY = StartY;

   lx[CheckPointY] = CheckPointX;
   rx[CheckPointY] = CheckPointX;
	
   SearchDirection = 6;
   First = 1;

   while (StartX != CheckPointX || StartY != CheckPointY || First == 1)
   {
      Found = 0;
      changedirect = 0;
      while (Found ==0 )
      {
          NextDirection = SearchDirection-1;
          if(NextDirection<0) NextDirection += 8;
          HasNextPoint = NextCheckPoint(Row, Col,NextDirection, CheckPointX, CheckPointY, &NextPointX, &NextPointY);	
          if(HasNextPoint && SegImg[NextPointY][NextPointX])
          {
             CheckPointX = NextPointX;
             CheckPointY = NextPointY;
             if(lx[CheckPointY]>CheckPointX) lx[CheckPointY] = CheckPointX;
             if(rx[CheckPointY]<CheckPointX) rx[CheckPointY] = CheckPointX;
             changedirect=0;
             SearchDirection = SearchDirection-2;
             if(SearchDirection<0) SearchDirection += 8;
             Found = 1;
             goto end;
          }

          HasNextPoint = NextCheckPoint(Row, Col, SearchDirection, CheckPointX, CheckPointY, &NextPointX, &NextPointY);	
          if(HasNextPoint && SegImg[NextPointY][NextPointX])
          {
             CheckPointX = NextPointX;
             CheckPointY = NextPointY;
             if(lx[CheckPointY]>CheckPointX) lx[CheckPointY] = CheckPointX;
             if(rx[CheckPointY]<CheckPointX) rx[CheckPointY] = CheckPointX;
             changedirect=0;
             Found = 1;
             goto end;
          }

          NextDirection = SearchDirection+1;
          if(NextDirection>7) NextDirection -= 8;
          HasNextPoint = NextCheckPoint(Row, Col, NextDirection, CheckPointX, CheckPointY, &NextPointX, &NextPointY);	
          if(HasNextPoint && SegImg[NextPointY][NextPointX])
          {
             CheckPointX = NextPointX;
             CheckPointY = NextPointY;
             if(lx[CheckPointY]>CheckPointX) lx[CheckPointY] = CheckPointX;
             if(rx[CheckPointY]<CheckPointX) rx[CheckPointY] = CheckPointX;
             changedirect=0;
             Found = 1;
             goto end;
          }

          changedirect++;
          if(changedirect>4) break;
end:      if(!Found)
          SearchDirection = SearchDirection+2; 
          if(SearchDirection>7) SearchDirection -= 8;
      }
      First = 0;
   }

//serch the left_up point and right_down point in lx and ly
   i=0;
   while(lx[i]==RZERO) i++;
   *UpY = i;
   i = SROW-1;
   while(lx[i]==RZERO) i--;
   *DownY = i;

   *LeftX = SROW;
   *RightX = 0;
   for(i=*UpY; i<=*DownY; i++)
   {
       if(*LeftX>lx[i]) *LeftX = lx[i];
       if(*RightX<rx[i]) *RightX = rx[i];
   }

//clear the searched region, and calculate the aera
   *Aera = 0;
   for(i=*UpY; i<=*DownY; i++)
   {
       j = lx[i];
       while(j<=rx[i]) {
           (*Aera)++;  
           SegImg[i][j] = 0;
           j++;
       }
   }

}


int NextCheckPoint(int Row, int Col, int SearchDirection, int CurrentX, int CurrentY, int * NextX, int * NextY)
{
    switch (SearchDirection)
    {
        case 0:
            *NextX = CurrentX + 1;
            *NextY = CurrentY;
            if(*NextX>=Col) return 0;
            break;
        case 1:
            *NextX = CurrentX + 1;
            *NextY = CurrentY - 1;
            if(*NextX>=Col || *NextY<0) return 0;
            break;
        case 2:
            *NextX = CurrentX;
            *NextY = CurrentY - 1;
            if(*NextY<0) return 0;
            break;
        case 3:
            *NextX = CurrentX - 1;
            *NextY = CurrentY - 1;
            if(*NextX < 0 || *NextY<0) return 0;
            break;
        case 4:
            *NextX = CurrentX - 1;
            *NextY = CurrentY;
            if(*NextX<0) return 0;
            break;
        case 5:
            *NextX = CurrentX - 1;
            *NextY = CurrentY + 1;
            if(*NextX<0 || *NextY>=Row) return 0;
            break;
        case 6:
            *NextX = CurrentX;
            *NextY = CurrentY + 1;
            if(*NextY>=Row) return 0;
            break;
        case 7:
            *NextX = CurrentX + 1;
            *NextY = CurrentY + 1;
            if(*NextX>=Col || *NextY>=Row) return 0;
            break;
        }
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -