簡體   English   中英

如何使用opencv在python中計算不對稱形狀區域

[英]how to calculate Asymmetric shapes area in python with opencv

我需要計算不對稱形狀的面積,如下圖所示

在此處輸入圖片說明

這段代碼讀取圖像並將其轉換為灰色,並找到銳利度,我需要找出不對稱形狀的區域

import numpy as np
import cv2

# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("download.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)


# find contours in the edge map
cnts = cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(image,cnts, 0, (255, 0, 0), 8)
cv2.imshow("Image", image)
cv2.waitKey(0)

我已經測試了您的代碼,發現沒有找到形狀的輪廓。 這是因為您在灰色圖像中執行了cv2.findContours 該圖像應該是二進制圖像,因此我使用了cv2.threshold 然后可以使用cv2.contourArea計算cv2.contourArea

下面是代碼和結果。

import numpy as np
import cv2

image = cv2.imread("1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

_,thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY_INV)

im, cnts, hier = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

for cnt in cnts:
    cv2.drawContours(image,cnts, -1, (0, 0, 255), 1)
    print(cv2.contourArea(cnt))


cv2.imshow("thresh", thresh)
cv2.imshow("Image", image)
cv2.waitKey(0)

>> 8656.0
>> 3824.5

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM