Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

KCF目标跟踪

调用TrackerKCF_create直接进行物体追踪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import cv2

class Point:
def __init__(self,x,y):
self.x=x
self.y=y
tracker = cv2.TrackerKCF_create()
vc = cv2.VideoCapture(0)
pointArr=[]
while(True):
_, img = vc.read()

painted = img.copy()
(success, box)=tracker.update(img);
if success:
(x,y,w,h) = [int(v) for v in box] #解构x y w h参数
pointArr.append(Point(x + w / 2, y + h / 2))
cv2.rectangle(painted,(x,y),(w+x,h+y),(0,0,255),3)

if len(pointArr)>0:
for myPoint in pointArr:
cv2.circle(painted,(int(myPoint.x),int(myPoint.y)),1,(0,0,255),1)

cv2.imshow("video", painted)
key = cv2.waitKey(1) & 0xFF # 取低八位,避免某些操作系统返回的不是ascii值
if key == ord("s"): # 按下s选择ROI区域
rect = cv2.selectROI("video",img,fromCenter=False,showCrosshair=True);
tracker.init(img.copy(),rect)
Usage

S键选择目标

目标测量

由于不知道比例尺(需要结合传感器得到摄像机对物体的距离),就只对最大的轮廓面积进行面积计算,此外还可以使用霍夫变换对直线进行检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import time

import cv2


class AreaInfo:
def __init__(self, area, contour):
self.area = area
self.contour = contour


def measure(srcImg):
gray = cv2.cvtColor(srcImg, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY);
blur = cv2.GaussianBlur(srcImg, (11, 11), 1)
edge = cv2.Canny(blur, 30, 200);
_, contours ,__= cv2.findContours(edge, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areaList = []

for object in contours:
areaList.append(AreaInfo(cv2.contourArea(object), object))
maxArea = AreaInfo(0, [[0, 0]])
for areaInfoObject in areaList:
maxArea = areaInfoObject if areaInfoObject.area > maxArea.area else maxArea # 迭代拿到面积最大的区域
print(maxArea.area)
cv2.drawContours(blur, maxArea.contour, -1, (100, 255, 40), 5)
cv2.imshow("edge", blur)
#cv2.waitKey(0)

apple = cv2.imread("apple.png")
measure(apple)
cv2.waitKey(0)
#cap = cv2.VideoCapture(0)
#while True:
#
# _,src = cap.read()
# measure(src)
# cv2.waitKey(200)
Usage

None

模板匹配实现的OCR

使用模板检测的方法对信用卡上的数字进行识别,由于模板匹配的拟合要求过于苛刻,常常会出现识别不出\识别不准的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cv2
import numpy as np
def templateInit():
img = cv2.imread("template.png")
img = cv2.GaussianBlur(img,(3,3),1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,binary = cv2.threshold(gray,128,255,cv2.THRESH_BINARY_INV)
_,contour,_ = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(binary,contour,-1,(0,0,255),2)
#cv2.imshow("asd",binary)
#cv2.waitKey(0)
boundingBoxes = [cv2.boundingRect(c) for c in contour]
contour,_ = zip(*sorted(zip(contour, boundingBoxes),key=lambda b: b[1][0], reverse=False)) # 从左到右排序
list = []
for (i) in contour:
(x,y,w,h)= cv2.boundingRect(i)
#print(x,y,w,h)
cv2.rectangle(img,(x,y),(w+x,h+y),(0,0,255),1)
ROI = binary[y:y + h, x:x + w]
ROI=cv2.resize(ROI.copy(),(57,88))
list.append(ROI)
#cv2.imshow("感兴趣区域",ROI);
#cv2.waitKey(0)
#list.append()


# cv2.imshow("asd", img)
# cv2.waitKey(0)

return binary,contour,list


binary,templateContours,list = templateInit()
creditCard = cv2.imread("CreditCard.png")
img = cv2.GaussianBlur(creditCard, (3, 3), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#_, binary = cv2.threshold(gray, 130, 255, cv2.THRESH_BINARY)
#topHat = cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,np.ones((8,8),np.uint8)) # 使用礼帽提取图像中比较亮的部分
_,creditBinay = cv2.threshold(gray.copy(),128,255,cv2.THRESH_BINARY_INV)
_, creditContour, _ = cv2.findContours(creditBinay, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(creditBinay, creditContour, -1, (0, 0, 255), 2)
#cv2.imshow("asd",creditBinay)
#cv2.waitKey(0)

boundingBoxes = [cv2.boundingRect(c) for c in creditContour]
creditContour, _ = zip(*sorted(zip(creditContour, boundingBoxes), key=lambda b: b[1][0], reverse=False)) # 从左到右排序
#cv2.imshow("a",gray);
#cv2.waitKey(0)


numList=[]
filtedContour=[]
for i in creditContour:
(x, y, w, h) = cv2.boundingRect(i)
if (0.58 < w / h < 0.70) and (31<x<422) and (145<y<201):
filtedContour.append(i)
cv2.drawContours(creditCard,filtedContour,-1,(0,0,255),2)
for i in filtedContour: # 先遍历所有轮廓,找出候选的ROI区域
(x, y, w, h) = cv2.boundingRect(i)
cv2.rectangle(img, (x, y), (w + x, h + y), (0, 0, 255), 1)
ROI = gray[y:y + h, x:x + w]
_,ROI = cv2.threshold(ROI.copy(),128,255,cv2.THRESH_BINARY)
#kernel = np.zeros((5, 5), np.uint8) # 新建一个核
#ROI = cv2.erode(ROI.copy(), kernel, iterations=3)
ROI = cv2.resize(ROI.copy(), (57, 88))
maxList = [] # 存放十种模板的匹配度
for templateROI in list:
result = cv2.matchTemplate(ROI,templateROI,cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
#cv2.rectangle(creditCard,min_loc,max_loc,(0,255,255),1)
maxList.append(max_val)
maxValue = 0
Index = 0
for i in range(10):
if maxList[i] > maxValue:
maxValue = maxList[i] # 找出最大的可能性,maxList的index恰好为数字
Index = i
if not (maxValue == 0):
cv2.putText(creditCard,str(Index),(x,y-10),1,1,(0,0,255))

numList.append(Index)
#print(Index)

cv2.imshow("asd",creditCard)
cv2.waitKey(0)
print(numList)


# cv2.matchTemplate(creditCard)
#cv2.matchTemplate()
Usage

template.png 为模板0~9

CreditCard.png 为信用卡图片

ocr

评论