示例#1
0
    def __generate_stabilized_video(self, frame, frame_n, new_x_motion_meshes,
                                    new_y_motion_meshes):
        """
        @param: cap is the cv2.VideoCapture object that is
                instantiated with given video
        @param: x_motion_meshes is the motion vectors on
                mesh vertices in x-direction
        @param: y_motion_meshes is the motion vectors on
                mesh vertices in y-direction
        @param: new_x_motion_meshes is the updated motion vectors
                on mesh vertices in x-direction to be warped with
        @param: new_y_motion_meshes is the updated motion vectors
                on mesh vertices in y-direction to be warped with
        """

        new_x_motion_mesh = new_x_motion_meshes[:, :, -1]
        new_y_motion_mesh = new_y_motion_meshes[:, :, -1]

        # mesh warping
        new_frame = mesh_warp_frame(frame, new_x_motion_mesh,
                                    new_y_motion_mesh)
        new_frame = new_frame[self.HORIZONTAL_BORDER:-self.HORIZONTAL_BORDER,
                              self.VERTICAL_BORDER:-self.VERTICAL_BORDER, :]
        new_frame = cv2.resize(new_frame, (frame.shape[1], frame.shape[0]),
                               interpolation=cv2.INTER_CUBIC)
        return new_frame
def generate_stabilized_video(cap, new_x_motion_meshes, new_y_motion_meshes):
    """
    @param: cap -  объект cv2.VideoCapture, который
             инициализруется с подаваемым видео
    @param: x_motion_meshes - векторы движения на
             вершинах сетки в направлении х
    @param: y_motion_meshes - векторы движения на
             вершинах сетки в направлении y
    @param: new_x_motion_meshes - обновленные векторы движения
             на вершинах сетки в направлении оси X для преобразования
    @param: new_y_motion_meshes - обновленные векторы движения
             на вершинах сетки в направлении оси X для преобразования
    """

    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    fourcc = cv2.VideoWriter_fourcc(*'DIVX')

    out = cv2.VideoWriter('stable.avi', int(fourcc), frame_rate, (2*frame_width, frame_height))

    frame_num = 0
    bar = tqdm(total=frame_count)
    while frame_num < frame_count:
        try:
            ret, frame = cap.read()
            new_x_motion_mesh = new_x_motion_meshes[:, :, frame_num]
            new_y_motion_mesh = new_y_motion_meshes[:, :, frame_num]

            new_frame = mesh_warp_frame(frame, new_x_motion_mesh, new_y_motion_mesh)
            new_frame = new_frame[int(HORIZONTAL_BORDER):-int(HORIZONTAL_BORDER), int(VERTICAL_BORDER):-int(VERTICAL_BORDER), :]
            new_frame = cv2.resize(new_frame, (frame.shape[1], frame.shape[0]), interpolation=cv2.INTER_CUBIC)
            out.write(new_frame)

            frame_num += 1
            bar.update(1)
        except:
            break

    bar.close()
    cap.release()
    out.release()
def generate_stabilized_video(cap, x_motion_meshes, y_motion_meshes,
                              new_x_motion_meshes, new_y_motion_meshes):
    """
    @param: cap is the cv2.VideoCapture object that is
            instantiated with given video
    @param: x_motion_meshes is the motion vectors on
            mesh vertices in x-direction
    @param: y_motion_meshes is the motion vectors on
            mesh vertices in y-direction
    @param: new_x_motion_meshes is the updated motion vectors 
            on mesh vertices in x-direction to be warped with
    @param: new_y_motion_meshes is the updated motion vectors 
            on mesh vertices in y-direction to be warped with
    """

    # get video properties
    cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    frame_rate = int(cap.get(cv2.CAP_PROP_FPS))
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

    # generate stabilized video
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('../stable.avi', fourcc, frame_rate,
                          (2 * frame_width, frame_height))

    frame_num = 0
    bar = tqdm(total=frame_count)
    while frame_num < frame_count:
        try:
            # reconstruct from frames
            ret, frame = cap.read()
            x_motion_mesh = x_motion_meshes[:, :, frame_num]
            y_motion_mesh = y_motion_meshes[:, :, frame_num]
            new_x_motion_mesh = new_x_motion_meshes[:, :, frame_num]
            new_y_motion_mesh = new_y_motion_meshes[:, :, frame_num]

            # mesh warping
            new_frame = mesh_warp_frame(frame, new_x_motion_mesh,
                                        new_y_motion_mesh)
            new_frame = new_frame[HORIZONTAL_BORDER:-HORIZONTAL_BORDER,
                                  VERTICAL_BORDER:-VERTICAL_BORDER, :]
            new_frame = cv2.resize(new_frame, (frame.shape[1], frame.shape[0]),
                                   interpolation=cv2.INTER_CUBIC)
            output = np.concatenate((frame, new_frame), axis=1)
            out.write(output)

            # draw old motion vectors
            r = 5
            for i in range(x_motion_mesh.shape[0]):
                for j in range(x_motion_mesh.shape[1]):
                    theta = np.arctan2(y_motion_mesh[i, j], x_motion_mesh[i,
                                                                          j])
                    cv2.line(frame, (j * PIXELS, i * PIXELS),
                             (int(j * PIXELS + r * np.cos(theta)),
                              int(i * PIXELS + r * np.sin(theta))), 1)
            cv2.imwrite(
                '../results/old_motion_vectors/' + str(frame_num) + '.jpg',
                frame)

            # draw new motion vectors
            for i in range(new_x_motion_mesh.shape[0]):
                for j in range(new_x_motion_mesh.shape[1]):
                    theta = np.arctan2(new_y_motion_mesh[i, j],
                                       new_x_motion_mesh[i, j])
                    cv2.line(new_frame, (j * PIXELS, i * PIXELS),
                             (int(j * PIXELS + r * np.cos(theta)),
                              int(i * PIXELS + r * np.sin(theta))), 1)
            cv2.imwrite(
                '../results/new_motion_vectors/' + str(frame_num) + '.jpg',
                new_frame)

            frame_num += 1
            bar.update(1)
        except:
            break

    bar.close()
    cap.release()
    out.release()