消除小于指定数量阈值的连接像素数 [英] Eliminating number of connected pixels smaller than some specified number threshold

查看:64
本文介绍了消除小于指定数量阈值的连接像素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据,尺寸为249X250.我使用以下代码来绘制数据:

I have some data and the dimension is 249X250. I have used the following code to plot the data:

import numpy as np

import pandas as pd

import matplotlib.pyplot as pl

data = pd.read_excel("sample_data.xlsx")

x = np.arange(data.shape[0])

y = np.arange(data.shape[1])

mask_data = np.ma.masked_outside(data,0,233)


pl.contourf(y,x,mask_data)

pl.colorbar()

剧情是这样的:

现在,我想删除图右侧的较小色块,而只保留最大的色块.为此,我的逻辑是删除那些连接的像素数小于某个指定阈值的连接像素(为此,将其设为200).我该怎么办?

Now I want to remove the smaller patches on the right side of the plot and want to keep only the biggest patches. For this purpose my logic is to remove those connected pixels where the number of connected pixels are less than some specified threshold (for this purpose lets it be 200). How can I do this?

推荐答案

基本上,您要寻找的是识别图像中的所有对象.这可以通过 scipy中的 ndimage.measurements.label 完成.本质上,它会在图像中搜索连续的像素组,并为其分配标签.然后,您可以遍历那些标记的扇区并计算对象的大小(以像素为单位)并在此基础上进行过滤.

Essentially what you are looking to do is identify all objects in your image. This can be done with ndimage.measurements.label from scipy. Essentially it searches through an image for continuous groups of pixels and assigns them a label. You can then loop through those labeled sectors and count the size (in pixels) of the object and filter on that basis.

即使您是从Excel中提取数据,实际上您正在绘制的是249x250像素的图像".Excel中的每个单元实际上是一个包含值的像素".为了说明这一点,您可以完全使用matplotlib中的图像显示功能(例如 plt.imshow )

Even though you are pulling data in from Excel--what you effectively have is a 249x250 pixel "image" that you are plotting. Each cell in Excel is effectively a "pixel" containing a value. To drive this point home you could quite literally use the image-showing functions in matplotlib (e.g. plt.imshow)

import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage

xn = 250
yn = 249

# fake data to illustrate that images are just matrices of values
X = np.stack([np.arange(xn)] * yn)
Y = np.stack([np.arange(yn)] * xn).transpose()
Z = np.sin(3*np.pi * X/xn) * np.cos(4*np.pi * Y/yn) * np.sin(np.pi * X/xn)
Z[Z <.5] = 0

fig,axes = plt.subplots(1,2)
axes[0].contourf(Z)
axes[0].set_title("Before Removing Features")

# now identify the objects and remove those above a threshold
Zlabeled,Nlabels = ndimage.measurements.label(Z)
label_size = [(Zlabeled == label).sum() for label in range(Nlabels + 1)]
for label,size in enumerate(label_size): print("label %s is %s pixels in size" % (label,size))

# now remove the labels
for label,size in enumerate(label_size):
    if size < 1800:
        Z[Zlabeled == label] = 0

axes[1].contourf(Z)
axes[1].set_title("After Removing Features")

说明的结果:

这篇关于消除小于指定数量阈值的连接像素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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