Problem Running Watershed Distance Transform in Opencv 4.0.0

Asked

Viewed 73 times

0

I am working on an object detection project. I use Opencv 4.0.0 and codeblocks 16.01 on Ubuntu 18.04.1. I am testing the Watershed Distance Transform technique using the code of this tutorial:

https://docs.opencv.org/4.0.0/d2/dbd/tutorial_distance_transform.html

I just changed the path to my test image, so:

// Load the image
    CommandLineParser parser( argc, argv, "{@input | ../../testes/img1.jpg | input image}" );
    Mat src = imread( parser.get<String>( "@input" ) );
    if( src.empty() )
    {
        cout << "Could not open or find the image!\n" << endl;
        cout << "Usage: " << argv[0] << " <Input image>" << endl;
        return -1;
    }
    else cout << "img ok\n";

The rest of the code is exactly the same as in the tutorial. However, when executing it, the following error message appears:

inserir a descrição da imagem aqui

Does anyone have any idea what might be going on?

EDIT: I was testing step by step of the algorithm, here the error happens in:

// Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1.0, NORM_MINMAX);
imshow("Distance Transform Image", dist);

The image dist after performing the distanceTranform() function becomes totally black, and the error happens in normalize().

  • 1

    Hello. From the error message, it seems that the problem occurs when displaying the image in a graphic window (more specifically, when setting the image in the window widget). Your code works with the original tutorial image?

  • The same thing happens. After several tests I found out where the error is (I edited the question), but I do not know how to solve...

  • You may be using a different image type from the example. The image of the example is in https://github.com/opencv/opencv/blob/master/samples/data/cards.png Compare image to image depth of both (Gray, color, 8 bits, 16 bits)?

1 answer

1

Apparently, the function of imshow() only shows images in CV_8U format. Then it was necessary to change the function parameters normalize(), being like this:

normalize(dist, dist, 0, 255.0, NORM_MINMAX, CV_8U);

In the rest the Watershed algorithm works very well, but it is necessary to be careful with these details: CV_8U, CV_32S, etc.

  • For more information on data types, see this answer. In Opencv it is necessary to be aware of this, because most functions accept only certain type of data type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.