Python中图像的裁剪前景

2024-03-29 11:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我操纵了一个jpg图像,使我的前景隔离,所有的黑色像素无处不在。 我希望能够裁剪图像,这样在前景的左右两边没有全黑的行,也没有全黑的列。 我想我可以通过Numpy数组循环得到所需的4个索引,但是我想知道是否有更直接和/或更快速的方法。在

import numpy as np
import matplotlib.pyplot as plt


im=np.array(    
[[0,0,0,0,0,0,0],
[0,0,1,1,1,0,0],    
[0,1,1,1,0,0,0],    
[0,0,1,1,0,0,0],    
[0,0,0,0,0,0,0]])

plt.imshow(im, cmap=plt.cm.gray,interpolation='nearest')

enter image description here

然后这里发生了一些事情,我得到:

enter image description here


Tags: 方法图像importnumpyasnpplt像素