Skip to content

Commit

Permalink
final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhaoliang committed Feb 12, 2020
1 parent 959580e commit cbbd5c7
Show file tree
Hide file tree
Showing 8 changed files with 19,525 additions and 10 deletions.
6 changes: 0 additions & 6 deletions calibrate_camera.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""
import argparse
import os
import sys
import numpy as np
import yaml
import cv2
Expand All @@ -41,7 +40,6 @@ def main():
if not os.path.exists(args.output):
os.mkdir(args.output)

# 读取输入流
try:
source = cv2.VideoCapture(int(args.input))
except:
Expand All @@ -51,7 +49,6 @@ def main():
source.set(3, W)
source.set(4, H)

# 定义网格点
grid_size = tuple(int(x) for x in args.grid.split("x"))
grid_points = np.zeros((np.prod(grid_size), 3), np.float32)
grid_points[:, :2] = np.indices(grid_size).T.reshape(-1, 2)
Expand Down Expand Up @@ -83,10 +80,8 @@ def main():
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.01)
cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), term)
print("OK")
# 将找到的对应点加入列表
imgpoints.append(corners.reshape(1, -1, 2))
objpoints.append(grid_points.reshape(1, -1, 3))
# 在图像中标注出角点并保存到 debug 目录中
cv2.drawChessboardCorners(img, grid_size, corners, found)
text1 = "press c to calibrate"
text2 = "press q to quit"
Expand Down Expand Up @@ -146,7 +141,6 @@ def main():
if ret:
print(ret)
data = {"dim": np.array([W, H]).tolist(), "K": K.tolist(), "D": D.tolist()}
# 写入 yaml 文件
fname = os.path.join(args.output, "camera" + str(args.input) + ".yaml")
print(fname)
with open(fname, "w") as f:
Expand Down
Binary file added car.mp4
Binary file not shown.
1 change: 0 additions & 1 deletion get_projection_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import cv2
import numpy as np
import yaml
import os
from paramsettings import *


Expand Down
Binary file added paramsettings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions show_undistorted_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Show all four cameras and their device numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
import sys
import time
import cv2
import numpy as np
import yaml
Expand Down Expand Up @@ -66,7 +64,7 @@ def main():
cv2.imshow("corrected", corrected)

key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
if key == ord("q"):
break

for cap in captures:
Expand Down
2 changes: 2 additions & 0 deletions stitch_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
camera_params = [os.path.join(work_dir, f) for f in ("front.yaml", "back.yaml", "left.yaml", "right.yaml")]
camera_images = [os.path.join(work_dir, f) for f in ("front.png", "back.png", "left.png", "right.png")]


def dline(x, y, x1, y1, x2, y2):
"""Compute a pixel (x, y) to line segment (x1, y1) and (x2, y2).
"""
Expand Down Expand Up @@ -213,5 +214,6 @@ def merge(imA, imB, k):
with open(os.path.join(work_dir, "weights.yaml"), "w") as f:
yaml.dump(mats, f)


if __name__ == "__main__":
main()
189 changes: 189 additions & 0 deletions surroundview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Show the projected 360 surround view of four cameras
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Press Esc to exit.
"""
import numpy as np
import cv2
import yaml
import os
import time
from imutils.video import VideoStream
from config import *


# -----------------
# Global parameters
# -----------------
W, H = 640, 480 # camera resolution
window_title = "Carview"
camera_devices = [0, 1, 2, 3] # camera devices front, back, left, right
yaml_dir = "./yaml"
camera_params = [os.path.join(yaml_dir, f) for f in ("front.yaml", "back.yaml", "left.yaml", "right.yaml")]
weights_file = os.path.join(yaml_dir, "weights.yaml")
car_icon = os.path.join(yaml_dir, "car.png")


def adjust_illuminance(im, a):
return np.minimum((im.astype(np.float) * a), 255).astype(np.uint8)


def rgb_ratio(imA, imB):
overlap = cv2.bitwise_and(imA, imB)
gray = cv2.cvtColor(overlap, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
mask = cv2.dilate(mask, np.ones((5, 5), np.uint8), iterations=4)
imA_here = cv2.bitwise_and(imA, imA, mask=mask)
imB_here = cv2.bitwise_and(imB, imB, mask=mask)
B1, G1, R1 = cv2.split(imA_here)
B2, G2, R2 = cv2.split(imB_here)
c1 = np.mean(B1) / np.mean(B2)
c2 = np.mean(G1) / np.mean(G2)
c3 = np.mean(R1) / np.mean(R2)
return c1, c2, c3


def main():
print("[INFO] openning camera devices...")
captures = [VideoStream(src=k, resolution=(W, H)).start() for k in camera_devices]
time.sleep(2)
print("[INFO] loading camera intrinsic parameters...")
matrices = []
undistort_maps = []
for conf in camera_params:
with open(conf, "r") as f:
data = yaml.load(f)

proj_mat = np.array(data["M"])
matrices.append(proj_mat)

K = np.array(data["K"])
D = np.array(data["D"])
scale = np.array(data["scale"])
new_K = K.copy()
new_K[0, 0] *= scale[0]
new_K[1, 1] *= scale[1]
map1, map2 = cv2.fisheye.initUndistortRectifyMap(
K,
D,
np.eye(3),
new_K,
(W, H),
cv2.CV_16SC2
)
undistort_maps.append((map1, map2))

print("[INFO] loading weight matrices...")
with open(weights_file, "r") as f:
data = yaml.load(f)
G0 = np.array(data["G0"])
G1 = np.array(data["G1"])
G2 = np.array(data["G2"])
G3 = np.array(data["G3"])
weights = [np.stack((G, G, G), axis=2) for G in (G0, G1, G2, G3)]

def merge(imA, imB, k):
G = weights[k]
return G * imA + imB * (1 - G)

car = cv2.imread(car_icon)
car = cv2.resize(car, (x2 - x1, y2 - y1))

cv2.namedWindow(window_title)
cv2.moveWindow(window_title, 1500, 0)
result = np.zeros((totalHeight, totalWidth, 3), np.uint8)

while True:
frames = []
for i, cap in enumerate(captures):
frame = cap.read()
map1, map2 = undistort_maps[i]
frame = cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
frames.append(frame)

front = cv2.warpPerspective(
frames[0],
matrices[0],
frontShape
)
back = cv2.warpPerspective(
frames[1],
matrices[1],
frontShape
)
left = cv2.warpPerspective(
frames[2],
matrices[2],
leftShape
)
right = cv2.warpPerspective(
frames[3],
matrices[3],
leftShape
)

# flip the images
back = back[::-1, ::-1, :]
left = cv2.transpose(left)[::-1]
right = np.flip(cv2.transpose(right), 1)

# top-left
FL = merge(front[:, :x1], left[:y1], 0)
result[:y1, :x1] = FL
# bottom-left
BL = merge(back[:, :x1], left[y2:], 1)
result[y2:, :x1] = BL
# top-right
FR = merge(front[:, x2:], right[:y1], 2)
result[:y1, x2:] = FR
# bottom-right
BR = merge(back[:, x2:], right[y2:], 3)
result[y2:, x2:] = BR

# front
F = front[:, x1:x2]
result[:y1, x1:x2] = F
# back
B = back[:, x1:x2]
result[y2:, x1:x2] = B
# left
L = left[y1:y2]
result[y1:y2, :x1] = L
# right
R = right[y1:y2]
result[y1:y2, x2:] = R

a1, a2, a3 = rgb_ratio(right[:y1], front[:, x2:])
b1, b2, b3 = rgb_ratio(back[:, x2:], right[y2:])
c1, c2, c3 = rgb_ratio(left[y2:], back[:, :x1])
d1, d2, d3 = rgb_ratio(front[:, :x1], left[:y1])

e1 = (a1 + b1 + c1 + d1) / (a1*a1 + b1*b1 + c1*c1 + d1*d1)
e2 = (a2 + b2 + c2 + d2) / (a2*a2 + b2*b2 + c2*c2 + d2*d2)
e3 = (a3 + b3 + c3 + d3) / (a3*a3 + b3*b3 + c3*c3 + d3*d3)

ch1, ch2, ch3 = cv2.split(result)
ch1 = adjust_illuminance(ch1, e1)
ch2 = adjust_illuminance(ch2, e2)
ch3 = adjust_illuminance(ch3, e3)

result = cv2.merge((ch1, ch2, ch3))
result[y1:y2, x1:x2] = car # add car icon
cv2.imshow(window_title, result)

key = cv2.waitKey(1) & 0xFF
if key == 27:
break

time.sleep(2)

for cap in captures:
cap.stop()

cv2.destroyAllWindows()


if __name__ == "__main__":
main()
Loading

0 comments on commit cbbd5c7

Please sign in to comment.