乳房X光片对象周围的裁剪图像

2024-05-16 16:03:07 发布

您现在位置:Python中文网/ 问答频道 /正文

一点背景:

我有几张乳房X光片(CBIS-DDSM),我想裁剪成内容,这里的内容不仅仅是图像的非零部分,它是胸部区域,在胸肌下面一点(没有胸肌)

现在,我用hough线去除了胸肌,但是裁剪到内容有点棘手。这就是我发现的:

def adjust_rotate(image):
    x, y = np.nonzero(image)
    xl,xr = x.min(),x.max()
    yl,yr = y.min(),y.max()
    dst = image[xl:xr+1, yl:yr+1]
    return dst

这段代码将去掉周围的黑色像素。正如你可以从下面的图片中看到的,它包括乳房区域上方的区域,这是我正在尝试移除的区域

我没有裁剪到最小值和最大值,而是尝试从y轴上的中间索引进行裁剪,但似乎不起作用:

def adjust_rotate(image):
    x, y = np.nonzero(image)
    median = numpy.argsort(y)[len(y)//2]
    xl,xr = x.min(),x.max()
    yl,yr = y.min(),y.max()
    dst = image[xl:xr+1, median:yr+1]
    return dst

当剪切到非零索引时,会考虑左上角,因为它不是零,这会让我看到如下图像:

未移除胸肌的原始图像:

Original image 1

已删除胸肌的裁剪图像(请注意左上方区域):

1 Cropped to content

未移除胸肌的原始图像:

Original image 2

已删除胸肌的裁剪图像(请注意左上方区域):

2 Cropped to content

我想要的是:

请注意,如何仅删除乳房区域:

Removed and cropped 1

这是一个更好的示例,因为它几乎与y轴齐平:

enter image description here

未剪切的图像:

图像尺寸较大,我上传的是截图,而不是实际文件:

enter image description here

enter image description here

编辑:澄清更多信息并添加未剪切的示例图像


Tags: 图像image区域内容defminmaxdst
1条回答
网友
1楼 · 发布于 2024-05-16 16:03:07

请尝试以下代码:

from skimage import feature
from imageio import imread
from matplotlib import pyplot as plt
from matplotlib import cm
from skimage.transform import hough_line, hough_line_peaks, rotate
import numpy as np

image0 = imread("rRTtj.png")

# canny edge detection (show muscle boundary) 
image = feature.canny(image0[:,:,1], sigma=2)

# https://scikit-image.org/docs/dev/auto_examples/edges/plot_line_hough_transform.html
# Classic straight-line Hough transform
# Set a precision of 0.5 degree.
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360, endpoint=False)
h, theta, d = hough_line(image, theta=tested_angles)

# Generating figure 1
fig, axes = plt.subplots(1, 5, figsize=(10, 6))
ax = axes.ravel()

ax[0].imshow(image0, cmap=cm.gray)
ax[0].set_title('Orig')
ax[0].set_axis_off()

ax[1].imshow(image, cmap=cm.gray)
ax[1].set_title('Edge detection')
ax[1].set_axis_off()


angle_step = 0.5 * np.diff(theta).mean()
d_step = 0.5 * np.diff(d).mean()
bounds = [np.rad2deg(theta[0] - angle_step),
          np.rad2deg(theta[-1] + angle_step),
          d[-1] + d_step, d[0] - d_step]
ax[2].imshow(np.log(1 + h), extent=bounds, cmap=cm.gray, aspect=1 / 1.5)
ax[2].set_title('Hough transform')
ax[2].set_xlabel('Angles (degrees)')
ax[2].set_ylabel('Distance (pixels)')
ax[2].axis('image')

ax[3].imshow(image, cmap=cm.gray)
ax[3].set_ylim((image.shape[0], 0))
ax[3].set_axis_off()
ax[3].set_title('Detected lines')

peaks = [p for p in zip(*hough_line_peaks(h, theta, d))]
_, angle, dist = peaks[0]
# The origin is the top left corner of the original image. X and Y axis are horizontal and vertical edges respectively.
# The distance is the minimal algebraic distance from the origin to the detected line.

#print(angle, dist)
(x0, y0) = dist * np.array([np.cos(angle), np.sin(angle)])
slope = np.tan(angle + np.pi/2)
#print((x0, y0, angle, slope))

ax[3].axline((x0, y0), slope=slope)

# rotate and crop
image = rotate(image0, angle*180/np.pi, center=(0,0), resize=True)
image = image[:,int(dist):]  #
ax[4].imshow(image, cmap=cm.gray)

plt.tight_layout()
plt.show()

    

相关问题 更多 >