📜  Mahotas – 裁剪图像

📅  最后修改于: 2022-05-13 01:55:49.925000             🧑  作者: Mango

Mahotas – 裁剪图像

在本文中,我们将了解如何在 mahotas 中裁剪图像。裁剪很容易通过从数组中切出正确的部分来完成,这里数组是图像对象,它是 numpy.ndarray。
在本教程中,我们将使用“luispedro”图像,下面是加载它的命令。

mahotas.demos.load('luispedro')

下面是 luispedro 图像

为了做到这一点,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令

image = image[i1:i2, j1:j2]

示例 1:

Python3
# importing required libraries
import numpy as np
import mahotas
import mahotas.demos
from mahotas.thresholding import soft_threshold
from matplotlib import pyplot as plt
from os import path
 
# loading image as grey
f = mahotas.demos.load('luispedro', as_grey = True)
 
# making plt grey
plt.gray()
 
# showing image
print("Image")
plt.imshow(f)
plt.show()
 
# cropping image
f = f[50:200, 20: 250]
 
 
# Show the image
print("Cropped Image")
plt.imshow(f)
plt.show()


Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
 
# showing image
print("Image")
imshow(img)
show()
 
     
 
# cropping image
img = img[:, 200:700]
 
 
# showing the image
print("Cropped Image")
imshow(img)
show()


输出 :

示例 2:

Python3

# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
 
# showing image
print("Image")
imshow(img)
show()
 
     
 
# cropping image
img = img[:, 200:700]
 
 
# showing the image
print("Cropped Image")
imshow(img)
show()

输出 :