SimpleCV 其实是OPENCV 的python精简版本.
用了这么久的SimpleCV发现有个方法挺好用,template matching 模块
一种是:from SimpleCV import * source = Image("templatetest.png", sample=True) # the image to search template = Image("template.png", sample=True) # the template to search the image for t = 5 methods = ["SQR_DIFF","SQR_DIFF_NORM","CCOEFF","CCOEFF_NORM","CCORR","CCORR_NORM"] # the various types of template matching available for m in methods: print "current method:", m # print the method being used result = Image("templatetest.png", sample=True) fs = source.findTemplate(template,threshold=t,method=m) for match in fs: dl.rectangle((match.x,match.y),(match.width(),match.height()),color=Color.RED)
另一种是:
'''This example finds a quarter in the picture and then uses that measurementto determine the rest of the coins in the picture. Since a quarter is alwaysa certain size we can use it as a reference because it is known.
In this example we use millimeters to pixels to do the conversion.
The sizes of coins are as follows:penny - 19.05 mmnickel - 21.21 mmdime - 17.9 mmquarter - 24.26 mm
'''
print __doc__
from SimpleCV import *# A quarter is 24.26mm or 0.955inquarterSize = 24.26 #millimeters
# This will hold the ratio of the image size to a quarter's actual sizesizeRatio = 0objectSize = 0
img = Image ( 'coins.jpg' , sample = True )segmented = img . hueDistance ( Color . BLACK )coins = img . invert () . findBlobs ( minsize = 200 )
#Here we compute the scale factorif coins :c = coins [ - 1 ]diameter = c . radius () * 2sizeRatio = quarterSize / diameter
#Now we print the measurements back on the picturefor coin in coins :#get the physical size of the coinsize = ( coin . radius () * 2 ) * sizeRatio#label the coin accordinglyif size > 18 and size < 20 :text = "Type: penny"elif size > 20 and size < 23 :text = "Type: nickel"elif size > 16 and size < 18 :text = "Type: dime"elif size > 23 and size < 26 :text = "Type: quarter"else :text = "unknown"
text = text + ", Size:" + str ( size ) + "mm"img . drawText ( text , coin . x , coin . y )
img . show ()time . sleep ( 10)