簡體   English   中英

如何使用opencv-python識別圖像的形狀是對稱的還是非對稱的?

[英]How to identify if the shapes of an image are symmetric or asymmetric using opencv - python?

我正在研究圖像特征的提取,其中我試圖識別某個圖像是否對稱。 我正在使用opecv - python來開發這項工作。

下面的代碼用於識別感興趣區域的中心和直徑。 你怎么知道這個圖像是否對稱?

import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText

IMG = '015'
thresh = cv2.imread(IMD+'.png',0)
_, contours,hierarchy = cv2.findContours(thresh,2,1)
print (len(contours))
cnt = contours

for i in range (len(cnt)):
    (x,y),radius = cv2.minEnclosingCircle(cnt[i])
    center = (int(x),int(y))
    radius = int(radius)
    cv2.circle(thresh,center,radius,(0,255,0),2)
    print ('Circle: ' + str(i) + ' - Center: ' + str(center) + ' - Radius: ' + str(radius))
plt.text(x-21, y+15, '+', fontsize=25, color = 'red')
plt.text(10, -10, 'Centro: '+str(center), fontsize=11, color = 'red')
plt.text(340, -10, 'Diametro: '+str((radius*2)/100)+'mm', fontsize=11,     color = 'red')
plt.Circle((10, -10), 7.2, color='blue')
plt.imshow(thresh, cmap='gray')
#plt.savefig(IMG+'-diam.png')
plt.show()

出口: 在此輸入圖像描述

在這種情況下,我想要分類,如果我正在分析的點是對稱的,下面的圖像,在視覺上注意到它不是對稱的,而上面的圖的第一個圖像是對稱的。

在此輸入圖像描述

我假設變量thresh是二進制圖像。

為了找到非均勻物體的對稱性,我建議我們比較X和Y軸上二進制像素的投影。 在此輸入圖像描述

然后通過直方圖比較方法比較2直方圖,如相關性,卡方或Bhattacharyya距離。 (openCV中的示例: https ://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html)

G_X = cv2.reduce(thresh_square, 0 ,cv2.REDUCE_SUM)
G_Y = cv2.reduce(thresh_square, 1 ,cv2.REDUCE_SUM)

compare_val = cv2.compareHist(G_X ,G_Y ,cv2.HISTCMP_CORREL)

其中thresh_square是以二進制blob為中心的平方ROI。 您需要為G_X和G_Y設置相同的容器才能進行有意義的比較。

較高的相關值應對應於對稱對象,而較低的相關值將對應於不對稱的對象。

將此代碼運行到一些對稱和非對稱示例中,並檢查compare_val值。 您應該能夠找到將兩者分開的閾值。

以下是我將如何解決這個問題:

  1. 測量每個半徑距離中心的距離
  2. 將測量分為兩組(0到180,180到360)
  3. 獲取兩組的平均值並進行比較,看它們是否在誤差范圍內相等。
  4. 將組的分割旋轉1度,然后再次嘗試直到達到179度
  5. 檢查返回的任何拆分是否在保證金范圍內相等。

您可能需要調整相等邊距一段時間才能找到可接受准確的范圍。

此外,您可能必須定期檢查旋轉以查看x旋轉在邊距內是否相等然后它是對稱的。

您可能還需要將其划分為象限而不是一半,等待您要檢查對稱的軸數。

暫無
暫無

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

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