Exclusiveicon
Exclusiveicon

Reputation: 33

how would you take the white pixels generated by a mask and put them into a list

I have a picture of a street, (the street has small variations of color) and with some help I was able to crop part of the street for a sample of the color I then took the color and calculated the mean and stdv and created the lower and upper boundry for a mask. I took the mask output and ran closing = cv2.morphologyEx(output, cv2.MORPH_CLOSE, kernel)

I would like to take all the white pixels that are in closing and create a list of their x,y coordinates. then I would like to take the x,y coordinates and create another list of there b,g,r values.

so then I can run it back through

blue=cropimg[:,:,0]; green=cropimg[:,:,1]; red=cropimg[:,:,2];

See image, showing: original, mask, closing, cropped areas:

original, mask, closing, cropped areas

Upvotes: 1

Views: 575

Answers (3)

ebeneditos
ebeneditos

Reputation: 2612

In order to get the lists you asked, you can do it like:

coords, colors = [], []
for y in range(closing.shape[0]):
    for x in range(closing.shape[1]):
        if np.all(closing[y, x] > 0):
            coords.append((y, x))
            colors.append(original[y, x])

Thus you get a list with the white coordinates and the BGR values in those coordinates in the original image.

Upvotes: 1

Exclusiveicon
Exclusiveicon

Reputation: 33

ebeneditos, I found that this works, but for large images it takes too much time.

 coords, colors = [], []
    for y in range(closing.shape[0]):
        for x in range(closing.shape[1]):
            if np.all(closing[y, x] > 0):
                coords.append((y, x))
                colors.append(img[y, x])

Upvotes: 1

Eric
Eric

Reputation: 97591

It sounds like you're simply looking for:

rgb = cropimg[mask,:]  # or mask > 0, if mask is not a boolean array

which will return an Nx3 array of the pixels under the mask

Upvotes: 1

Related Questions