我有一個分割成超像素的影像,如下所示:
from skimage.data import astronaut
img = astronaut()
segments_slic = slic(img, n_segments=250, compactness=10, sigma=1,
start_label=1)
fig = plt.figure(figsize = (16,8));
plt.imshow(mark_boundaries(img, segments_slic))
并得到以下影像:

我希望在每個超像素周圍切割一個補丁。例如,考慮一下頭盔發光部分周圍的紅色補丁:

如果我想仔細(手動)查看使用的片段plt.imshow(segments_slic[425:459,346:371]),我會在片段周圍得到這個補丁:

具有特定超像素標簽的像素在行425:459和列上伸展346:371。
目前,我正在這樣做: patch = list()
for superpixel in np.unique(segments_slic ):
x_min = np.min(np.where(segments == 15)[0]);
x_max = np.max(np.where(segments == 15)[0]);
y_min = np.min(np.where(segments == 15)[1]);
y_max = np.max(np.where(segments == 15)[1]);
patches.append(I[x_min:x_max,y_min:y_max,:]);
不確定它是否正確,盡管它似乎很好。為每個超像素生成這樣的補丁的最佳方法是什么?此外,是否可以將補丁中不屬于超像素的像素設定為黑色?
uj5u.com熱心網友回復:
您可以通過如下方式使用regionprops和訪問補丁坐標region.bbox
from skimage.data import astronaut
import matplotlib.pyplot as plt
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.measure import regionprops
import matplotlib.patches as mpatches
img = astronaut()
segments_slic = slic(img, n_segments=250, compactness=10, sigma=1, start_label=1)
fig, ax = plt.subplots(figsize=(16, 8))
ax.imshow(img)
for region in regionprops(segments_slic):
# 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)
# access patch via img[minr:maxr, minc:maxc]
plt.imshow(mark_boundaries(img, segments_slic))
plt.show()
這導致

示例改編自此處。
編輯:此外,region.image您可以使用您所在地區的面具將其他地區設定為黑色。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435693.html
上一篇:用好這28個工具,開發效率爆漲
