Finding a subimage inside a Numpy image

后端 未结 5 1136
太阳男子
太阳男子 2020-12-08 03:24

I have two Numpy arrays (3-dimensional uint8) converted from PIL images.

I want to find if the first image contains the second image, and if so, find out the coordin

5条回答
  •  青春惊慌失措
    2020-12-08 03:44

    You can actually reduce this problem to a simple string search using a regex like the following implementation - accepts two PIL.Image objects and finds coordinates of the needle within the haystack. This is about 127x faster than using a pixel-by-pixel search.

    def subimg_location(haystack, needle):
        haystack = haystack.convert('RGB')
        needle   = needle.convert('RGB')
    
        haystack_str = haystack.tostring()
        needle_str   = needle.tostring()
    
        gap_size = (haystack.size[0] - needle.size[0]) * 3
        gap_regex = '.{' + str(gap_size) + '}'
    
        # Split b into needle.size[0] chunks
        chunk_size = needle.size[0] * 3
        split = [needle_str[i:i+chunk_size] for i in range(0, len(needle_str), chunk_size)]
    
        # Build regex
        regex = re.escape(split[0])
        for i in xrange(1, len(split)):
            regex += gap_regex + re.escape(split[i])
    
        p = re.compile(regex)
        m = p.search(haystack_str)
    
        if not m:
            return None
    
        x, _ = m.span()
    
        left = x % (haystack.size[0] * 3) / 3
        top  = x / haystack.size[0] / 3
    
        return (left, top)
    

提交回复
热议问题