Image_Registration_XYAngle.ipynb

未分類

To use Image_Registration_XYAngle.ipynb, first we shoud run Template matching (larval fish tracking) parallel ThreadPool.jpynb and the resulted .tsv file is used her.

The .tsv file should have columns named, ‘Frame’, ‘X_center’, ‘Y_center’, ‘Angle’. With this information, the script will translate and rotate the input avi file so that the fish larva will be locatec at the center upright. 

# Image_Registration_XYAngle.ipynb
# 20250204AM with ChatGPT

import cv2
import numpy as np
import pandas as pd

def register_frames(video_path, tsv_path, output_path):
    # Read the TSV file
    df = pd.read_csv(tsv_path, delimiter='\t')
    
    # Open video
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        print("Error: Cannot open video file.")
        return
    
    # Get video properties
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
    # Define the output video writer
    fourcc = 0     out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))

    # Image center (fixed)
    img_center = np.array([frame_width / 2, frame_height / 2])

    frame_idx = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret or frame_idx >= len(df):
            break
        
        # Read transformation parameters
        x_center, y_center, angle = df.loc[frame_idx, ['X_center', 'Y_center', 'Angle']]
        
        # Compute translation matrix
        translation_matrix = np.float32([[1, 0, img_center[0] - x_center],
                                         [0, 1, img_center[1] - y_center]])
        
        # Apply translation
        frame = cv2.warpAffine(frame, translation_matrix, (frame_width, frame_height))

        # Compute rotation matrix (negative angle for inverse rotation)
        rotation_matrix = cv2.getRotationMatrix2D(tuple(img_center), -angle, 1.0)

        # Apply rotation
        frame = cv2.warpAffine(frame, rotation_matrix, (frame_width, frame_height))

        # Write frame to output video
        out.write(frame)

        frame_idx += 1

    # Release resources
    cap.release()
    out.release()
    print(f"Registered video saved to {output_path}")

# Example usage
video_file = "input.avi"
tsv_file = "XYAngle.tsv"
output_file = "registered.avi"

register_frames(video_file, tsv_file, output_file)
タイトルとURLをコピーしました