📜  使用 Matplotlib 计算图像的面积

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

使用 Matplotlib 计算图像的面积

让我们看看如何使用 Matplotlib 在Python中计算图像的面积。

算法:

  1. 导入 matplotlib.pyplot 模块。
  2. 使用 imread() 方法导入图像。
  3. 使用图片的shape属性获取图片的高度和宽度。它获取图像中的通道数。
  4. 计算面积为,面积=高度*宽度。
  5. 显示区域。

示例 1:考虑下图:

“GFG.jpg”

Python3
# import necessary library
import matplotlib.pyplot as plt
 
# read an image
img = plt.imread("GFG.jpg")
 
# fetch the height and width
height, width, _ = img.shape
 
# area is calculated as “height x width”
area = height * width
 
# display the area
print("Area of the image is : ", area)


Python3
# import necessary library
import matplotlib.pyplot as plt
 
# read an image
img = plt.imread("image.jpg")
 
# fetch the height and width
height, width, _ = img.shape
 
# area is calculated as “height x width”
area = height * width
 
# display the area
print("Area of the image is : ", area)


输出 :

Area of the image is : 50244

示例 2:考虑下图:

“图像.jpg”


Python3

# import necessary library
import matplotlib.pyplot as plt
 
# read an image
img = plt.imread("image.jpg")
 
# fetch the height and width
height, width, _ = img.shape
 
# area is calculated as “height x width”
area = height * width
 
# display the area
print("Area of the image is : ", area)

输出 :

Area of the image is : 213200