我使用skimage在给定的图像中裁剪一个矩形,现在我有(x1,y1,x2,y2)作为矩形坐标,然后我已经加载了图像
image = skimage.io.imread(filename)
cropped = image(x1,y1,x2,y2)然而,这是错误的方式来裁剪图像,我该如何在skimage中以正确的方式进行裁剪
发布于 2016-02-05 08:06:32
这似乎是一个简单的语法错误。
在Matlab中,您可以使用_'parentheses'_来提取像素或图像区域。但是在Python和numpy.ndarray中,你应该使用括号来分割图像的一个区域,此外,在这段代码中,你使用了错误的方法来剪切一个矩形。
正确的剪切方法是使用:运算符。
因此,
from skimage import io
image = io.imread(filename)
cropped = image[x1:x2,y1:y2]发布于 2019-04-23 04:37:15
也可以使用skimage.util.crop()函数,如以下代码所示:
import numpy as np
from skimage.io import imread
from skimage.util import crop
import matplotlib.pylab as plt
A = imread('lena.jpg')
# crop_width{sequence, int}: Number of values to remove from the edges of each axis.
# ((before_1, after_1), … (before_N, after_N)) specifies unique crop widths at the
# start and end of each axis. ((before, after),) specifies a fixed start and end
# crop for every axis. (n,) or n for integer n is a shortcut for before = after = n
# for all axes.
B = crop(A, ((50, 100), (50, 50), (0,0)), copy=False)
print(A.shape, B.shape)
# (220, 220, 3) (70, 120, 3)
plt.figure(figsize=(20,10))
plt.subplot(121), plt.imshow(A), plt.axis('off')
plt.subplot(122), plt.imshow(B), plt.axis('off')
plt.show()具有以下输出(具有原始和裁剪后的图像):

发布于 2020-05-12 17:56:14
您可以使用skimage裁剪图像,只需对图像数组进行切片,如下所示:
image = image_name[y1:y2, x1:x2]示例代码:
from skimage import io
import matplotlib.pyplot as plt
image = io.imread(image_path)
cropped_image = image[y1:y2, x1:x2]
plt.imshow(cropped_image)https://stackoverflow.com/questions/33287613
复制相似问题