Python相当于Matlab函数'imfill'的灰度? [英] Python equivalent to Matlab funciton 'imfill' for grayscale?

查看:1165
本文介绍了Python相当于Matlab函数'imfill'的灰度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用OpenCV或scikit-image的实现等效于Matlab的灰度图像 imfill 功能(即灰度孔填充)?



在以下示例链接中查看imfill部分的灰度( I2 = imfill(I)



第一个,更简单的一个包含两个未定义的变量,但这是一个工作版本:

  import numpy as np 
导入scipy为sp
import scipy.n dimage

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不是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),足迹= el))
返回output_array


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

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

这篇关于Python相当于Matlab函数'imfill'的灰度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆