python - 使用 skimage 从分割图像中查找和调整像素数据
问题描述
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 数据中的数据相同的数据。
提前感谢您的帮助。
解决方案
你可以使用resize方法skimage
来调整你的段
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)
推荐阅读
- c++ - 内联静态常量与静态常量变量
- google-api-nodejs-client - Google Drive v3 文件夹创建
- excel - If 语句公式破坏 VBA 循环代码
- javascript - pdfjs:使用正确的换行符/带空格从 pdf 获取原始文本
- selenium - 当我的断言通过时如何停止执行?
- angular - ionic3 中的深层链接:不要使用 Gmail 链接启动 ionic 应用
- c# - 从数据库中获取数据后如何多次提高排序列表的性能
- sql - 将 HTML 解析为 SQL Server 中的列
- sdk - VLC Vetinari(360 度视频).NET SDK
- java - 检查条件并在java中发送邮件