Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:创建具有未知形状的掩码_Python 3.x_Image Processing_Masking_Edge Detection - Fatal编程技术网

Python 3.x Python:创建具有未知形状的掩码

Python 3.x Python:创建具有未知形状的掩码,python-3.x,image-processing,masking,edge-detection,Python 3.x,Image Processing,Masking,Edge Detection,我正试图提取一个形状未知的面具。我要更好地解释我自己: 我的原始数据由一个矩阵组成,矩阵中的NaN或多或少地围绕着真实数据。我使用了sobel算子来检测边缘: #data is a matrix with Nan mask = np.isnan(data) data[mask] = 0 data[~mask] = 1 out = sobel(data) #sobel is a function that returns the gradient 图中报告了sobel操作的输出。由于原始数据

我正试图提取一个形状未知的面具。我要更好地解释我自己:

我的原始数据由一个矩阵组成,矩阵中的NaN或多或少地围绕着真实数据。我使用了sobel算子来检测边缘:

#data is a matrix with Nan
mask = np.isnan(data)
data[mask] = 0
data[~mask] = 1 
out = sobel(data) #sobel is a function that returns the gradient

图中报告了sobel操作的输出。由于原始数据在真实数据中也存在非一致性,sobel算子检测内部边缘。 我想尝试一种只检测外部边缘(看起来像菱形的图形)的方法。考虑到不仅这个形状可以变化(它可以是正方形或矩形),而且位置也可以改变(即可以是偏心的,或者非常小的相对于图像尺寸)。我将获得的结果应该是所有外部像素都设置为True(或False),而所有内部像素都设置为False(或True)


谢谢

一种可能的局部解决方案是使用
打开操作,定义为先腐蚀后扩张。我使用了skimage提供的一个:

from skimage.morphology import opening
#data has shape shape_1, shape_2
mask_data = np.ones((shape_1, shape_2), dtype=bool)
mask = np.isnan(data)
mask_data[_mask] = 0
mask_data = opening(mask_data).astype(bool)
这种方法返回的内容与我要查找的内容类似。如图所示,这实际上留下了一些黑色的内部圆点,但这是我发现的最好的