آیا کد زیر این کار ها را انجام میدهد:  تصویر را به سه کانال تقسیم کند و دو تا از کانال ها را با کانال سوم تراز کند )باید ترتیب های مختلف تراز کردن کانال ها را امتحان
کنید و بفهمید که کدام یک بهترین نتیجه را دارد(. برای هر تصویر ورودی، تصویر خروجی رنگی و بردارهای جابجایی (tx,ty) را که برای تراز
کردن کانال ها استفاده شده است را در  دهد.                                                                                                                                                                                                                                                                                                                                                                                                                 
 اimport cv2
import numpy as np
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow  # For Google Colab

def create_colored_canvas(channel, color_index):
    """Create a blank black canvas and embed one colored channel in its position."""
    colored_canvas = np.zeros((channel.shape[0], channel.shape[1], 3), dtype=np.uint8)
    
    # Set the specific channel for its corresponding color and keep others as zero
    colored_canvas[:, :, color_index] = channel
    return colored_canvas

def display_channels_as_colors(r_channel, g_channel, b_channel):
    """Display each channel in its respective color."""
    # Create colored images for each channel
    red_image = create_colored_canvas(r_channel, 2)  # Red is the third channel
    green_image = create_colored_canvas(g_channel, 1)  # Green is the second channel
    blue_image = create_colored_canvas(b_channel, 0)  # Blue is the first channel

    # Display using matplotlib
    fig, ax = plt.subplots(1, 3, figsize=(15, 5))
    ax[0].imshow(cv2.cvtColor(red_image, cv2.COLOR_BGR2RGB))
    ax[0].set_title('Red Channel')
    ax[0].axis('off')

    ax[1].imshow(cv2.cvtColor(green_image, cv2.COLOR_BGR2RGB))
    ax[1].set_title('Green Channel')
    ax[1].axis('off')

    ax[2].imshow(cv2.cvtColor(blue_image, cv2.COLOR_BGR2RGB))
    ax[2].set_title('Blue Channel')
    ax[2].axis('off')

    plt.show()

# Load the image
image_path = '/content/prokudin-Gorskii/hw1/test.jpg'
image = cv2.imread(image_path)

# Check if image is loaded
if image is None:
    print(f"Error loading image at path {image_path}")
else:
    # Split image into B, G, R channels
    b_channel, g_channel, r_channel = cv2.split(image)

    # Display the individual channels in their respective colors
    display_channels_as_colors(r_channel, g_channel, b_channel)

    # Possible orders to align
    orders = [
        ('B', 'G', 'R'),
        ('B', 'R', 'G'),
        ('G', 'R', 'B'),
        ('G', 'B', 'R'),
        ('R', 'B', 'G'),
        ('R', 'G', 'B')
    ]

    best_alignment = None
    min_score = float('inf')

    # Try all orders and find the one with the smallest aligning score
    for order in orders:
        # Add borders
        r_canvas = cv2.copyMakeBorder(r_channel, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=255)
        g_canvas = cv2.copyMakeBorder(g_channel, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=255)
        b_canvas = cv2.copyMakeBorder(b_channel, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=255)

        # Map order to channel variables
        base_layer_name = order[0]
        layer1_name, layer2_name = order[1], order[2]
        base_layer = {'R': r_canvas, 'G': g_canvas, 'B': b_canvas}[base_layer_name]
        layer1 = {'R': r_canvas, 'G': g_canvas, 'B': b_canvas}[layer1_name]
        layer2 = {'R': r_canvas, 'G': g_canvas, 'B': b_canvas}[layer2_name]

        # Align layers
        aligned_layer1, shift1 = align_layers(base_layer, layer1)
        aligned_layer2, shift2 = align_layers(base_layer, layer2)

        # Calculate score
        score = np.sum((base_layer - aligned_layer1) ** 2) + np.sum((base_layer - aligned_layer2) ** 2)

        if score < min_score:
            min_score = score
            merged = {
                base_layer_name: base_layer,
                layer1_name: aligned_layer1,
                layer2_name: aligned_layer2
            }
            aligned_image = cv2.merge((merged['B'], merged['G'], merged['R']))
            best_shift1 = shift1
            best_shift2 = shift2
            best_order = order

    # Save the aligned image
    output_image_path = 'aligned_image.jpg'
    cv2.imwrite(output_image_path, aligned_image)
    print(f"Aligned image saved to {output_image_path}")
    print(f"Best order: {best_order}")
    print(f"Shift for first alignment: {best_shift1}")
    print(f"Shift for second alignment: {best_shift2}")

    # Display aligned image using OpenCV method (works in some Colab versions)
    cv2_imshow(aligned_image)