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

📄 centroid.cpp

📁 图像处理软件,功能比较基础
💻 CPP
📖 第 1 页 / 共 3 页
字号:
   int **cor;
   int min;
   
   cor = (int **)fspace_2d(row,col,sizeof(int));
   if(!cor) return;
  
   for(i=0; i<row; i++)
   for(j=0; j<col; j++)  cor[i][j] = 0;

   for(i=temrow/2; i<row-temrow/2; i++)
   for(j=temcol/2; j<col-temcol/2; j++)
   {
      for(k=0; k<temrow; k++)
      for(l=0; l<temcol; l++)
      {
         cor[i][j] += abs(oriimg[k+i-temrow/2][l+j-temcol/2]-temimg[k][l]);
      }
   }   

   /* search the position with the smallest value */
   min = 30000;
   for(i=0; i<row; i++)
   for(j=0; j<col; j++)
   {
      if(cor[i][j] && (min>cor[i][j])) 
      {
        min = cor[i][j];
        *r = i;
        *c = j;        
      } 
   }
}

void CentroidTrackWithCorr(unsigned char **oriimg,int row,int col,RegionInfo *target)
{
   FRAME SearchWin;
   int dr,dc;       /*   */
   double ratio;       /* expended ratio  */
   int threshold;
   int SearchWinRow,SearchWinCol;
   int labelnum;    /* label region number */
   unsigned char **segimg;
   RegionInfo region[10],temp;
   double aera_ratio;
   double mean_ratio;
   int i,j,k;
   int newr,newc;
   unsigned char **searchimg,**temimg;
   int found=0;

   ratio = 1.0;
   dr = (int)((target->down - target->up)*ratio);
   dc = (int)((target->right - target->left)*ratio);
   if(dr<5) dr = 8;
   if(dc<5) dc = 8;
   /* define the search window  */
   SearchWin.up = target->up - dr;
   SearchWin.down = target->down + dr;
   SearchWin.left = target->left - dc;
   SearchWin.right = target->right + dc;
   SearchWinRow = SearchWin.down - SearchWin.up;
   SearchWinCol = SearchWin.right - SearchWin.left;

 
   /* get the segmented threshold */
//   threshold = NonlineOtsu(oriimg,SearchWin.up,SearchWin.left,SearchWinRow,SearchWinCol,0.1);
   threshold = Otsu1(oriimg,SearchWin.up,SearchWin.left,SearchWinRow,SearchWinCol);
//   threshold = GrdientSeg(oriimg,SearchWin.up,SearchWin.left,SearchWinRow,SearchWinCol);
   
   segimg = (unsigned char **)fspace_2d(SearchWinRow,SearchWinCol,sizeof(unsigned char));   
   if(!segimg) return ;
//   searchimg = (unsigned char **)fspace_2d(SearchWinRow,SearchWinCol,sizeof(unsigned char));   
//   if(!searchimg) return ;
   /* segment the selected window  */
   for(i=0; i<SearchWinRow; i++)
   for(j=0; j<SearchWinCol; j++)
   { 
      if(oriimg[i+SearchWin.up][j+SearchWin.left]>threshold) segimg[i][j] = 255;
   }

   /* label the segmented image */
   labelnum = Label(segimg,SearchWinRow,SearchWinCol,LABELMODE_1,4);
   
   /* get the segmented region's information,including aera,mean,range,centroid,etc. */
   for(k=0; k<labelnum; k++)
   {
      region[k].left = SearchWinCol;
      region[k].up = SearchWinRow;
      region[k].right = 0;
      region[k].down = 0;
      region[k].mean = 0;
      region[k].aera = 0;
      region[k].centroidr = 0;
      region[k].centroidc = 0;
      region[k].distance = 0;
      region[k].aera = 0;
      region[k].mean = 0;
   }

   for(i=0; i<SearchWinRow; i++)
   for(j=0; j<SearchWinCol; j++)
   {
      if(segimg[i][j])
      {
         k = segimg[i][j];
         if(i<region[255-k].up) region[255-k].up = i;
         if(i>region[255-k].down) region[255-k].down = i;
         if(j<region[255-k].left) region[255-k].left = j;
         if(j>region[255-k].right) region[255-k].right = j;
         region[255-k].aera += 1;
         region[255-k].mean += oriimg[i+SearchWinRow][j+SearchWinCol];
         region[255-k].centroidr += i*oriimg[i+SearchWinRow][j+SearchWinCol];
         region[255-k].centroidc += j*oriimg[i+SearchWinRow][j+SearchWinCol];
      }
   }

   for(k=0; k<labelnum; k++)
   {
      region[k].centroidr = region[k].centroidr/region[k].mean;
      region[k].centroidc = region[k].centroidc/region[k].mean;
      region[k].mean /= region[k].aera;
      region[k].distance = sqrt((region[k].centroidr+SearchWin.up-target->centroidr)*(region[k].centroidr+SearchWin.up-target->centroidr)
                 + (region[k].centroidc+SearchWin.left-target->centroidc)*(region[k].centroidc+SearchWin.left-target->centroidc));
   }
   /* sort region according the distance */
   for(i=0; i<labelnum; i++)
   for(j=i+1; j<labelnum; j++)
   {
      if(region[j].distance<region[i].distance)
      {
         temp.aera = region[i].aera;
         temp.mean = region[i].mean;
         temp.distance = region[i].distance;
         temp.left = region[i].left;
         temp.up = region[i].up;
         temp.right = region[i].right;
         temp.down = region[i].down;            
         temp.centroidr = region[i].centroidr;
         temp.centroidc = region[i].centroidc;

         region[i].aera = region[j].aera;
         region[i].mean = region[j].mean;
         region[i].distance = region[j].distance;
         region[i].left = region[j].left;
         region[i].up = region[j].up;
         region[i].right = region[j].right;
         region[i].down = region[j].down;            
         region[i].centroidr = region[j].centroidr;
         region[i].centroidc = region[j].centroidc;

         region[j].aera = temp.aera;
         region[j].mean = temp.mean;
         region[j].distance = temp.distance;
         region[j].left = temp.left;
         region[j].up = temp.up;
         region[j].right = temp.right;
         region[j].down = temp.down;            
         region[j].centroidr = temp.centroidr;
         region[j].centroidc = temp.centroidc;
      }
   }

   /* get the target region in search window  */
   found = 0;
   for(k=0; k<labelnum; k++)
   {
      mean_ratio = fabs(region[k].mean-target->mean)/target->mean;
      aera_ratio = fabs(region[k].aera-target->aera)/target->aera;
      if(mean_ratio<0.3 && aera_ratio<0.4)
      {
         target->left = region[k].left;
         target->up = region[k].up;
         target->down = region[k].down;
         target->right = region[k].right;
         target->mean = region[k].mean;
         target->aera = region[k].aera;
         target->distance = region[k].distance;
         target->centroidr = region[k].centroidr;
         target->centroidc = region[k].centroidc;
         k = labelnum+1;   /* end the cycle */
        /* adjust the coordinate */
         target->left += SearchWin.left;
         target->up += SearchWin.up;
         target->down += SearchWin.up;
         target->right += SearchWin.left;
         target->centroidr += SearchWin.up;
         target->centroidc += SearchWin.left;
         found = 1;
      }
   }

   ffree_2d((void **)segimg,SearchWinRow);     
   if(!found)
   {
      SearchWinRow += 5;
      SearchWinCol += 5;
      SearchWin.up -= 5;
      SearchWin.left -= 5;
      searchimg = (unsigned char **)fspace_2d(SearchWinRow,SearchWinCol,sizeof(unsigned char));   
      if(!searchimg) return ;
      for(i=0; i<SearchWinRow; i++)
      for(j=0; j<SearchWinCol; j++)
         searchimg[i][j] = oriimg[i+SearchWin.up][j+SearchWin.left];
      temimg = (unsigned char **)fspace_2d(target->down-target->up,target->right-target->left,sizeof(unsigned char));
      if(!temimg) return;
      CorrTrack(searchimg,SearchWinRow,SearchWinCol,temimg,target->down-target->up,target->right-target->left,&newr,&newc);
      newr = newr + SearchWin.up;
      newc = newc + SearchWin.left;
      dr = target->centroidr-newr;
      dc = target->centroidc-newc;
      target->centroidc = newc;
      target->centroidr = newr;
      target->left -= dc;
      target->up -= dr;
      target->right += dc;
      target->down -= dr;
      ffree_2d((void **)searchimg,SearchWinRow);
      return;
   }

/*   for(i=0; i<SearchWinRow; i++)
   for(j=0; j<SearchWinCol; j++)
      oriimg[i+SearchWin.up][j+SearchWin.left]=segimg[i][j];
  */ 
//   ffree_2d((void **)segimg,SearchWinRow);     
}

void track(unsigned char **img,int row,int col,double *er,double *ec,RegionInfo *target)
{
   RegionInfo prev;
   int i,j;

   prev.centroidc = target->centroidc;
   prev.centroidr = target->centroidr;
   if(CentroidTrack1(img,row,col,target))
   {
      *er = target->centroidr - prev.centroidr;
      *ec = target->centroidc - prev.centroidc;
      PushQueue(*er,*ec,20);
   }
   else
   {
      NextPosition(img,row,col,target,20);
      *er = target->centroidr - prev.centroidr;
      *ec = target->centroidc - prev.centroidc;
      PushQueue(*er,*ec,20);
   }

   /* draw frame on the original image */
   for(i=target->up; i<=target->down; i++)
   {
      img[i][target->left] = 255;
	  img[i][target->right] = 255;
   }
   for(j=target->left; j<target->right; j++)
   {
	  img[target->down][j] = 255;
	  img[target->up][j] = 255;
   }
      
}


int TrackWithCor(unsigned char **oriimg,int row,int col,RegionInfo *target)
{
   FRAME SearchWin;
   int SearchWinRow,SearchWinCol;
   unsigned char **subimg;  
   int dr,dc;
   int i,j;
   double ratio;

   ratio = 1.0;

   //define the search window
   dr = (int)((target->down - target->up)*ratio);
   dc = (int)((target->right - target->left)*ratio);
   dr = dr<10 ? 10 : dr;
   dc = dc<10 ? 10 : dc;
   /* define the search window  */
   SearchWin.up = target->up - dr;
   SearchWin.down = target->down + dr;
   SearchWin.left = target->left - dc;
   SearchWin.right = target->right + dc;
   //boundary detection
   SearchWin.up = SearchWin.up<0 ? 0 : SearchWin.up;
   SearchWin.down = SearchWin.down>=row ? row-1 : SearchWin.down;
   SearchWin.left = SearchWin.left<0 ? 0 : SearchWin.left;
   SearchWin.right = SearchWin.right>=col ? col-1 : SearchWin.right;
   SearchWinRow = SearchWin.down - SearchWin.up;
   SearchWinCol = SearchWin.right - SearchWin.left;

   //allocating the memory for the image in the search window
   subimg = (unsigned char **)fspace_2d(SearchWinRow,SearchWinCol,sizeof(unsigned char));   
   if(!subimg) return 0;
   for(i=0; i<SearchWinRow; i++)
   for(j=0; j<SearchWinCol; j++)
      subimg[i][j] = oriimg[i+SearchWin.up][j+SearchWin.left];

   //computing the correlation


   
}

⌨️ 快捷键说明

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