반응형
Moments
윤곽선(contour)이나 이미지(array)의 0차 모멘트부터 3차 모멘트까지 계산하는 알고리즘입니다.
공간 모멘트(spatial moments), 중심 모멘트(central moments), 정규화된 중심 모멘트(normalized central moments), 질량 중심(mass center) 등을 계산할 수 있습니다.s
Moment¶
In [118]:
import cv2
import matplotlib.pyplot as plt
import numpy as np
src = cv2.imread('./image/momentTest.jpg')
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
ret, blmage = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
mode = cv2.RETR_EXTERNAL
methos = cv2.CHAIN_APPROX_SIMPLE
contours, _ = cv2.findContours(blmage, mode, methos)
dst = src.copy()
cnt = contours[0]
cv2.drawContours(dst, [cnt], 0, (255,0,0), 3)
plt.style.use("grayscale")
plt.figure(figsize=(13,8)) #width, height
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
ax1.imshow(src)
ax2.imshow(dst)
ax1.axis('off')
ax2.axis('off')
ax1.set_title("src")
ax2.set_title("cnt")
# cv2.imshow('cnt', dst)
# cv2.waitKey()
# cv2.destroyAllWindows()
# cv2.waitKey(1)
Out[118]:
Text(0.5, 1.0, 'cnt')
In [119]:
M = cv2.moments(cnt) # 모멘트 구함
hu = cv2.HuMoments(M)
print('hu.shape =', hu.shape)
print('hu=', hu)
# Humomment의 회전,사이즈 변경 invaiant 검증
angle = 45.0 #원본 영상 45도 회전
scale = 0.2 #사이즈 20%로 변경
cx = M['m10']/M['m00']
cy = M['m01']/M['m00']
center = (cx, cy)
t = (20, 30) # 이동량
A = cv2.getRotationMatrix2D(center, angle, scale)
A[:, 2] += t #translation
cnt2 = cv2.transform(cnt, A) # 변경 적용
cv2.drawContours(dst, [cnt2], 0, (0,255,0), 3)
plt.style.use("grayscale")
plt.figure(figsize=(13,8)) #width, height
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
ax1.imshow(src)
ax2.imshow(dst)
ax1.axis('off')
ax2.axis('off')
ax1.set_title("src")
ax2.set_title("cnt")
# cv2.imshow('cnt', dst)
# cv2.waitKey()
# cv2.destroyAllWindows()
# cv2.waitKey(1)
hu.shape = (7, 1)
hu= [[ 1.72272960e-01]
[ 2.17960438e-03]
[ 9.24428655e-05]
[ 1.90785217e-06]
[ 1.11977849e-12]
[-6.96325160e-09]
[-2.53121609e-11]]
Out[119]:
Text(0.5, 1.0, 'cnt')
refernce source를 이용하여 test source에서 찾아본다.¶
In [123]:
import cv2
import numpy as np
ref_src = cv2.imread('./image/refShapes.jpg')
ref_gray = cv2.cvtColor(ref_src, cv2.COLOR_BGR2GRAY)
ret, ref_bin = cv2.threshold(ref_gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
test_src = cv2.imread('./image/testShapes.jpg')
test_gray = cv2.cvtColor(test_src, cv2.COLOR_BGR2GRAY)
ret, test_bin = cv2.threshold(test_gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
mode = cv2.RETR_EXTERNAL
method = cv2.CHAIN_APPROX_SIMPLE
ref_contours, _ = cv2.findContours(ref_bin, mode, method)
test_contours, _ = cv2.findContours(test_bin, mode, method)
ref_dst = ref_src.copy()
colors = ((0,0,255), (0,255,0), (255,0,0))
for i, cnt in enumerate(ref_contours):
cv2.drawContours(ref_dst, [cnt], 0, colors[i], 2)
test_dst = test_src.copy()
method = cv2.CONTOURS_MATCH_I1
for i, cnt1 in enumerate(test_contours):
matches = []
for cnt2 in ref_contours:
ret = cv2.matchShapes(cnt1, cnt2, method, 0)
matches.append(ret)
k = np.argmin(matches)
cv2.drawContours(test_dst, [cnt1], 0, colors[k], 2)
plt.style.use("grayscale")
plt.figure(figsize=(13,8)) #width, height
ax1 = plt.subplot(1,2,1)
ax2 = plt.subplot(1,2,2)
ax1.imshow(ref_dst)
ax2.imshow(test_dst)
ax1.axis('off')
ax2.axis('off')
ax1.set_title("ref")
ax2.set_title("test")
# cv2.imshow('reference', ref_dst)
# cv2.imshow('test', test_dst)
# cv2.waitKey()
# cv2.destroyAllWindows()
# cv2.waitKey(1)
# cv2.waitKey(1)
Out[123]:
Text(0.5, 1.0, 'test')
반응형
'Python' 카테고리의 다른 글
[Python] PCA, LDA (0) | 2022.11.17 |
---|---|
[Python] openCV : Matching (0) | 2022.11.17 |
[Python] openCV : Hough Transformation (0) | 2022.11.17 |
[Python] openCV : Corner Detect (0) | 2022.11.17 |
[Python] openCV : Canny Edge Detection (0) | 2022.11.17 |