【问题标题】:Finding and resizing pixel data from segmented images using skimage使用 skimage 从分割图像中查找和调​​整像素数据
【发布时间】:2020-08-04 11:13:21
【问题描述】:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb


image = data.coins()[50:-50, 50:-50]

# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))

# remove artifacts connected to image border
cleared = clear_border(bw)

# label image regions
label_image = label(cleared)
# to make the background transparent, pass the value of `bg_label`,
# and leave `bg_color` as `None` and `kind` as `overlay`
image_label_overlay = label2rgb(label_image, image=image, bg_label=0)

fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(image_label_overlay)

for region in regionprops(label_image):
    # take regions with large enough areas
    if region.area >= 100:
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2)
        ax.add_patch(rect)

ax.set_axis_off()
plt.tight_layout()
plt.show()

大家好,

我正在尝试使用此代码对像素数据进行分段、调整大小并将其加载到数组中。 我想我应该使用 region.image 虽然我不确定如何调整它的大小(所有单独的图像都具有相同的大小)并将所有单独的图像加载到一个数组中。我正在尝试获取与 MNIST 数据中的数据相同的数据。

提前感谢您的帮助。

【问题讨论】:

  • 您好 Mahad,请尝试找出您的代码的确切问题并提供出现此问题的最少代码片段

标签: python scikit-learn scikit-image mnist


【解决方案1】:

您可以使用skimage 中的resize 方法来调整细分的大小

from skimage.transform import resize

# initialize segments list
segments = []
for region in regionprops(label_image):
    # take regions with large enough areas
    if region.area >= 100:
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        # crop the segment
        segment = image_label_overlay[minr:maxr,minc:maxc,:]
        # resize to 28x28 (MNIST is 28x28)
        segment = resize(segment, (28, 28))
        segments.append(segment)
# convert to numpy array
segments = np.array(segments)

【讨论】:

  • 分割后的图像是RGB的,如果你想像MNIST一样将其转换为灰度,你可以使用rgb2gray函数将你的分割转换为灰度
  • 谢谢你!一个问题:我得到的 3 位数的形状是 (3, 28, 28, 3)。如何将其转换为二维数组?
  • 将其转换为二维数组是什么意思?形状 (3, 28, 28, 3) 表示您有 3 个尺寸为 28x28 的段和 3 个颜色通道,即 RGB 颜色。如果你想将它重塑为二维数组,我不明白你想要什么图像格式,但如果你想重塑数组维度,你可以使用 numpy 中的 reshape 函数。你可以通过segments.reshape(x, y) 来做,其中 x, y 是你想要的尺寸
  • @MahadMohamed 正如所言,这是 3 位数字的确切预期形状。请避免在 cmets 中提出后续问题,如果答案解决了您的问题,请接受 - 见What should I do when someone answers my question?
猜你喜欢
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多