0

What's the best way to iterate over labels pixel positions in relation to source image retrieved from opencv connectedComponentsWithStats? Currently, I'm putting the labels over a blank background the size of the source image like the following and iterative over the source image pixels to find where the labels are located:

bg = np.uint8(np.zeros(labels.shape[:2])) 
bg[np.where(labels == 1)] = 255
# Start pixel by pixel iteration on bg, but limited to their bounding box taken from stats

but I feel like there must be an easier way doing it without having to put them on a blank background. Any thoughts?

Yasin
  • 609
  • 1
  • 10
  • 22
  • if you just want to speed up, you can iterate over subimages after generating a bounding box. – Micka Oct 21 '19 at 08:08

1 Answers1

1

It was easier than I thought! You can simply iterate labels pixel like this.

y = np.where(labels == 1)[1]
x = np.where(labels == 1)[0]
Yasin
  • 609
  • 1
  • 10
  • 22