使用 python3.8.x,opencv
硬币检测
问题描述设计思路1使用简单特征识别具体操作部分代码
设计思路2模板匹配源码模板制作完整代码
问题描述
使用图像处理技术,从照片中识别硬币的个数,并判断总价值。
设计思路1
使用简单特征识别
使用颜色特征,识别出5角硬币使用半径大小,判断出1角和1元硬币。
具体操作
将图片转换为HSV颜色模型
hsv = cv2.cvtColor(imgROI, cv2.COLOR_BGR2HSV)
lowerYellowHSV = np.array([11,43,46])
upperYellowHSV = np.array([34,255,255])
mask=cv2.inRange(hsv,lowerb=lowerYellowHSV,upperb=upperYellowHSV)/255
![对比](https://img-blog.csdnimg.cn/827a54b4e8e14d8d8f20ca47be05b1e1.png#pic_center)
部分代码
def coinSimpleDetection(img,circlesVector):
# 简单识别,利用硬币的色彩和半径进行区分
circlesNum = circlesVector.shape[0]
# 将图片裁剪出来
candidateImage = {}
for i in range(circlesNum):
information = {"radius":0,"value":0}
col,row,radius = circlesVector[i,:]
imgTemp = img.copy()
imgROI = imgTemp[row-radius:row+radius,col-radius:col+radius,:]
information["radius"] = radius
# 识别5角硬币
hsv = cv2.cvtColor(imgROI, cv2.COLOR_BGR2HSV)
lowerYellowHSV = np.array([11,43,46])
upperYellowHSV = np.array([34,255,255])
mask=cv2.inRange(hsv,lowerb=lowerYellowHSV,upperb=upperYellowHSV)/255
if np.sum(np.array(mask))/((row+2*radius)*(col+2*radius)) > 0.05:
information["value"] = 0.5
# 根据半径判断1块,新旧1角硬币
if information["value"] == 0:
if information["radius"] > 180 and information["radius"] 250 and information["radius"] |