Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Per pixel labeling in distanceTransform?

It would be really convenient if this function worked like

  cv::distanceTransform(mask, dist, labels, distance_type, 3, DIST_LABEL_PIXEL);

and then within a double for loop on y and x

  const int label = labels32.at<int>(y,x);
  const int label_x = label % width;
  const int label_y = label / width;

But instead the label is dependent on the non-zero pixels in the mask input (the label only increments when pixels are zero), so I have to manually build up a mapping between labels and pixel coordinates to find out where the labeled pixels actually are- or is there an easier way?

I'd take this to the dev zone but code.opencv.org appears to be down.

In my somewhat old git version of opencv disttransform.cpp:

   else    // it would be nice if DIST_LABEL_PIXEL was here for clarity
   {
   int k = 1;
   for( int i = 0;  i < src->rows; i++ )
   {
      const uchar* srcptr = src->data.ptr + src->step*i;
      int* labelptr = (int*)(labels->data.ptr + labels->step*i);

      for( int j = 0; j < src->cols; j++ )
        // it would be nice to get rid of 
        // this conditional and 
        // always increment labelptr, but then 
        // would the icvDistanceTransformEx_5x5_C1R
        // be messed up?
         if( srcptr[j] == 0 ) 
            labelptr[j] = k++;
      }
  }

  icvDistanceTransformEx_5x5_C1R( src->data.ptr, src->step, temp->data.i, temp->step,
                    dst->data.fl, dst->step, labels->data.i, labels->step, size, _mask );

Per pixel labeling in distanceTransform?

It would be really convenient if this function worked like

  cv::distanceTransform(mask, dist, labels, distance_type, 3, DIST_LABEL_PIXEL);

and then within a double for loop on y and x

  const int label = labels32.at<int>(y,x);
labels.at<int>(y,x);
  const int label_x = label % width;
  const int label_y = label / width;

But instead the label is dependent on the non-zero pixels in the mask input (the label only increments when pixels are zero), so I have to manually build up a mapping between labels and pixel coordinates to find out where the labeled pixels actually are- or is there an easier way?

I'd take this to the dev zone but code.opencv.org appears to be down.

In my somewhat old git version of opencv disttransform.cpp:

   else    // it would be nice if DIST_LABEL_PIXEL was here for clarity
   {
   int k = 1;
   for( int i = 0;  i < src->rows; i++ )
   {
      const uchar* srcptr = src->data.ptr + src->step*i;
      int* labelptr = (int*)(labels->data.ptr + labels->step*i);

      for( int j = 0; j < src->cols; j++ )
        // it would be nice to get rid of 
        // this conditional and 
        // always increment labelptr, but then 
        // would the icvDistanceTransformEx_5x5_C1R
        // be messed up?
         if( srcptr[j] == 0 ) 
            labelptr[j] = k++;
      }
  }

  icvDistanceTransformEx_5x5_C1R( src->data.ptr, src->step, temp->data.i, temp->step,
                    dst->data.fl, dst->step, labels->data.i, labels->step, size, _mask );