cv::cuda::labelComponents(const GpuMat& mask, GpuMat& components, int flags = 0, Stream& stream = Stream::Null());

I want to know how to use this function.Can somebody give me an example?

From memory that is legacy code which doesn’t work on any recent versions of CUDA. I think it may work on CUDA 7 but I would have to check.

As a result CUDA connected component labeling has been missing from OpenCV for a while. Lucking someone added it a few months back, see OpenCV: Image Processing. Personally I haven’t tested this so I would time your results in case the implementation is slow for the structures in your images.

A futher consideration is that there is currently no function to return the bbox’s (stats) for the connected regions. Luckily Nvidia’s Robert Crovella posted a workable solution
to this problem. This solution may not be optimum however the run time of it in my experiance is small enough to make it useable.

1 Like

Thank you very much for your professional answer, which is very helpful to me :smiling_face:

1 Like

do you know how to get the connected area by cuda?

I should have mentioned that cuda:: connectedComponents does not compress the labels, that is n blobs will not be labeled 1,…,n. Therefore as the post I linked to mentioned you will first need to compress the labels (relabel from 1,…,n). You could do this manually or using NPP libs. Last time I checked the NPP libs (CUDA 11.4) they were buggy, occasionally producing incorrect results. Either way you will need to compress the labels before you can easily run any futher analysis on the labels using CUDA.

Regarding calculating the area you could risk the NPP libs and not use OpenCV at all, or you could write your own kernel to calculate just the histogram.