Reputation: 25
I have this image that has been converted to binary, all I need is the coordinate data (x-y) of all the white pixels but in excel format (.xlsx or .xls) like this, how do I get it?
Previously, I've tried in Matlab using bw.boundaries
command, but all I got was the coordinates of the edges (not the full area). Now i want to do it in python, is there a way to do that?
Thank you in advance.
Upvotes: 1
Views: 942
Reputation: 5294
import imageio
from skimage.color import rgb2gray
im = rgb2gray(imageio.imread("img.jpg"))
np.savetxt('img.csv', np.argwhere(img > 0.5), fmt='%d', delimiter=',')
or if your image is stored as uint8
np.savetxt('img.csv', np.argwhere(img > 127), fmt='%d', delimiter=',')
or if your image is stored as a binary array
np.savetxt('img.csv', np.argwhere(img != 0), fmt='%d', delimiter=',')
EDIT: a little example for OP to hopefully better understand how this works
import numpy as np
import imageio
# let's load a sample image
cat = imageio.imread('imageio:chelsea.png')
# let's turn it into b/w by thresholding the read channel
cat_bw = cat[..., 0] > 127
# now we can get the white pixel coordinates
white_coords = np.argwhere(cat_bw)
# you could save them to csv with
np.savetxt('img.csv', white_coords, fmt='%d', delimiter=',')
you would get something like
$ cat img.csv
0,0
0,1
0,2
0,3
0,4
[...]
299,445
299,446
299,447
299,448
299,449
299,450
Not sure how are you going to process them in Excel, that is beyond my expertise but let's say you want make be sure the coordinates are what we want them to be.
Let's plot our b/w chelsea (cat_bw
):
Now let's try to turn our coordinates in the original image to another color, let's make it pink in a super-dumb not numpythonic way:
for x, y in white_coords:
cat[x, y] = [219, 49, 190]
and plot it:
So, it seems to me the white_coords
array, and the csv file where we saved them, do actually contain our white pixel coordinates.
Upvotes: 2