Finding matching submatrices inside a matrix

前端 未结 2 1723
慢半拍i
慢半拍i 2021-02-14 20:19

I have a 100x200 2D array expressed as a numpy array consisting of black (0) and white (255) cells. It is a bitmap file. I then have 2D shapes (it\'s easiest to think of them as

2条回答
  •  时光说笑
    2021-02-14 21:06

    Here is a method you may be able to use, or adapt, depending upon the details of your requirements. It uses ndimage.label and ndimage.find_objects:

    1. label the image using ndimage.label this finds all blobs in the array and labels them to integers.
    2. Get the slices of these blobs using ndimage.find_objects
    3. Then use set intersection to see if the found blobs correspond with your wanted blobs

    Code for 1. and 2.:

    import scipy
    from scipy import ndimage
    import matplotlib.pyplot as plt
    
    #flatten to ensure greyscale.
    im = scipy.misc.imread('letters.png',flatten=1)
    objects, number_of_objects = ndimage.label(im)
    letters = ndimage.find_objects(objects)
    
    #to save the images for illustrative purposes only:
    plt.imsave('ob.png',objects)
    for i,j in enumerate(letters):
        plt.imsave('ob'+str(i)+'.png',objects[j])
    

    example input:

    enter image description here

    labelled:

    enter image description here

    isolated blobs to test against:

    enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

提交回复
热议问题