使用 Matplotlib 计算图像的面积
让我们看看如何使用 Matplotlib 在Python中计算图像的面积。
算法:
- 导入 matplotlib.pyplot 模块。
- 使用 imread() 方法导入图像。
- 使用图片的shape属性获取图片的高度和宽度。它获取图像中的通道数。
- 计算面积为,面积=高度*宽度。
- 显示区域。
示例 1:考虑下图:
![](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Calculate_the_area_of_an_image_using_Matplotlib_0.jpg)
“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:考虑下图:
![](https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Calculate_the_area_of_an_image_using_Matplotlib_1.png)
“图像.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