在 python 中使用 PIL 包中的 Image 进行图像裁剪时,传入的四元组信息为 [left, top, right, bottom],而 MATLAB 中的 imcrop 传入的四元组为 [xmin, ymin, weight, height]。
也就是说,在 python 中传入的是矩形框左上角和右下角的坐标,而在 MATLAB 中传入的是矩形框左上角与宽、高信息。
假设 r e c t p y t h o n = [ l e f t , t o p , r i g h t , b o t t o m ] rect_{python} = [left, top, right, bottom] rectpython=[left,top,right,bottom],则 r e c t m a t l a b = [ l e f t , t o p , r i g h t − l e f t , b o t t o m − t o p ] rect_{matlab} = [left, top, right-left, bottom-top] rectmatlab=[left,top,right−left,bottom−top]。
#----------- python --------------#
from PIL import Image
image = Image.open(img_path)
cropped = image.crop(rect_python)
cropped.show()
#----------- matlab -------------#
I = imread(img_path)
I2 = imcrop(I,rect_matlab)
imshow(I2)
虽然这是一个小问题,但是也很容易被忽略。