Python equivalent to Matlab funciton 'imfill' for grayscale?

发布于 2021-01-29 14:09:49

Is there an implementation using OpenCV or scikit-image that is equivalent to
Matlab’s grayscale image imfill funciton (i.e. grayscale hole filling)?

See the imfill section for grayscale ( I2= imfill(I) ) in the following
example link
matlab_imfill. Or see
image: matlab_tire_ex

Here’s a link to the tire image in the example

tire

I’ve been trying to replicate the Matlab output using
scipy.ndimage.grey_closing function with varying the size parameter, but
have not been successful.

I’m using Python 3.5.

关注者
0
被浏览
181
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    Two versions of the flood-fill algorithm have been implemented in Python here:

    http://arcgisandpython.blogspot.de/2012/01/python-flood-fill-algorithm.html

    The first, simpler one contained two undefined variables, but here is a
    working version:

    import numpy as np
    import scipy as sp
    import scipy.ndimage
    
    def flood_fill(test_array,h_max=255):
        input_array = np.copy(test_array) 
        el = sp.ndimage.generate_binary_structure(2,2).astype(np.int)
        inside_mask = sp.ndimage.binary_erosion(~np.isnan(input_array), structure=el)
        output_array = np.copy(input_array)
        output_array[inside_mask]=h_max
        output_old_array = np.copy(input_array)
        output_old_array.fill(0)   
        el = sp.ndimage.generate_binary_structure(2,1).astype(np.int)
        while not np.array_equal(output_old_array, output_array):
            output_old_array = np.copy(output_array)
            output_array = np.maximum(input_array,sp.ndimage.grey_erosion(output_array, size=(3,3), footprint=el))
        return output_array
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看