当前位置: 首页 > 工具软件 > SimpleCV > 使用案例 >

SimpleCV

长孙沈义
2023-12-01

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)



 类似资料:

相关阅读

相关文章

相关问答