use*_*624 6 python image-processing scikit-image
我正在使用skimage来裁剪给定图像中的矩形,现在我将(x1,y1,x2,y2)作为矩形坐标,然后我加载了图像
image = skimage.io.imread(filename)
cropped = image(x1,y1,x2,y2)
Run Code Online (Sandbox Code Playgroud)
然而,这是裁剪图像的错误方法,我将如何以正确的方式在skimage中进行
Dar*_*ues 19
这似乎是语法上的一个简单错误.
好吧,在Matlab中,您可以使用_'parentheses'_提取像素或图像区域.但是在Python中,numpy.ndarray你应该使用括号来切割图像的一个区域,除了在这段代码中,你使用错误的方法来剪切一个矩形.
正确的切割方式是使用:操作员.
从而,
from skimage import io
image = io.imread(filename)
cropped = image[x1:x2,y1:y2]
Run Code Online (Sandbox Code Playgroud)
也可以使用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()
Run Code Online (Sandbox Code Playgroud)
具有以下输出(原始图像和裁剪图像):