python - Python:如何正确分割此图像?
问题描述
我想分割(隔离)这张图片中显示的棒状结构:
我能做到的最好的就是这个
# Imports the libraries.
from skimage import io, filters
import matplotlib.pyplot as plt
import numpy as np
# Imports the image as a numpy array.
img = io.imread('C:/Users/lopez/Desktop/Test electron/test.tif')
# Thresholds the images using a local threshold.
thresh = filters.threshold_local(img,301,offset=0)
binary_local = img > thresh # Thresholds the image
binary_local = np.invert(binary_local) # inverts the thresholded image (True becomes False and vice versa).
# Shows the image.
plt.figure(figsize=(10,10))
plt.imshow(binary_local,cmap='Greys')
plt.axis('off')
plt.show()
产生这个结果
但是,正如您从分段图像中看到的那样,我还没有设法分离出杆。应该是黑色背景的东西充满了相互连接的结构。有没有办法将棒状结构与图像中的所有其他元素巧妙地隔离开来?
原图可以从这个网站下载
https://dropoff.nbi.ac.uk/pickup.php
声明 ID:qMNrDHnfEn4nPwB8
领取密码:UkwcYoYfXUfeDto8
解决方案
这是我使用 Meijering 过滤器的尝试。Meijering 过滤器在寻找管状结构时依赖于对称性,因此杆重叠的区域(破坏管状形状的对称性)不能很好地恢复,如下面的叠加图所示。
此外,还有一些随机的废话,我很难用数字方式摆脱,但也许你可以在成像之前多清理一下你的准备工作。
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.transform import rescale
from skimage.restoration import denoise_nl_means
from skimage.filters import meijering
from skimage.measure import label
from skimage.color import label2rgb
def remove_small_objects(binary_mask, size_threshold):
label_image = label(binary_mask)
object_sizes = np.bincount(label_image.ravel())
labels2keep, = np.where(object_sizes > size_threshold)
labels2keep = labels2keep[1:] # remove the first label, which corresponds to the background
clean = np.in1d(label_image.ravel(), labels2keep).reshape(label_image.shape)
return clean
if __name__ == '__main__':
raw = imread('test.tif')
raw -= raw.min()
raw /= raw.max()
# running everything on the large image took too long for my patience;
raw = rescale(raw, 0.25, anti_aliasing=True)
# smooth image while preserving edges
smoothed = denoise_nl_means(raw, h=0.05, fast_mode=True)
# filter for tubular shapes
sigmas = range(1, 5)
filtered = meijering(smoothed, sigmas=sigmas, black_ridges=False)
# Meijering filter always evaluates to high values at the image frame;
# we hence set the filtered image to zero at those locations
frame = np.ones_like(filtered, dtype=np.bool)
d = 2 * np.max(sigmas) + 1 # this is the theoretical minimum ...
d += 2 # ... but doesn't seem to be enough so we increase d
frame[d:-d, d:-d] = False
filtered[frame] = np.min(filtered)
thresholded = filtered > np.percentile(filtered, 80)
cleaned = remove_small_objects(thresholded, 200)
overlay = raw.copy()
overlay[np.invert(cleaned)] = overlay[np.invert(cleaned)] * 2/3
fig, axes = plt.subplots(2, 3, sharex=True, sharey=True)
axes = axes.ravel()
axes[0].imshow(raw, cmap='gray')
axes[1].imshow(smoothed, cmap='gray')
axes[2].imshow(filtered, cmap='gray')
axes[3].imshow(thresholded, cmap='gray')
axes[4].imshow(cleaned, cmap='gray')
axes[5].imshow(overlay, cmap='gray')
for ax in axes:
ax.axis('off')
fig, ax = plt.subplots()
ax.imshow(overlay, cmap='gray')
ax.axis('off')
plt.show()
如果这段代码写成论文,我想要一份致谢和一份论文的副本。;-)
推荐阅读
- c++ - 你愿意就我的程序程序给我反馈吗?
- c# - 当使用 Fody.PropertyChanged 将文本添加到文本框时,为什么我的按钮没有启用?
- swiftui - 如何在 SwiftUI 中显示整个 CALayer 内容?
- c++ - c++ 类对象与另一个类的序列化
- asp.net - 获取“用户'TestUser'登录失败。” 在通过 ASP.Net 程序连接到 DB 期间
- javascript - 请问有什么方法可以将这些数据处理成所需的格式吗?
- c# - HttpPostedFileBase 有错误 (MVC) 红色下划线
- python - 数据算法 (Python)
- python - 是否可以在 TensorFlow py_function 中使用调用会话?
- python-3.x - 尝试使用 python shell 在 python3 中向客户端发送消息但仍然出现错误