【问题标题】:crop image from polygon_perimeter从 polygon_perimeter 裁剪图像
【发布时间】:2020-07-19 22:11:14
【问题描述】:

我正在尝试裁剪图像。 我在要裁剪的部分周围创建了一个多边形。

xs = []
ys = []
for props in measurements:
    minr, minc, maxr, maxc = props.bbox
    by = (minc , maxc, maxc, minc, minc)
    bx = (minr, minr, maxr, maxr, minr)
    xs.append(bx)
    ys.append(by)
    
    
r = xs[0] 
c = ys[0] 


rr,cc = draw.polygon_perimeter(r ,c)
img_blob[rr,cc] = 1
cool = img_blob[rr,cc]

[rows, columns] = np.where(cool)
row1 = min(rows)
row2 = max(rows)
col1 = min(columns)
col2 = max(columns)


cropped = img_blob[row1 : row2, col1:col2]
plt.imshow(cropped)

通过上面的代码,我得到了

ValueError to be 'not enough values to unpack (expected 2, got 1)'

我怎样才能只获得polygon_perimeter 所在的位置以便我可以根据它进行裁剪?

我不能使用cv2PIL,只能使用skimagenumpy

【问题讨论】:

    标签: python crop scikit-image


    【解决方案1】:

    cool 是一个仅包含 1 的一维数组,因为您使用 cool = img_blob[rr, cc]img_blog 中提取它们。使用坐标列表进行索引称为fancy indexing,它始终为您提供与索引形状相同的数组,在本例中为一维。

    所以,当你执行np.where(cool) 时,你会得到一个长度为 1 的元组,具有 1d 坐标,但表达式 rows, cols = np.where(cool) 需要两个值,所以它会报错。 (在完整的回溯中查看引发 ValueError 的 总是很好的。)

    改为使用rows, columns = np.where(img_blob),一切正常! ?

    【讨论】:

      猜你喜欢
      • 2016-08-14
      • 2016-02-06
      • 2013-04-02
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多