zython
zython

Reputation: 1288

Get n largest regions from binary image

I have given a large binary image (every pixel is either 1 or 0).

I know that in that image there are multiple regions (a region is defined as a set of neighboring 1s which are enclosed by 0s).

The goal is to find the largest (in terms of pixel-count or enclosed area, both would work out for me for now)

My current planned approach is to:

My question is: is there a more efficient way of doing this, and are there already tested (bonus points for parallel or GPU-accelerated) implementations out there (in any of the big libraries) ?

Upvotes: 1

Views: 1275

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60780

You want to use connected component analysis (a.k.a. labeling). It is more or less what you suggest to do, but there are extremely efficient algorithms out there. Answers to this question explain some of the algorithms. See also .

This library collects different efficient algorithms and compares them.

From within Python, you probably want to use OpenCV. cv.connectedComponentsWithStats does connected component analysis and outputs statistics, among other things the area for each connected component.

With regards to your suggestion: using coordinates of pixels rather than the original image matrix directly is highly inefficient: looking for neighbor pixels in an image is trivial, looking for the same in a list of coordinates requires expensive searchers.

Upvotes: 2

Marcos C. Loures
Marcos C. Loures

Reputation: 234

You could Flood Fill every region with an unique ID, mapping the ID to the size of the region.

Upvotes: 3

Related Questions