如何在OpenCV Python中計算Hu矩?
Hu矩可以使用cv2.HuMoments()函式計算。它返回七個對平移、旋轉和縮放不變的矩。第七矩是對傾斜不變的。
要計算Hu矩,我們首先需要找到影像。影像矩是使用物件的輪廓為物件計算的。因此,首先,我們檢測物件的輪廓,然後應用cv2.moments()函式計算矩。
語法
此函式使用以下語法:
M = cv2.moments(cnt) cv2.HuMoments(M)
這裡:
cnt - 它是影像中物件輪廓點的NumPy陣列。
M - 上面計算的影像矩。
步驟
您可以使用以下步驟來計算影像中的Hu矩:
匯入所需的庫。在以下所有Python示例中,所需的Python庫是OpenCV。確保您已安裝它。
import cv2
使用cv2.imread()讀取輸入影像並將其轉換為灰度影像。
img = cv2.imread('shape.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
對灰度影像應用閾值處理以建立二值影像。
ret,thresh = cv2.threshold(gray,150,255,0)
使用cv2.findContours()函式查詢影像中的輪廓。
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
使用cv2.moments(cnt)函式查詢輪廓的矩。
cnt = contours[0] M = cv2.moments(cnt)
對於特定輪廓,使用cv2.HuMoments(M)函式查詢Hu矩。
Hm = cv2.HuMoments(M)
在輸入影像上繪製輪廓。
cv2.drawContours(img, [cnt], -1, (0,255,255), 3)
列印Hu矩並顯示帶有已繪製輪廓的影像。
print("Hu-Moments of first contour:\n", Hm)
cv2.imshow(Hu-Moments", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
讓我們看一些例子以便更好地理解。
我們將在下面的示例中使用以下影像作為輸入檔案。

示例1
在下面的Python程式中,我們檢測影像中的輪廓並找到第一個輪廓的Hu矩。我們還在影像上繪製第一個輪廓。
# import the required libraries import cv2 # Read the input image img = cv2.imread('shape.png') # Convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding on gray image ret,thresh = cv2.threshold(gray,150,255,0) # Find the contours in the image contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of Contours detected:",len(contours)) # Find the moments of first contour cnt = contours[0] M = cv2.moments(cnt) Hm = cv2.HuMoments(M) # Draw the contour cv2.drawContours(img, [cnt], -1, (0,255,255), 3) x1, y1 = cnt[0,0] cv2.putText(img, 'Contour:1', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # print the moments of the first contour print("Hu-Moments of first contour:\n", Hm) cv2.imshow("Hu-Moments", img) cv2.waitKey(0) cv2.destroyAllWindows()
輸出
執行上述程式碼時,將產生以下輸出:
Number of Contours detected: 3 Hu-Moments of first contour: [[ 1.59307685e-01] [ 4.69721864e-05] [ 1.89651880e-10] [ 8.95011994e-14] [ 2.03401550e-25] [ 4.24017740e-16] [-3.07567885e-25]]
我們將得到以下輸出視窗,顯示影像中第一個檢測到的輪廓:

示例2
在下面的Python程式中,我們檢測影像中的輪廓並找到所有輪廓的Hu矩。我們還在影像上繪製所有輪廓。
import cv2 import numpy as np img = cv2.imread('shape.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,170,255,0) contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:",len(contours)) # compute HuMoments for all the contours detected in the image for i, cnt in enumerate(contours): x,y = cnt[0,0] moments = cv2.moments(cnt) hm = cv2.HuMoments(moments) cv2.drawContours(img, [cnt], -1, (0,255,255), 3) cv2.putText(img, f'Contour {i+1}', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2) print(f"\nHuMoments for Contour {i+1}:\n", hm) cv2.imshow("Hu-Moments", img) cv2.waitKey(0) cv2.destroyAllWindows()
輸出
執行上述程式碼時,將產生以下輸出:
Number of contours detected: 3 HuMoments for Contour 1: [[ 1.59307685e-01] [ 4.69721864e-05] [ 1.89651880e-10] [ 8.95011994e-14] [ 2.03401550e-25] [ 4.24017740e-16] [-3.07567885e-25]] HuMoments for Contour 2: [[ 1.67576439e-01] [ 3.03541843e-04] [ 2.96933966e-10] [ 8.49276231e-12] [-3.47095391e-22] [ 2.67821989e-14] [ 2.47818355e-22]] HuMoments for Contour 3: [[2.29748674e-01] [1.57527336e-02] [5.92081089e-03] [4.77994195e-04] [4.24175667e-07] [1.65030169e-05] [6.83150707e-07]]
我們將得到以下輸出視窗,顯示影像中檢測到的輪廓:

廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP