OpenCVとPythonと私 vol.4
実験用のチェスボードは
http://blog.livedoor.jp/hen_cyberne/archives/2010-08.html
こちらの勉強されている方のを使わせていただきました。
で、FindChessboardCornersとDrawChessboardCornersを使ってみました。簡単ですね、OpenCV
#!/usr/bin/python
import cv
from opencv import highgui
import sys
import getopt
filename = None
debug = False
def main () :
global filename
global debug
try:
opts, args = getopt.getopt( sys.argv[1:], "hd",["help","debug"])
except getopt.GetoptError, err :
print str( err )
usage()
sys.exit(2)
if len( args ) != 0 :
filename = args[0]
else :
usage()
sys.exit(0)
for o, a in opts :
if o in ( "-h", "--help" ) :
usage()
sys.exit(0)
elif o in ( "-d", "--debug" ) :
debug = True
else :
assert False, "unhandled option"
def debugout ( str ) :
if debug == True :
print str
def usage () :
print "Usage: detectChessboard.py [OPTION] FILE"
print " "
print " -h, --help Display this message."
print " -d, --debug Output debug log."
if __name__ == "__main__" :
main()
debugout( "Input file name is " + filename )
img = cv.LoadImage( filename , cv.CV_LOAD_IMAGE_GRAYSCALE )
output = cv.LoadImage( filename )
crnrs = cv.FindChessboardCorners( img, (6,8) )
cv.DrawChessboardCorners( output, (6,8), crnrs[1], crnrs[0] )
cv.NamedWindow( "result", cv.CV_WINDOW_AUTOSIZE )
cv.MoveWindow( "result", 100, 100)
cv.ShowImage( "result", output )
cv.SaveImage( "result.png", output )
print "Hit any key."
cv.WaitKey( 0 )
cv.DestroyWindow( "result" )