Python + OpenCV에서 HSV 히스토그램 정보로 패턴 매칭
패턴계의 패턴의 매칭에서는 상당히 사용할 수 있는 인상
실시 결과
일러스트의 책상 패턴의 패턴을 바탕으로 책상의 개소를 특정해 보았다
1. 인식 대상 이미지 준비
인식 대상용 이미지를 준비

2. 책상 무늬 패턴 이미지 준비
위의 이미지에서 책상 부분 이미지를 잘라냅니다.

3. 책상 부분을 확인
아래와 같이 책상의 개소만이 마킹된다(밝은 빨강의 개소가 가장 책상의 정확도가 높은 개소)

소스 코드
상기 실시시의 소스 코드입니다
test.py
import cv2
import os
# 対象画像読み込み
img = cv2.imread("targetImg.png",cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 画像情報(幅,高さ,チャンネル数,depth)を取得
height, width, channels = img.shape[:3]
# 机模様のテンプレート画像読み込み
imgTmp = cv2.imread("imgTmp.png",cv2.IMREAD_COLOR)
heightTmp, widthTmp, channelsTmp = imgTmp.shape[:3]
hsvTmp = cv2.cvtColor(imgTmp, cv2.COLOR_BGR2HSV)
# マーキング画像生成
imgMark = img
ch_names = {0: 'Hue', 1: 'Saturation', 2: 'Brightness'}
# 対象領域を順に切り出し確認
for x in range(0,int(width / widthTmp)):
for y in range(0,int(height / heightTmp)):
# 対象領域を切り出し
hsvClp = hsv[int(y * heightTmp):int((y + 1) * heightTmp),int(x * widthTmp):int((x + 1) * widthTmp)]
# 各チャンネルごとにヒストグラムの類似度を算出する。
scores, hists1, hists2 = [], [], []
for ch in ch_names:
h1 = cv2.calcHist([hsvTmp], [ch], None, histSize=[256], ranges=[0, 256])
h2 = cv2.calcHist([hsvClp], [ch], None, histSize=[256], ranges=[0, 256])
# 類似度を算出
score = cv2.compareHist(h1, h2, cv2.HISTCMP_CORREL)
hists1.append(h1)
hists2.append(h2)
scores.append(score)
mean = np.mean(scores)
# 一定閾値の箇所のみマーキング
if mean > 0.20 :
cv2.rectangle(imgMark, (int(x * widthTmp), int(y * heightTmp)), (int((x + 1) * widthTmp), int((y + 1) * heightTmp)), (0, 0, int(mean * 500)), 2)
cv2.imwrite("markImg.png", imgMark)
참고문헌
상기 실시시의 소스 코드입니다
test.py
import cv2
import os
# 対象画像読み込み
img = cv2.imread("targetImg.png",cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 画像情報(幅,高さ,チャンネル数,depth)を取得
height, width, channels = img.shape[:3]
# 机模様のテンプレート画像読み込み
imgTmp = cv2.imread("imgTmp.png",cv2.IMREAD_COLOR)
heightTmp, widthTmp, channelsTmp = imgTmp.shape[:3]
hsvTmp = cv2.cvtColor(imgTmp, cv2.COLOR_BGR2HSV)
# マーキング画像生成
imgMark = img
ch_names = {0: 'Hue', 1: 'Saturation', 2: 'Brightness'}
# 対象領域を順に切り出し確認
for x in range(0,int(width / widthTmp)):
for y in range(0,int(height / heightTmp)):
# 対象領域を切り出し
hsvClp = hsv[int(y * heightTmp):int((y + 1) * heightTmp),int(x * widthTmp):int((x + 1) * widthTmp)]
# 各チャンネルごとにヒストグラムの類似度を算出する。
scores, hists1, hists2 = [], [], []
for ch in ch_names:
h1 = cv2.calcHist([hsvTmp], [ch], None, histSize=[256], ranges=[0, 256])
h2 = cv2.calcHist([hsvClp], [ch], None, histSize=[256], ranges=[0, 256])
# 類似度を算出
score = cv2.compareHist(h1, h2, cv2.HISTCMP_CORREL)
hists1.append(h1)
hists2.append(h2)
scores.append(score)
mean = np.mean(scores)
# 一定閾値の箇所のみマーキング
if mean > 0.20 :
cv2.rectangle(imgMark, (int(x * widthTmp), int(y * heightTmp)), (int((x + 1) * widthTmp), int((y + 1) * heightTmp)), (0, 0, int(mean * 500)), 2)
cv2.imwrite("markImg.png", imgMark)
참고문헌
Reference
이 문제에 관하여(Python + OpenCV에서 HSV 히스토그램 정보로 패턴 매칭), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Zumwalt/items/979d450ca43b81b35424텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)