图像处理链

image processing the Chain

我有下面这段代码,让我修改它,添加x[]和y[],并将边界坐标保存到x[]和y[]

代码确实将图​​像读取为矩阵 2X2,如果找到任何对象,它会计算周长。找到对象的边界。

ps。链码,基本上就是Freeman Chain Code

我将添加一张它如何工作的图片,

谁能帮帮我,我将不胜感激,

// compute the chain code of the object begninning at pixel (i,j)
//and return the direction code as NN integers in the array C[] , and coordinates of boundary in x[] and y[]

    void chain8 (struct image *x, int *c, int i, int j, int *nn)
    {
        int val,n,m,q,r, di[9],dj[9],ii,dii;
        int lastdir, jj;

    /*      Table given index offset for each of the 8 directions.          */
        di[0] = 0;      di[1] = -1;     di[2] = -1;     di[3] = -1;
        dj[0] = 1;      dj[1] = 1;      dj[2] = 0;      dj[3] = -1;
        di[4] = 0;      di[5] = 1;      di[6] = 1;      di[7] = 1;
        dj[4] = -1;     dj[5] = -1;     dj[6] = 0;      dj[7] = 1;

        for (ii=0; ii<200; ii++) c[ii] = -1;    /* Clear the code table */
        val = x->data[i][j];    n = 0;  /* Initialize for starting pixel */
        q = i;  r = j;  lastdir = 4;

        do {
           m = 0;
           dii = -1; 
           for (ii=lastdir+1; ii<lastdir+8; ii++) {     /* Look for next */
              jj = ii%8;
              if (range(x,di[jj]+q, dj[jj]+r))
            if ( x->data[di[jj]+q][dj[jj]+r] == val) {
               dii = jj;    m = 1;
               break;
            } 
           }

           if (m) {     /* Found a next pixel ... */
            if (n<200) c[n++] = dii;        /* Save direction as code */
            q += di[dii];   r += dj[dii];
            lastdir = (dii+5)%8;
           } else break;        /* NO next pixel */
           if (n>200) break;
        } while ( (q!=i) || (r!=j) );   /* Stop when next to start pixel */

        *nn = n;
    }

变量qr包含当前像素坐标。您从像素 q = i, r = j 开始。在外部循环中你做:

  1. 求边界方向索引dii。您从最后一个方向 lastdir 开始依次(逆时针)查看所有方向,找到颜色为 val 的像素。您使用 dii 作为方向索引,您可以使用查找表 didj.

  2. 从中获取像素偏移
  3. 通过将对应方向dii的像素偏移量(dx, dy)添加到当前坐标q += di[dii]; r += dj[dii];

  4. 来更新当前像素位置

您需要将边界像素的 qr 存储到数组 bxby。这很简单:

bx[n] = q;
by[n] = r;

所以你的最终代码将是

// compute the chain code of the object begninning at pixel (i,j)
//and return the direction code as NN integers in the array C[] , and coordinates of boundary in bx[] and by[]

void chain8(struct image *x, int* bx, int* by,  int *c, int i, int j, int *nn)
{
   int val, n, m, q, r, di[9], dj[9], ii, dii;
   int lastdir, jj;

   /*      Table given index offset for each of the 8 directions.          */
   di[0] = 0;      di[1] = -1;     di[2] = -1;     di[3] = -1;
   dj[0] = 1;      dj[1] = 1;      dj[2] = 0;      dj[3] = -1;
   di[4] = 0;      di[5] = 1;      di[6] = 1;      di[7] = 1;
   dj[4] = -1;     dj[5] = -1;     dj[6] = 0;      dj[7] = 1;

   for (ii = 0; ii<200; ii++) by[ii] = bx[ii] = c[ii] = -1;    /* Clear the code table */
   val = x->data[i][j];    n = 0;  /* Initialize for starting pixel */
   q = i;  r = j;  lastdir = 4;

   do {
      m = 0;
      dii = -1;
      for (ii = lastdir + 1; ii<lastdir + 8; ii++) {     /* Look for next */
         jj = ii % 8;
         if (range(x, di[jj] + q, dj[jj] + r))
            if (x->data[di[jj] + q][dj[jj] + r] == val) {
               dii = jj;    m = 1;
               break;
            }
      }

      if (m) {     /* Found a next pixel ... */
         q += di[dii];   r += dj[dii];
         if (n < 200)
         {
            c[n] = dii;        /* Save direction as code */
            bx[n] = q;
            by[n] = r;
            n++;
         }

         lastdir = (dii + 5) % 8;
      }
      else break;        /* NO next pixel */
      if (n>200) break;
   } while ((q != i) || (r != j));   /* Stop when next to start pixel */

   *nn = n;
}