示例#1
0
dist_paras = calibration.get_distortion_paras()
print('Camera intrinsix matrix is :')
print(intrinsic_mat)
print('Camera distortion parameters are :')
print(dist_paras)
print()

cap = cv2.VideoCapture('./project_video.mp4')
video_writer = cv2.VideoWriter('output_video.avi',
                               cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30,
                               image_size)

while cap.isOpened():
    ret, frame = cap.read()
    # distortion correction
    undist = calibration.distort_correction(frame)

    undist = cv2.resize(undist, (0, 0), fx=scaled_size, fy=scaled_size)

    # binary images from different processing methods
    sobel_x_bin = imgproc.abs_sobel_thresh(undist,
                                           sobel_kernel=5,
                                           orient='x',
                                           thresh_min=20,
                                           thresh_max=100)
    s_channel_bin = imgproc.hsv_s_threshold(undist, thresh=(80, 255))
    v_channel_bin = imgproc.hsv_v_threshold(undist, thresh=(80, 255))

    # combination of binary images
    thresholded_img = np.zeros_like(undist[:, :, 0])
    thresholded_img[(v_channel_bin == 255)
file_name = './output_images/undist_straight_lines1.jpg'

calibration = CameraCalibration('camera_cal/')
calibration.calibrate()
intrinsic_mat = calibration.get_intrinsic()
dist_paras = calibration.get_distortion_paras()

perspective_src_points = scaled_size * np.float32(
    [[233, 694], [595, 450], [686, 450], [1073, 694]])  # These points are manually selected
perspective_dst_points = np.float32([[offset, image_size[1]], [offset, 0],
                                     [image_size[0] - offset, 0], [image_size[0] - offset, image_size[1]]])

#######################
img = cv2.imread(file_name)
undist = calibration.distort_correction(img)
undist = cv2.resize(undist, (0, 0), fx=scaled_size, fy=scaled_size)

hls = cv2.cvtColor(undist, cv2.COLOR_BGR2HSV)
h = hls[:, :, 0]
s = hls[:, :, 1]
v = hls[:, :, 2]
s_bin = np.zeros_like(s)
s_bin[(s >= 100) & (s <= 255)] = 255
v_bin = np.zeros_like(v)
v_bin[(v >= 80) & (v <= 255)] = 255

gray = cv2.cvtColor(undist, cv2.COLOR_BGR2GRAY)
gray_bin = np.zeros_like(gray)

gray_bin[gray > 70] = 255
print(intrinsic_mat)
print('Camera distortion parameters are :')
print(dist_paras)
print()

# Part 2 -- Apply a distortion correction to raw images.
calibration.undistort_images()
print()

# Part 3 -- Use color transforms, gradients, etc., to create a thresholded binary image
test_image_paths = glob.glob('./test_images/*.jpg')
print('Start generating binary images from sample images...')
for test_img_path in test_image_paths:
    test_img = cv2.imread(test_img_path)
    # distortion correction
    test_img = calibration.distort_correction(test_img)
    # test_img = cv2.GaussianBlur(test_img, (5, 5), 0)

    # binary images from different processing methods
    # sobel_y_bin = imgproc.abs_sobel_thresh(test_img, orient='y', thresh_min=20, thresh_max=100)
    # mag_bin = imgproc.mag_thresh(test_img, sobel_kernel=5, thresh=(30, 100))
    # dir_bin = imgproc.dir_threshold(test_img, sobel_kernel=5, thresh=(0.7, 1.3))
    sobel_x_bin = imgproc.abs_sobel_thresh(test_img,
                                           sobel_kernel=5,
                                           orient='x',
                                           thresh_min=20,
                                           thresh_max=100)
    s_channel_bin = imgproc.hsv_s_threshold(test_img, thresh=(80, 255))
    v_channel_bin = imgproc.hsv_v_threshold(test_img, thresh=(80, 255))

    # combination of binary images