کد زیر جهت ها را به اشتباه تشخیص میدهد و همه جهت های حرکت را رو به پایین نمایش میدهد لطفا کد را اصلاه کنید:                                                                                                                                                                                                        import numpy as np
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from scipy.ndimage import convolve
from scipy.linalg import lstsq

# Preprocess images
def preprocess_image(image_path):
    """Load and preprocess image (grayscale and normalized)."""
    image = Image.open(image_path).convert('L')  # Convert to grayscale
    return np.array(image) / 255.0  # Normalize to [0, 1]

# Detect corners
def detect_corners(image, num_corners=50):
    """Detect corners using Shi-Tomasi method."""
    img = (image * 255).astype(np.uint8)
    corners = cv2.goodFeaturesToTrack(img, maxCorners=num_corners, qualityLevel=0.01, minDistance=10)
    return np.int0(corners)

# Compute Optical Flow for the corners
def compute_opt_flow(I1, I2, corners, windowSize, tau):
    """Compute optical flow at the specified corners using Lucas-Kanade method."""
    half_win = windowSize // 2
    Ix = convolve(I1, np.array([[-1, 1], [-1, 1]]) * 0.5) + convolve(I2, np.array([[-1, 1], [-1, 1]]) * 0.5)
    Iy = convolve(I1, np.array([[-1, -1], [1, 1]]) * 0.5) + convolve(I2, np.array([[-1, -1], [1, 1]]) * 0.5)
    It = convolve(I2, np.ones((2, 2)) * 0.25) - convolve(I1, np.ones((2, 2)) * 0.25)

    u = np.zeros_like(I1)
    v = np.zeros_like(I1)

    for corner in corners:
        y, x = corner.ravel()
        if x - half_win < 0 or x + half_win >= I1.shape[1] or y - half_win < 0 or y + half_win >= I1.shape[0]:
            continue

        Ix_win = Ix[y-half_win:y+half_win+1, x-half_win:x+half_win+1].flatten()
        Iy_win = Iy[y-half_win:y+half_win+1, x-half_win:x+half_win+1].flatten()
        It_win = It[y-half_win:y+half_win+1, x-half_win:x+half_win+1].flatten()

        A = np.stack((Ix_win, Iy_win), axis=1)
        b = -It_win

        ATA = A.T @ A
        eigenvalues = np.linalg.eigvals(ATA)

        if min(eigenvalues) >= tau:
            flow = lstsq(ATA, A.T @ b, cond=None)[0]
            u[y, x], v[y, x] = flow

    return u, v

def display_results(image, u, v, corners):
    """Display the image with optical flow vectors for detected corners."""
    plt.figure(figsize=(10, 10))  # Larger figure
    plt.imshow(image, cmap='gray')

    scale_factor = 100  # Increased scaling factor for making arrows larger

    for corner in corners:
        x, y = corner.ravel()
        plt.plot(x, y, 'ro', markersize=5, markerfacecolor='none')  # Red hollow circle

        # Scale up arrows uniformly
        plt.arrow(x, y, u[y, x] * scale_factor, v[y, x] * scale_factor,
                  color='blue', head_width=8, head_length=15, linewidth=2)

    plt.title("Optical Flow with Larger Arrows", fontsize=16)
    plt.axis('off')
    plt.show()

# Paths to images
I1_path = "/content/coridor/hw4/data/corridor/bt.000.png"
I2_path = "/content/coridor/hw4/data/corridor/bt.004.png"

# Load and preprocess images
I1 = preprocess_image(I1_path)
I2 = preprocess_image(I2_path)

# Detect corners in the first image
corners = detect_corners(I1)

# Compute optical flow for these corners
window_size = 15  # Example window size
tau = 0.01        # Example tau value
u, v = compute_opt_flow(I1, I2, corners, window_size, tau)

# Display the results
display_results(I1, u, v, corners)