【发布时间】:2019-02-24 14:58:23
【问题描述】:
要求是从二值图像中裁剪感兴趣的区域。
我需要通过删除感兴趣区域周围的额外空间来从二进制图像中获取矩形图像。
例如: 从这张Original 图像中,我只想要标有黄色矩形的region of interest。
注意:黄色矩形仅供参考,它不存在于将要处理的图像中。
我尝试了以下 python 代码,但它没有提供所需的输出。
from PIL import Image
from skimage.io import imread
from skimage.morphology import convex_hull_image
import numpy as np
from matplotlib import pyplot as plt
from skimage import io
from skimage.color import rgb2gray
im = imread('binaryImageEdited.png')
plt.imshow(im)
plt.title('input image')
plt.show()
# create a binary image
im1 = 1 - rgb2gray(im)
threshold = 0.8
im1[im1 <= threshold] = 0
im1[im1 > threshold] = 1
chull = convex_hull_image(im1)
plt.imshow(chull)
plt.title('convex hull in the binary image')
plt.show()
imageBox = Image.fromarray((chull*255).astype(np.uint8)).getbbox()
cropped = Image.fromarray(im).crop(imageBox)
cropped.save('L_2d_cropped.png')
plt.imshow(cropped)
plt.show()
谢谢。
【问题讨论】:
-
我的回答解决了你的问题吗?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步为您提供帮助。谢谢。 meta.stackexchange.com/questions/5234/…