<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import numpy as np
import math
import matplotlib.pyplot as plt
from tensorflow import keras
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
def convolution(image, kernel):
    output = np.zeros(image.shape)

    image_row, image_col = (image.shape[0], image.shape[1])
    kernel_row, kernel_col = kernel.shape

    pad_height = int((kernel_row - 1) / 2)
    pad_width = int((kernel_col - 1) / 2)
    
    padded_image = np.zeros((image_row + (2 * pad_height), image_col + (2 * pad_width)))
    padded_image[pad_height:padded_image.shape[0] - pad_height, pad_width:padded_image.shape[1] - pad_width] = image

    for row in range(image_row):
        for col in range(image_col):
            output[row, col] = np.sum(kernel * padded_image[row:row + kernel_row, col:col + kernel_col])
    return output


def gaussian_kernel(size, sigma):
    xa = np.linspace(-(size // 2), size // 2, size)
    xb = np.linspace(-(size // 2), size // 2, size)

    x, y = np.meshgrid(xa, xb, sparse=True)
    kernel = (1 / (np.sqrt(2 * np.pi * sigma))) * np.exp(-(x*x + y*y) / (2. * sigma * sigma))
    
    print(kernel)
    return kernel


def rgb_to_gray(image):
    return np.reshape(
            np.dot(image[..., :3], [0.2989, 0.5870, 0.1140]), 
            (image.shape[0], image.shape[1])
        )


def convert_image(image):
    if image.shape[2] > 2:
        image = rgb_to_gray(image)
    return image


def sobel(image):
    image = convert_image(image)
    image = convolution(image, gaussian_kernel(9, 1))

    vertical_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

    vertical_conv = convolution(image, vertical_filter)
    horizontal_conv = convolution(image, vertical_filter.T)

    output = np.sqrt(np.square(vertical_conv) + np.square(horizontal_conv))

    return output

def prewitt(image):
    image = convert_image(image)
    image = convolution(image, gaussian_kernel(9, 1))

    vertical_filter = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
    vertical_conv = convolution(image, vertical_filter)
    horizontal_conv = convolution(image, vertical_filter.T)

    output = np.sqrt(np.square(vertical_conv) + np.square(horizontal_conv))

    return output
plt.imshow(gaussian_kernel(5, 1))
plt.colorbar()
plt.axis('off')
plt.show()
img = load_img("Test.gif")
image_array = img_to_array(img)
image_array.shape
gray_img = convert_image(image_array)
gauss_filter = gaussian_kernel(5, 5)
result = convolution(gray_img, gauss_filter)
result.shape
fig, ax = plt.subplots(1, 2, figsize=(10,10))
ax[0].imshow(gray_img, cmap='gray')
ax[0].axis('off')

ax[1].imshow(result, cmap='gray')
ax[1].axis('off')
from keras_preprocessing import image
main_output = sobel(image_array)
plt.figure(figsize=(10, 10))
plt.imshow(main_output, cmap="gray")
plt.axis('off')
main_output = prewitt(image_array)
plt.figure(figsize=(10, 10))
plt.imshow(main_output, cmap="gray")
plt.axis('off')
plt.figure(figsize=(8,8))
plt.imshow(gaussian_kernel(5, 1))
def cosine_transform_kernel(point):
    output = np.zeros((point, point))
    for k in range(point):
        for n in range(point):
            if k == 0:
                output[k, n] = np.sqrt(1 / point) * np.cos( (90 * (2 * n + 1) * k) / 2 * point )
            else:
                output[k, n] = (np.sqrt(2 / point)) * np.cos( (90 * (2 * n + 1) * k) / 2 * point )
    return output
def dct(point):
    output = np.zeros((point, point))
    for u in range(point):
        for v in range(point):
            if u == 0:
                output[u, v] = np.sqrt(1 / point)
            else:
                output[u, v] = np.sqrt(2 / point) * math.cos( ( ( (2 * v + 1) * u) * math.pi) / (2 * point) )
    return output
dct(512)
def recenter(shape=(4, 4)):
    row, col = shape
    center_matrix = np.zeros((row, col))

    for i in range(0, row):
        for j in range(0, col):
            center_matrix[i, j] = math.pow(-1, i + j)

    return center_matrix
def step1(image, recenter):
    return np.multiply(image, recenter)
def step2(image, kernel):
    output = np.matmul(kernel, image)
    output = np.matmul(output, kernel.T)
    return output
from sklearn.preprocessing import minmax_scale
shape = gray_img.shape
image_scaled = minmax_scale(gray_img.ravel(), feature_range=(-128,127)).reshape(shape)
image_scaled.shape
image_scaled
recenter_mat = recenter((512, 512))
r_image = step1(image_scaled, recenter_mat)
dct_point = 8

out = np.zeros(r_image.shape)
for i in range(0, 512, dct_point):
    for j in range(0, 512, dct_point):
        out[i : i + dct_point, j : j + dct_point] = step2(r_image[i : i + dct_point, j : j + dct_point], dct(dct_point))
plt.imshow(r_image, cmap="gray")
plt.imshow(out,cmap='gray',vmax = np.max(out)*0.01,vmin = 0)
def calc_distance(image):
    row, col = image.shape
    result = np.zeros((row, col))

    for i in range(0, row):
        for j in range(0, col):
            result[i, j] = np.sqrt(math.pow((i - (row / 2)), 2) + math.pow((j - (col / 2)), 2))

    return result
calc_distance(out)
plt.imshow(calc_distance(out))
plt.colorbar()
np.average(out)
def ilpf(image, cutoff=1):
    return np.where(image > cutoff, 1, 0)
out.max() / 512
pq = calc_distance(out)
np.any(pq < 0)
pp = ilpf(pq, 1.09)
np.any(pp < 0)
def step3(f_u_v, h_u_v):
    return np.multiply(f_u_v, h_u_v)
result_image = step3(out, pp)
plt.imshow(result_image)
def step3(image, kernel):
    output = np.matmul(kernel.T, image)
    output = np.matmul(output, kernel)
    return output

rs = np.zeros(result_image.shape)
for i in range(0, 512, dct_point):
    for j in range(0, 512, dct_point):
        rs[i : i + dct_point, j : j + dct_point] = step3(out[i : i + dct_point, j : j + dct_point], dct(dct_point))
plt.imshow(rs, cmap="gray", vmax = np.max(out)*0.01,vmin = 0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1536383589397265, 'fraction_numerical': 0.029377004896167482, 'mean_word_length': 3.4374531835205993, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '263900', 'license': 'MIT', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2222, 'n_words': 520, 'path': 'EdgeDetectorsAlongwithDCT.ipynb', 'repo_name': 'ND15/Image-Processing', 'size': '5923'}