<jupyter_start><jupyter_text>Question 5 Part 1  Description Read Cameraman Image and Using MSE and PSNR method to calculate with itself and compare MSE and PSNR, then create some random white point in picture and compare MSE and PSNR, then transfer pixel up to 1 pixel to right and compare MSE and PSNR  Import required dependencies - Import cv2 for read and map the color channels- Import math to get the maximum number we can use as infinite- Import random to get random number- Import numpy to work with arrays- Import matplotlib to show image for each step<jupyter_code>import cv2
import math
import random
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Read Image<jupyter_code>cameraman = cv2.imread("../images/cameraman.tif")

# Convert BGR order to RGB
cameraman = cv2.cvtColor(cameraman, cv2.COLOR_BGR2RGB)

plt.imshow(cameraman)<jupyter_output><empty_output><jupyter_text>Define MSE Calculation Function The MSE measures the average of the squares of the errors<jupyter_code>def mse(image_1, image_2):
    return ((image_1 - image_2) ** 2).sum() / \
        (image_1.shape[0] * image_1.shape[1])<jupyter_output><empty_output><jupyter_text>Define PSNR Calculation Function PSNR represents a measure of the peak error. The lower the value of MSE, the lower the error<jupyter_code>def psnr(image_1, image_2):
    mse_value = mse(image_1, image_2)

    if mse_value == 0:
        return math.inf

    return 10 * np.log((255 ** 2) / mse_value)<jupyter_output><empty_output><jupyter_text>Calculate PSNR and MSE with itself<jupyter_code>print("MSE: " + str(mse(cameraman, cameraman)))
print("PSNR: " + str(psnr(cameraman, cameraman)))<jupyter_output>MSE: 0.0
PSNR: inf<jupyter_text>As result, if the images are same, MSE must be 0 and PSNR must be infinit  Add white point in Cameraman<jupyter_code># Create a copy from cameraman
edited_cameraman_1 = np.copy(cameraman)
    
edited_cameraman_2 = np.copy(cameraman)

for x in range(10):
    width = round(random.triangular(0, cameraman.shape[1]))
    height = round(random.triangular(0, cameraman.shape[0]))
    edited_cameraman_1[height, width, :] = [255, 255, 255]

for x in range(50):
    width = round(random.triangular(0, cameraman.shape[1]))
    height = round(random.triangular(0, cameraman.shape[0]))
    edited_cameraman_2[height, width, :] = [255, 255, 255]<jupyter_output><empty_output><jupyter_text>Calculate PSNR and MSE of Cameraman and Edited Version<jupyter_code>print("MSE 1: " + str(mse(edited_cameraman_1, cameraman)))
print("PSNR 1: " + str(psnr(edited_cameraman_1, cameraman)))
print()
print("MSE 2: " + str(mse(edited_cameraman_2, cameraman)))
print("PSNR 2: " + str(psnr(edited_cameraman_2, cameraman)))<jupyter_output>MSE 1: 0.04925537109375
PSNR 1: 140.9326394988614

MSE 2: 0.24920654296875
PSNR 2: 124.7200032683651<jupyter_text>As result, when MSE are high means the images are more different and when PSNR are high means the images are more similar  Transfer Pixels<jupyter_code># Create a copy from cameraman
moved_cameraman = np.copy(cameraman)

step = 1

# Transfer pixels up to step
moved_cameraman[:, 0, :] = cameraman[:, cameraman.shape[1] - step, :]
moved_cameraman[:, step:moved_cameraman.shape[1], :] \
    = cameraman[:, 0:cameraman.shape[1] - step, :]<jupyter_output><empty_output><jupyter_text>Calculate PSNR and MSE of Cameraman and Transfered Image<jupyter_code>print("MSE: " + str(mse(moved_cameraman, cameraman)))
print("PSNR: " + str(psnr(moved_cameraman, cameraman)))<jupyter_output>MSE: 95.3616943359375
PSNR: 65.24850119373079
{'fraction_non_alphanumeric': 0.10492445439283715, 'fraction_numerical': 0.049524342473419136, 'mean_word_length': 5.7965779467680605, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '377214', 'license': 'MIT', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1291, 'n_words': 348, 'path': 'Q5/Q5-1.ipynb', 'repo_name': 'AliRezaBeigy/MultiMediaCourse', 'size': '3574'}