python - extract "block" addresses and value from 2D numpy array -


i'm trying come algorithm extracting rectangular address , value 2d numpy array in python. input looks this:

[[1, 1, 0, 0, 0, 0, 2],  [1, 1, 0, 0, 0, 0, 2],  [0, 0, 0, 0, 0, 0, 0],  [0, 0, 0, 3, 3, 3, 0],  [0, 0, 0, 3, 3, 3, 0],  [0, 4, 4, 3, 3, 3, 0],  [0, 0, 0, 0, 0, 0, 3]] 

where there various clusters of equal data @ various locations in array. i'm trying output format consists of cluster start addresses (starty, startx), cluster end addresses (endy, endx), , value of cluster, in rectangular format. above 2d array above, series of output values (i have excluded 0 values sake of brevity, included in output):

(0, 0, 1, 1, 1) (0, 6, 1, 6, 2) (3, 3, 5, 5, 3) (5, 1, 5, 2, 4) (6, 6, 6, 6, 3) 

is there function in numpy can this? have looked through documentation, , can't seem find anything, though admittedly i'm quite new numpy, might missing something, or not putting logic of nesting functions make process 2-dimensional.

thank you!

i'm not seeing relevance of 2nd array.

just eye balling first array, can index non-zero blocks with

in [738]: arr[0:2,0:2] out[738]:  array([[1, 1],        [1, 1]])  in [739]: arr[0:2,6:7] out[739]:  array([[2],        [2]])  in [740]: arr[3:6,3:6] out[740]:  array([[3, 3, 3],        [3, 3, 3],        [3, 3, 3]])   in [741]: arr[5:6,1:3] out[741]: array([[4, 4]])  in [742]: arr[6:7,6:7] out[742]: array([[3]]) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -