Crop image using mask and Python scikit-image

∥☆過路亽.° 提交于 2021-01-28 07:51:04

问题


I'm working in image proceesing and I have the following code to obtain the convex hull of a image:

from skimage import io
from skimage.color import rgb2gray
from skimage.morphology import convex_hull_image

original = io.imread('test.png')

image = rgb2gray(original)

chull = convex_hull_image(image)

I want to crop the original image according to the convex hull in order to eliminate empty space that is in the image (original image attached), and have an image that only contains what is inside the convex hull. How could I crop the original image to reduce its size? (deleting the empty space at left and right)

Thank you.


回答1:


You can use min and max to find the border of the convex hull image.

import numpy as np
[rows, columns] = np.where(chull)
row1 = min(rows)
row2 = max(rows)
col1 = min(columns)
col2 = max(columns)
newImage = original[row1:row2, col1:col2]


来源:https://stackoverflow.com/questions/56650685/crop-image-using-mask-and-python-scikit-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!