Pythonはじめました。

はじめに

プライベートでPythonを始めました。
仕事などで必要になったわけでもないので、C/C++なみのことができるようになればOKというザックリした目標です。
文字列処理やネットワーク、データベース、GUI、、色々な分野がありますが、遊んでて楽しいのはOpenCVKinectなので、そこらへんから着手しようと思い、OpenCVのプログラムをつくり始めました。
手始めに、何か動かしたいと思い、特徴点でマッチングするプログラムを作ります。

動かした

詠み人知らずの備忘録さんのプログラムを改造して、WEBカメラから使えるようにしました。
特徴点はSURF以外も使えるようにしています。

プログラム

ちょっと読みにくいかもしれませんが初心者ということでご容赦ください。
>|python||

#/usr/bin/env python
# TEST 2014.03.21
# ref
# http://authorunknown408.blog.fc2.com/blog-entry-34.html
#

import cv2
import scipy as sp

##------ MAIN PROCEDURE -----
if __name__ == "__main__":
# 2014.03.21 ORB+BRISK is BEST!

# SURF / SIFT / FAST / STAR / Dense / ORB
detectType = "ORB"
detector = cv2.FeatureDetector_create(detectType)
# BRIEF / BRISK / FREAK
descType = "BRISK"
descriptor = cv2.DescriptorExtractor_create(descType)
#------------ INIT TEMPLATE DATA -----------
# load template image
templateImageFilePath = "C:\OpenCV.2.4.4\samples\c\\box.png"
templ = cv2.imread(templateImageFilePath)
templateImg = cv2.cvtColor(templ,cv2.COLOR_BGR2GRAY)
kpTempl = detector.detect(templateImg)
templKeypoints, templDescriptors = descriptor.compute(templateImg, kpTempl)
print ' [TEMPL] kp_num=%d / dsc_num=%d < [%s]' % (len(templKeypoints), len(templDescriptors), templateImageFilePath)
for kpt in templKeypoints:
ct = ( int(kpt.pt[0]), int(kpt.pt[1]))
cv2.circle(templ, ct, int(kpt.size/5), (255,0,0))
cv2.imshow('TEMPLATE',templ)

matcher = cv2.DescriptorMatcher_create("BruteForce-Hamming")

# ------ PREPARE CAMERA --------
capture=cv2.VideoCapture(0)
isopen=capture.isOpened()
if(isopen == False):
print 'ERR: failed to VideoCapture()'
exit()

#------ MAIN ROUTIN start ------
while True:
# CAPTURE (+RESIZE)
ret, frame = capture.read()
if not ret:
print 'ERR: Failed to get capture! ret=%d' % ret
break
capImg = frame
# You have to Use 'capImg' NOT 'frame'.

# extract Features
dbgImg = capImg;
grayImg = cv2.cvtColor(capImg,cv2.COLOR_BGR2GRAY)
capTempl = detector.detect(grayImg)
capKeypoints, capDescriptors = descriptor.compute(grayImg, capTempl)
for kpt in capKeypoints:
ct = ( int(kpt.pt[0]), int(kpt.pt[1]))
cv2.circle(dbgImg, ct, int(kpt.size/5), (255,0,0))
cv2.imshow('IMAGE',dbgImg)

# match Capture and Template
matches = matcher.match(templDescriptors, capDescriptors)
#print '#matches:', len(matches)
dist = [m.distance for m in matches]
thres_dist = (sum(dist) / len(dist)) * 0.5 # threshold: half the mean
sel_matches = [m for m in matches if m.distance < thres_dist] # keep only the reasonable matches
print '#selected matches:', len(sel_matches)

# Visualize
h1, w1 = templateImg.shape[:2]
h2, w2 = grayImg.shape[:2]
viewImg = sp.zeros*1
pt2 = (int(capKeypoints[m.trainIdx].pt[0] + w1), int(capKeypoints[m.trainIdx].pt[1]))
cv2.line(viewImg, pt1, pt2, color)
windowName = "MATCHING: " + detectType + " / " + descType
cv2.imshow(windowName,viewImg)

# HANDLE INPUT
inputkey=cv2.waitKey(30)
if inputkey > 10:
break
#------ MAIN ROUTIN end ------
cv2.destroyAllWindows()
|

環境

Windows 8.1
OpenCV 2.4.4
Python 2.7

*1:max(h1, h2), w1 + w2, 3), sp.uint8) viewImg[:h1, :w1, 0] =templateImg viewImg[:h2, w1:, 0] = grayImg viewImg[:, :, 1] = viewImg[:, :, 0] viewImg[:, :, 2] = viewImg[:, :, 0] for m in sel_matches: # draw the keypoints color = tuple([sp.random.randint(0, 255) for _ in xrange(3)]) pt1 = (int(templKeypoints[m.queryIdx].pt[0]),int(templKeypoints[m.queryIdx].pt[1]