mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
Merge branch 4.x
This commit is contained in:
@@ -188,7 +188,7 @@ def main():
|
||||
|
||||
fig = plt.figure()
|
||||
ax = fig.gca(projection='3d')
|
||||
ax.set_aspect("equal")
|
||||
ax.set_aspect("auto")
|
||||
|
||||
cam_width = args.cam_width
|
||||
cam_height = args.cam_height
|
||||
|
||||
@@ -28,11 +28,11 @@ def make_gaussians(cluster_n, img_size):
|
||||
return points, ref_distrs
|
||||
|
||||
def draw_gaussain(img, mean, cov, color):
|
||||
x, y = np.int32(mean)
|
||||
x, y = mean
|
||||
w, u, _vt = cv.SVDecomp(cov)
|
||||
ang = np.arctan2(u[1, 0], u[0, 0])*(180/np.pi)
|
||||
s1, s2 = np.sqrt(w)*3.0
|
||||
cv.ellipse(img, (x, y), (s1, s2), ang, 0, 360, color, 1, cv.LINE_AA)
|
||||
cv.ellipse(img, (int(x), int(y)), (int(s1), int(s2)), ang, 0, 360, color, 1, cv.LINE_AA)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -46,9 +46,9 @@ def hist_lines(im):
|
||||
im = cv.cvtColor(im,cv.COLOR_BGR2GRAY)
|
||||
hist_item = cv.calcHist([im],[0],None,[256],[0,256])
|
||||
cv.normalize(hist_item,hist_item,0,255,cv.NORM_MINMAX)
|
||||
hist=np.int32(np.around(hist_item))
|
||||
hist = np.int32(np.around(hist_item))
|
||||
for x,y in enumerate(hist):
|
||||
cv.line(h,(x,0),(x,y),(255,255,255))
|
||||
cv.line(h,(x,0),(x,y[0]),(255,255,255))
|
||||
y = np.flipud(h)
|
||||
return y
|
||||
|
||||
|
||||
+49
-48
@@ -1,14 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Tracking of rotating point.
|
||||
Rotation speed is constant.
|
||||
Point moves in a circle and is characterized by a 1D state.
|
||||
state_k+1 = state_k + speed + process_noise N(0, 1e-5)
|
||||
The speed is constant.
|
||||
Both state and measurements vectors are 1D (a point angle),
|
||||
Measurement is the real point angle + gaussian noise.
|
||||
The real and the estimated points are connected with yellow line segment,
|
||||
the real and the measured points are connected with red line segment.
|
||||
Measurement is the real state + gaussian noise N(0, 1e-1).
|
||||
The real and the measured points are connected with red line segment,
|
||||
the real and the estimated points are connected with yellow line segment,
|
||||
the real and the corrected estimated points are connected with green line segment.
|
||||
(if Kalman filter works correctly,
|
||||
the yellow segment should be shorter than the red one).
|
||||
Pressing any key (except ESC) will reset the tracking with a different speed.
|
||||
the yellow segment should be shorter than the red one and
|
||||
the green segment should be shorter than the yellow one).
|
||||
Pressing any key (except ESC) will reset the tracking.
|
||||
Pressing ESC will stop the program.
|
||||
"""
|
||||
# Python 2/3 compatibility
|
||||
@@ -21,8 +25,7 @@ if PY3:
|
||||
import numpy as np
|
||||
import cv2 as cv
|
||||
|
||||
from math import cos, sin, sqrt
|
||||
import numpy as np
|
||||
from math import cos, sin, sqrt, pi
|
||||
|
||||
def main():
|
||||
img_height = 500
|
||||
@@ -30,64 +33,62 @@ def main():
|
||||
kalman = cv.KalmanFilter(2, 1, 0)
|
||||
|
||||
code = long(-1)
|
||||
|
||||
cv.namedWindow("Kalman")
|
||||
|
||||
num_circle_steps = 12
|
||||
while True:
|
||||
state = 0.1 * np.random.randn(2, 1)
|
||||
|
||||
kalman.transitionMatrix = np.array([[1., 1.], [0., 1.]])
|
||||
kalman.measurementMatrix = 1. * np.ones((1, 2))
|
||||
kalman.processNoiseCov = 1e-5 * np.eye(2)
|
||||
kalman.measurementNoiseCov = 1e-1 * np.ones((1, 1))
|
||||
kalman.errorCovPost = 1. * np.ones((2, 2))
|
||||
kalman.statePost = 0.1 * np.random.randn(2, 1)
|
||||
img = np.zeros((img_height, img_width, 3), np.uint8)
|
||||
state = np.array([[0.0],[(2 * pi) / num_circle_steps]]) # start state
|
||||
kalman.transitionMatrix = np.array([[1., 1.], [0., 1.]]) # F. input
|
||||
kalman.measurementMatrix = 1. * np.eye(1, 2) # H. input
|
||||
kalman.processNoiseCov = 1e-5 * np.eye(2) # Q. input
|
||||
kalman.measurementNoiseCov = 1e-1 * np.ones((1, 1)) # R. input
|
||||
kalman.errorCovPost = 1. * np.eye(2, 2) # P._k|k KF state var
|
||||
kalman.statePost = 0.1 * np.random.randn(2, 1) # x^_k|k KF state var
|
||||
|
||||
while True:
|
||||
def calc_point(angle):
|
||||
return (np.around(img_width/2 + img_width/3*cos(angle), 0).astype(int),
|
||||
np.around(img_height/2 - img_width/3*sin(angle), 1).astype(int))
|
||||
|
||||
return (np.around(img_width / 2. + img_width / 3.0 * cos(angle), 0).astype(int),
|
||||
np.around(img_height / 2. - img_width / 3.0 * sin(angle), 1).astype(int))
|
||||
img = img * 1e-3
|
||||
state_angle = state[0, 0]
|
||||
state_pt = calc_point(state_angle)
|
||||
|
||||
# advance Kalman filter to next timestep
|
||||
# updates statePre, statePost, errorCovPre, errorCovPost
|
||||
# k-> k+1, x'(k) = A*x(k)
|
||||
# P'(k) = temp1*At + Q
|
||||
prediction = kalman.predict()
|
||||
predict_angle = prediction[0, 0]
|
||||
predict_pt = calc_point(predict_angle)
|
||||
|
||||
measurement = kalman.measurementNoiseCov * np.random.randn(1, 1)
|
||||
|
||||
predict_pt = calc_point(prediction[0, 0]) # equivalent to calc_point(kalman.statePre[0,0])
|
||||
# generate measurement
|
||||
measurement = kalman.measurementNoiseCov * np.random.randn(1, 1)
|
||||
measurement = np.dot(kalman.measurementMatrix, state) + measurement
|
||||
|
||||
measurement_angle = measurement[0, 0]
|
||||
measurement_pt = calc_point(measurement_angle)
|
||||
|
||||
# plot points
|
||||
def draw_cross(center, color, d):
|
||||
cv.line(img,
|
||||
(center[0] - d, center[1] - d), (center[0] + d, center[1] + d),
|
||||
color, 1, cv.LINE_AA, 0)
|
||||
cv.line(img,
|
||||
(center[0] + d, center[1] - d), (center[0] - d, center[1] + d),
|
||||
color, 1, cv.LINE_AA, 0)
|
||||
|
||||
img = np.zeros((img_height, img_width, 3), np.uint8)
|
||||
draw_cross(np.int32(state_pt), (255, 255, 255), 3)
|
||||
draw_cross(np.int32(measurement_pt), (0, 0, 255), 3)
|
||||
draw_cross(np.int32(predict_pt), (0, 255, 0), 3)
|
||||
|
||||
cv.line(img, state_pt, measurement_pt, (0, 0, 255), 3, cv.LINE_AA, 0)
|
||||
cv.line(img, state_pt, predict_pt, (0, 255, 255), 3, cv.LINE_AA, 0)
|
||||
|
||||
# correct the state estimates based on measurements
|
||||
# updates statePost & errorCovPost
|
||||
kalman.correct(measurement)
|
||||
improved_pt = calc_point(kalman.statePost[0, 0])
|
||||
|
||||
process_noise = sqrt(kalman.processNoiseCov[0,0]) * np.random.randn(2, 1)
|
||||
state = np.dot(kalman.transitionMatrix, state) + process_noise
|
||||
# plot points
|
||||
cv.drawMarker(img, measurement_pt, (0, 0, 255), cv.MARKER_SQUARE, 5, 2)
|
||||
cv.drawMarker(img, predict_pt, (0, 255, 255), cv.MARKER_SQUARE, 5, 2)
|
||||
cv.drawMarker(img, improved_pt, (0, 255, 0), cv.MARKER_SQUARE, 5, 2)
|
||||
cv.drawMarker(img, state_pt, (255, 255, 255), cv.MARKER_STAR, 10, 1)
|
||||
# forecast one step
|
||||
cv.drawMarker(img, calc_point(np.dot(kalman.transitionMatrix, kalman.statePost)[0, 0]),
|
||||
(255, 255, 0), cv.MARKER_SQUARE, 12, 1)
|
||||
|
||||
cv.line(img, state_pt, measurement_pt, (0, 0, 255), 1, cv.LINE_AA, 0) # red measurement error
|
||||
cv.line(img, state_pt, predict_pt, (0, 255, 255), 1, cv.LINE_AA, 0) # yellow pre-meas error
|
||||
cv.line(img, state_pt, improved_pt, (0, 255, 0), 1, cv.LINE_AA, 0) # green post-meas error
|
||||
|
||||
# update the real process
|
||||
process_noise = sqrt(kalman.processNoiseCov[0, 0]) * np.random.randn(2, 1)
|
||||
state = np.dot(kalman.transitionMatrix, state) + process_noise # x_k+1 = F x_k + w_k
|
||||
|
||||
cv.imshow("Kalman", img)
|
||||
|
||||
code = cv.waitKey(100)
|
||||
code = cv.waitKey(1000)
|
||||
if code != -1:
|
||||
break
|
||||
|
||||
|
||||
@@ -77,8 +77,8 @@ class App:
|
||||
|
||||
for (x0, y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]):
|
||||
if good:
|
||||
cv.line(vis, (x0, y0), (x1, y1), (0, 128, 0))
|
||||
cv.circle(vis, (x1, y1), 2, (red, green)[good], -1)
|
||||
cv.line(vis, (int(x0), int(y0)), (int(x1), int(y1)), (0, 128, 0))
|
||||
cv.circle(vis, (int(x1), int(y1)), 2, (red, green)[good], -1)
|
||||
draw_str(vis, (20, 20), 'track count: %d' % len(self.p1))
|
||||
if self.use_ransac:
|
||||
draw_str(vis, (20, 40), 'RANSAC')
|
||||
@@ -86,7 +86,7 @@ class App:
|
||||
p = cv.goodFeaturesToTrack(frame_gray, **feature_params)
|
||||
if p is not None:
|
||||
for x, y in p[:,0]:
|
||||
cv.circle(vis, (x, y), 2, green, -1)
|
||||
cv.circle(vis, (int(x), int(y)), 2, green, -1)
|
||||
draw_str(vis, (20, 20), 'feature count: %d' % len(p))
|
||||
|
||||
cv.imshow('lk_homography', vis)
|
||||
|
||||
@@ -65,7 +65,7 @@ class App:
|
||||
if len(tr) > self.track_len:
|
||||
del tr[0]
|
||||
new_tracks.append(tr)
|
||||
cv.circle(vis, (x, y), 2, (0, 255, 0), -1)
|
||||
cv.circle(vis, (int(x), int(y)), 2, (0, 255, 0), -1)
|
||||
self.tracks = new_tracks
|
||||
cv.polylines(vis, [np.int32(tr) for tr in self.tracks], False, (0, 255, 0))
|
||||
draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks))
|
||||
|
||||
@@ -50,8 +50,11 @@ def main():
|
||||
cur_str_mode = str_modes.next()
|
||||
|
||||
def update(dummy=None):
|
||||
sz = cv.getTrackbarPos('op/size', 'morphology')
|
||||
iters = cv.getTrackbarPos('iters', 'morphology')
|
||||
try: # do not get trackbar position while trackbar is not created
|
||||
sz = cv.getTrackbarPos('op/size', 'morphology')
|
||||
iters = cv.getTrackbarPos('iters', 'morphology')
|
||||
except:
|
||||
return
|
||||
opers = cur_mode.split('/')
|
||||
if len(opers) > 1:
|
||||
sz = sz - 10
|
||||
|
||||
@@ -450,7 +450,8 @@ def main():
|
||||
cameras[i].focal *= compose_work_aspect
|
||||
cameras[i].ppx *= compose_work_aspect
|
||||
cameras[i].ppy *= compose_work_aspect
|
||||
sz = (full_img_sizes[i][0] * compose_scale, full_img_sizes[i][1] * compose_scale)
|
||||
sz = (int(round(full_img_sizes[i][0] * compose_scale)),
|
||||
int(round(full_img_sizes[i][1] * compose_scale)))
|
||||
K = cameras[i].K().astype(np.float32)
|
||||
roi = warper.warpRoi(sz, K, cameras[i].R)
|
||||
corners.append(roi[0:2])
|
||||
|
||||
@@ -30,7 +30,7 @@ def main():
|
||||
color = (0, 255, 0)
|
||||
|
||||
cap = cv.VideoCapture(0)
|
||||
cap.set(cv.CAP_PROP_AUTOFOCUS, False) # Known bug: https://github.com/opencv/opencv/pull/5474
|
||||
cap.set(cv.CAP_PROP_AUTOFOCUS, 0) # Known bug: https://github.com/opencv/opencv/pull/5474
|
||||
|
||||
cv.namedWindow("Video")
|
||||
|
||||
@@ -67,7 +67,7 @@ def main():
|
||||
break
|
||||
elif k == ord('g'):
|
||||
convert_rgb = not convert_rgb
|
||||
cap.set(cv.CAP_PROP_CONVERT_RGB, convert_rgb)
|
||||
cap.set(cv.CAP_PROP_CONVERT_RGB, 1 if convert_rgb else 0)
|
||||
|
||||
print('Done')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user