卡尔曼滤波实例——预测橘子的轨迹

94 篇文章 9 订阅

（一）滚动条获取阈值

（二）获取到图像中的包围橘子对应的白色图形的最小矩形框的信息

step1:获取橘子的检测框

step2:求取橘子的质心

step3:将质心送入卡尔曼滤波器，获取到预测的下一次橘子的质心位置

（一）滚动条获取阈值

import cv2
import numpy as np

def nothing(x):
pass

cv2.namedWindow('image')
cv2.createTrackbar('a','image',0,255,nothing)
cv2.createTrackbar('b','image',0,255,nothing)
cv2.createTrackbar('c','image',0,255,nothing)
cv2.createTrackbar('d','image',0,255,nothing)
cv2.createTrackbar('e','image',0,255,nothing)
cv2.createTrackbar('f','image',0,255,nothing)

frame = cv2.resize(frame,(700,400))

while True:
a = cv2.getTrackbarPos('a', 'image')
b = cv2.getTrackbarPos('b', 'image')
c = cv2.getTrackbarPos('c', 'image')
d = cv2.getTrackbarPos('d', 'image')
e = cv2.getTrackbarPos('e', 'image')
f = cv2.getTrackbarPos('f', 'image')
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
low_orange = np.array([a, b, c])
high_orange = np.array([d, e, f])
k = cv2.waitKey(1)&0xff
if k==27:
break

（二）获取到图像中的包围橘子对应的白色图形的最小矩形框的信息

import cv2
import numpy as np

class OrangeDetector:
def __init__(self):
self.low_orange = np.array([10, 152, 89])
self.high_orange = np.array([180, 255, 255])

def detect(self, frame):
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
box = (0, 0, 0, 0)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
box = (x, y, x + w, y + h)
break
return box

od = OrangeDetector()
orange_bbox = od.detect(frame)
x, y, x2, y2 = orange_bbox
cx = int((x + x2) / 2)
cy = int((y + y2) / 2)

predicted = kf.predict(cx, cy)

import cv2
from orange_detector import OrangeDetector
from kalmanfilter import KalmanFilter

cap = cv2.VideoCapture("orange.mp4")
od = OrangeDetector()
kf = KalmanFilter()

while True:
if ret is False:
break

orange_bbox = od.detect(frame)
x, y, x2, y2 = orange_bbox
cx = int((x + x2) / 2)
cy = int((y + y2) / 2)

predicted = kf.predict(cx, cy)
cv2.circle(frame, (cx, cy), 20, (0, 0, 255), 4)
cv2.circle(frame, (predicted[0], predicted[1]), 20, (255, 0, 0), 4)

cv2.imshow("Frame", frame)
key = cv2.waitKey(10)
if key == 27:
break

• 1
点赞
• 4
收藏
• 3
评论
08-16 9797
07-25 2796
12-17 190
09-19
12-29 1216
02-22 116
09-13 7674
09-03 1万+
10-03 812
04-11 3万+
08-26 1216

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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