Python 求(不規則)多邊形的面積 通用辦法(已知多邊形頂點的座標)
阿新 • • 發佈:2018-12-10
# -*- coding: UTF-8 -*- import cv2 import numpy as np image = cv2.imread('img0.jpg') # (這裡讀入的圖的尺寸要大於你的多邊形) polygon = np.array([[[2, 2], [6, 2], [6, 6], [2, 6]]], dtype=np.int32) # 這裡是多邊形的頂點座標 im = np.zeros(image.shape[:2], dtype="uint8") # 獲取影象的維度: (h,w)=iamge.shape[:2] polygon_mask = cv2.fillPoly(im, polygon, 255) area = np.sum(np.greater(polygon_mask, 0)) print (area)
結果 表示 面積為25,實際這裡我們的多邊形就是拿正方形來舉例的
C:\Users\JulianYang\Anaconda2\python.exe C:/helloWorld/python/test6.py 25 Process finished with exit code 0