2

I'm thresholding an image which gives me some white regions. And I have a pixel location that's located in one of these regions. I'm using opencv connectedComponentsWithStats to get the regions and then find if the pixel is in any of these regions. How can I do that?

On that note, is there a better way of finding in which thresholded region that pixel is located?

2 Answers 2

3
numLabels, labelImage, stats, centroids = cv2.connectedComponentsWithStats(thresh, connectivity, cv2.CV_32S)

numLabels = number of labels or regions in your thresholded image

labelImage = matrix or image containing unique labels(1, 2, 3..) representing each region, background is represented as 0 in labelImage.

stats = stats is a matrix of stats which contains information about the regions.

centroids = centroid of each region.

In your case, you can use the labelImage to find out the unique label value on the pixel coordinate to find out in which region it lies in.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the answer. Just to clarify for myself, what exactly labelImage represent? Is it a multi-dimensional array of all pixels for each region? If so, why is numLabels way different from len(labelImage)? e.g. 4 vs 375
labelImage represents unique labels for your each region. It marks same unique label to connected pixels carrying a corresponding unique label. Here each region is given a unique number starting from 1 to n. As the background does not belong to any region it is given '0' label just to differentiate with other regions. labelImage is a multidimensional array. When you do len(labelImage) you are getting length of numpy array which is height of your image. What you can try here is numpy.unique to get unique values present in the labelImage.
I think i'm starting to understand how it works. Another question. How can I access/display a certain label? For example I have 4 labels and I want to display label 1.
To access coordinate values what you can do is use np.where(labelImage == 1). To display you can plot these points on blank image to get a region of only that coordinates.
And yes to visualize you can use matplotlib.pyplot.imshow function to view labelImage to get more insights on how does it look like.
|
0

You can use pointPolygonTest function to check whether a point is inside a contour or not.

So, after thresholding, find the contours in the image using findContours function. Then you can pass the contours and the point to this function to check whether the point is inside the region or not.

Since you have the connected components and stats (that you found using connectedComponentsWithStats), you can test faster using this approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.