当前位置: 首页 > 面试题库 >

Python equivalent to Matlab funciton 'imfill' for grayscale?

周宏伯
2023-03-14
问题内容

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.


问题答案:

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


 类似资料:

相关阅读

相关文章

相关问答