Python: How to check if an array is contained in another array using `numpy.all`and `numpy.any`? -


i working on detecting areas on image using scikit-image. able detect blobs using blob_doh function. able find regions using canny edge detector , labeling.

now want check if blobs, found before, inside of regions, , sort out blobs not in of regions. want draw blobs inside regions.

i trying implement using numpy.all , numpy.any misunderstand way these functions work. here have:

for region in regions:     # list of pixels' coords in region     pixel_array = region.coords      # check if blob inside region     blob in blobs_doh:         y, x, r = blob          if np.any(pixel_array == [x, y]):             c = plt.circle((x, y), r, color='red', linewidth=1, fill=false)             minr, minc, maxr, maxc = region.bbox             rect = mpatches.rectangle((minc, minr), maxc - minc, maxr - minr, fill=false, edgecolor='lime', linewidth=1)             # draw blobs , regtangles             ax.add_patch(c)             ax.add_patch(rect)             break 

so, do. region array of shape [[a1, b1], [a2, b2], [a3, b3], ..., [an, bn]], , blob array of shape [c, d]. idea check if there sub-array in region equal blob. of course in trivial way searching in loop, thought there more efficient way of doing this, , tried use numpy.all , numpy.any. unfortunately, cannot working correctly. line np.any(pixel_array == [x, y]) checks first element of blob not sub-array [x, y] whole. tried different combinations of .any , .all using axis argument:

np.any(pixel_array == [x, y], axis = 1).all(axis = 0) 

but not acceptable result.

please me task. better way of performing such check?

thank you.

you can if convert pixel_array list. not sure how efficient be, works:

if [x,y] in pixel_array.tolist(): 

edit:

looks has timed lots of different options in answer. tolist() solution above isn't bad, best option on range of scenarios seems be:

 if any(np.equal(pixel_array,[x,y]).all(1)): 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -