SimpleCV

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 measurement
to determine the rest of the coins in the picture. Since a quarter is always
a 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 mm
nickel - 21.21 mm
dime - 17.9 mm
quarter - 24.26 mm

'''

print __doc__

from SimpleCV import *
# A quarter is 24.26mm or 0.955in
quarterSize = 24.26 #millimeters

# This will hold the ratio of the image size to a quarter's actual size
sizeRatio = 0
objectSize = 0

img = Image ( 'coins.jpg' , sample = True )
segmented = img . hueDistance ( Color . BLACK )
coins = img . invert () . findBlobs ( minsize = 200 )

#Here we compute the scale factor
if coins :
c = coins [ - 1 ]
diameter = c . radius () * 2
sizeRatio = quarterSize / diameter

#Now we print the measurements back on the picture
for coin in coins :
#get the physical size of the coin
size = ( coin . radius () * 2 ) * sizeRatio
#label the coin accordingly
if 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)



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值