问题
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