简体   繁体   中英

2D numpy Array to skimage

I am wondering how to convert a numpy array in the form [x, y] (positions of pixels):

[[ 93  58]
 [ 94  58]
 [ 95  58]
 ..., 
 [ 99 142]
 [100 142]
 [101 142]]

to a form that works with skimage. To do this I think I need the array normalized to some canvas size width x height that fits the dataset (say 500 x 500).

Ultimately I want to perform edge/contour detection on this array.

http://scikit-image.org/docs/dev/auto_examples/edges/plot_contours.html

How can I normalize this data so that its in the form skimage requires?

http://scikit-image.org/docs/dev/user_guide/data_types.html

Without calling any external functions, this is a simple way to transform/convert a series of x,y datapoints into a 2 dimensional array suitable for use in skimage:

def xy_to_binary2d(ts):
    '''Convert a list of (x,y) tuples to binary 2d format acceptable to skimage.'''
    if ts.dtype != 'int32': 
        print('Only integer input is supported.')

    xmax,ymax = ts.max(axis=0)
    __,ymin = ts.min(axis=0)

    if ymin < 0:
        print('Negative integers are not supported.')

    r = np.zeros((ymax+2,xmax+2))
    for each in ts:r.itemset(each[1],each[0])

    return r

Lets test it:

ts =np.array([[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13]])
xy_to_binary2d(ts)

Output:

array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

And, a pretty picture... plt.imshow(xy_to_binary2d(ts))

2D skimage格式的斐波那契序列

Found a decent solution. Use matplotlib to generate an rgb representation of the pixel locations [x,y] numpy > use skimage color.rgb2gray to convert this to skimage format.

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)

ax.axis('off')
fig.patch.set_facecolor('white')
print(data.shape)
ax.fill(data[:, [0]], data[:, [1]],'black')

# Set canvas size
mi = min(x_min, y_min)
ma = max(x_max, y_max)
ax.set_xlim(mi, ma)
ax.set_ylim(mi, ma)
fig.canvas.draw()
buf = fig.canvas.tostring_rgb()
ncols, nrows = fig.canvas.get_width_height()
new_data = np.fromstring(buf, dtype=np.uint8).reshape(nrows, ncols, 3)
new_data.reshape(nrows, ncols, 3)

image = color.rgb2gray(new_data)

return image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM