0

I have a Mat Dist (CV_8U) done by distanceTransform.

Now I have to check each coordinate of Dist is > 0 and modify the value of another Mat M = Mat :: zeros

the code is

      int main(){
              ....

               for(i=0;i<Dist.rows;++i)
               {
                    for(j=0;j<Dist.cols;++j)
                    {
                   if(Dist.at<uchar>(i,j) > 0){
                     M.at<uchar>(i,j)=2;
                      }
                    }
              }
            ....
            }

but I error cv :: exception.

I looked in the documentation and elsewhere , I tried to change from uchar to vec3b . I modified the exception in visual studio 2015 but nothing . Where am I wrong?

Miki
  • 40,887
  • 13
  • 123
  • 202
kea
  • 103
  • 3
  • 5

1 Answers1

0

The function distanceTransform does not return ad CV_8U, it is CV_32 as we can see in the documentation:

dst – Output image with calculated distances. It is a 32-bit floating-point, single-channel image of the same size as src .

So the code should not read uchar, but float

...
if(Dist.at<float>(i,j) > 0.f)
...

In case you used the labels from distance transform, in the documentation we have the following:

labels – Optional output 2D array of labels (the discrete Voronoi diagram). It has the type CV_32SC1 and the same size as src . See the details below.

So, in this case you should access it as an int

...
if(Dist.at<int>(i,j) > 0)
...

I hope this helps you.

By the way, maybe an epsilon value instead of the 0 will be better....

api55
  • 11,070
  • 4
  • 41
  • 57