Beispiel #1
0
 def test_warp_frame(self):
     start = 3300
     end = 3400
     ratio = 0.9
     frames, fps = stabilization.extract_frames_from_video(
         self.sample_video_file, start=start, end=end)
     raw_homography = stabilization.compute_homographies_from_frames(frames)
     shape = frames[0].shape
     smoothed = stabilization.compute_smooth_path(shape,
                                                  raw_homography,
                                                  ratio=ratio)
     boxed_frames = np.copy(frames)
     new_frames = stabilization.apply_homography_on_frames(frames,
                                                           smoothed,
                                                           ratio=ratio)
     stabilization.create_video_from_frames(
         new_frames,
         path.join(self.sample_video_output_dir,
                   "frame_smooth_{}_{}.mp4".format(start, end)),
         fps=30)
     boxed_frames = stabilization.draw_smoothed_box_on_frames(boxed_frames,
                                                              smoothed,
                                                              ratio=ratio)
     stabilization.create_video_from_frames(
         boxed_frames,
         path.join(self.sample_video_output_dir,
                   "frame_box_{}_{}.mp4".format(start, end)),
         fps=30)
     stabilization.plot_homographies(raw_homography,
                                     self.sample_video_output_dir,
                                     smoothed_homography=smoothed,
                                     start=start,
                                     end=end)
Beispiel #2
0
 def test_draw_box(self):
     frames, fps = stabilization.extract_frames_from_video(
         self.sample_video_file, end=2)
     shape = frames[0].shape
     new_image = stabilization.draw_box(frames[0],
                                        stabilization.get_corners(shape))
     cv2.imwrite("box.jpg", new_image)
Beispiel #3
0
    def test_compute_smooth_path_with_motion_video(self):
        # params for ShiTomasi corner detection
        feature_params = dict(maxCorners=100,
                              qualityLevel=0.1,
                              minDistance=4,
                              blockSize=4)

        # Parameters for lucas kanade optical flow
        lk_params = dict(winSize=(5, 5),
                         maxLevel=2,
                         criteria=(cv2.TERM_CRITERIA_EPS
                                   | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
        start = 0
        end = None
        homographies = stabilization.compute_homographies_from_video(
            self.motion_video_file,
            start=start,
            end=end,
            feature_params=feature_params,
            lk_params=lk_params,
            show_frames=False,
            output_dir=self.motion_video_output_dir)
        frames, fps = stabilization.extract_frames_from_video(
            self.motion_video_file, end=2)
        shape = frames[0].shape

        smoothed = stabilization.compute_smooth_path(shape,
                                                     homographies,
                                                     ratio=0.8)
        stabilization.plot_homographies(
            raw_homography=homographies,
            output_dir=self.motion_video_output_dir,
            smoothed_homography=smoothed,
            start=start,
            end=end)
Beispiel #4
0
 def test_write_frames_to_file(self):
     """write frames to image file"""
     frames, fps = stabilization.extract_frames_from_video(
         self.motion_video_file)
     for i in range(len(frames)):
         frame_filename = path.join(self.motion_video_output_dir,
                                    "frame_{}.jpg".format(i))
         cv2.imwrite(frame_filename, frames[i])
Beispiel #5
0
    def test_optical_flow(self):
        frames, fps = stabilization.extract_frames_from_video(
            self.motion_video_file)
        homographies = []
        # params for ShiTomasi corner detection
        feature_params = dict(maxCorners=100,
                              qualityLevel=0.1,
                              minDistance=4,
                              blockSize=4)

        # Parameters for lucas kanade optical flow
        lk_params = dict(winSize=(5, 5),
                         maxLevel=2,
                         criteria=(cv2.TERM_CRITERIA_EPS
                                   | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

        for i in range(1, len(frames)):
            prev_frame = frames[i - 1]
            prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
            p0 = cv2.goodFeaturesToTrack(prev_gray,
                                         mask=None,
                                         **feature_params)

            curr_frame = frames[i]
            curr_gray = cv2.cvtColor(curr_frame, cv2.COLOR_BGR2GRAY)
            # calculate optical flow
            p1, st, err = cv2.calcOpticalFlowPyrLK(prev_gray, curr_gray, p0,
                                                   None, **lk_params)

            # Select good points
            good_new = p1[st == 1]
            good_old = p0[st == 1]

            circled = np.copy(prev_frame)
            for j in good_old:
                x, y = j.ravel()
                cv2.circle(circled, (x, y), 1, (0, 0, 255), -1)
            frame_filename = path.join(self.motion_video_output_dir,
                                       "frame_{}_1.jpg".format(i))
            cv2.imwrite(frame_filename, circled)

            circled = np.copy(curr_frame)
            for j in good_new:
                x, y = j.ravel()
                cv2.circle(circled, (x, y), 1, (0, 0, 255), -1)
            frame_filename = path.join(self.motion_video_output_dir,
                                       "frame_{}_2.jpg".format(i))
            cv2.imwrite(frame_filename, circled)

            M, _ = cv2.estimateAffine2D(good_new, good_old, method=cv2.RANSAC)
            H = np.append(M, np.array([0, 0, 1]).reshape((1, 3)), axis=0)
            homographies.append(H)

        homographies = np.array(homographies)
        print(homographies)
        stabilization.plot_homographies(homographies,
                                        self.motion_video_output_dir)
Beispiel #6
0
 def test_apply_homography_on_frame(self):
     frames, fps = stabilization.extract_frames_from_video(
         self.sample_video_file, end=2)
     new_frame = stabilization.apply_homography_on_frame(frames[0],
                                                         np.array(
                                                             [[1, 0, 0],
                                                              [0, 1, 0],
                                                              [0, 0, 1]]),
                                                         ratio=0.8)
     print(new_frame.shape)
     cv2.imwrite("cropped.jpg", new_frame)
Beispiel #7
0
 def test_plot_draw_good_feature(self):
     """draw good features on a image"""
     frames, fps = stabilization.extract_frames_from_video(
         self.motion_video_file)
     image = frames[0]
     image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     feature_params = dict(maxCorners=100,
                           qualityLevel=0.3,
                           minDistance=7,
                           blockSize=7)
     corners = cv2.goodFeaturesToTrack(image_gray,
                                       mask=None,
                                       **feature_params)
     for i in corners:
         x, y = i.ravel()
         cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
     img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
     plt.imshow(img)
     plt.title("good_feature_to_track with {} corners".format(100))
     plt.show()
Beispiel #8
0
 def test_compute_smooth_path_with_sample_video(self):
     start = 0
     end = 500
     raw_homography = stabilization.compute_homographies_from_video(
         self.sample_video_file,
         start=start,
         end=end,
         feature_params=self.sample_video_feature_params,
         lk_params=self.sample_video_lk_params,
         show_frames=False,
         output_dir=self.sample_video_output_dir)
     frames, fps = stabilization.extract_frames_from_video(
         self.sample_video_file, end=2)
     shape = frames[0].shape
     smoothed = stabilization.compute_smooth_path(shape,
                                                  raw_homography,
                                                  ratio=0.8)
     stabilization.plot_homographies(
         raw_homography=raw_homography,
         output_dir=self.sample_video_output_dir,
         smoothed_homography=smoothed,
         start=start,
         end=end)
Beispiel #9
0
 def test_save(self):
     frames, fps = stabilization.extract_frames_from_video(
         self.sample_video_file)
     np.save(path.join(self.sample_video_output_dir, "frames.npy"), frames)