File size: 3,953 Bytes
02546ba b9267dc 51f6fb9 02546ba 51f6fb9 02546ba b9267dc 02546ba 51f6fb9 02546ba 51f6fb9 02546ba b9267dc 02546ba b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 02546ba b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 02546ba b9267dc 02546ba b9267dc 02546ba b9267dc 02546ba b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 02546ba 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 02546ba 51f6fb9 02546ba 51f6fb9 b9267dc 51f6fb9 b9267dc 51f6fb9 02546ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import gradio as gr
import cv2
import numpy as np
import matplotlib.pyplot as plt
# =========================================================
# KERNELS
# =========================================================
kernels = {
"Blur": np.ones((3, 3), np.float32) / 9,
"Sharpen": np.array([
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]
]),
"Edge Detection": np.array([
[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]
]),
"Emboss": np.array([
[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]
])
}
# =========================================================
# IMAGE PROCESSING
# =========================================================
def process_image(image, kernel_name):
if image is None:
return None
# RGB → Gray
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Selected Kernel
kernel = kernels[kernel_name]
# Apply Kernel
filtered = cv2.filter2D(gray, -1, kernel)
# Edge Detection
canny = cv2.Canny(gray, 100, 200)
# Histogram
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
# =========================================================
# VISUALIZATION
# =========================================================
fig, axs = plt.subplots(2, 3, figsize=(14, 8))
fig.suptitle(
"Kernel Matrix Visualization",
fontsize=18,
fontweight="bold"
)
# ---------------------------------------------------------
axs[0, 0].imshow(image)
axs[0, 0].set_title("Original Image")
axs[0, 0].axis("off")
# ---------------------------------------------------------
axs[0, 1].imshow(gray, cmap="gray")
axs[0, 1].set_title("Grayscale")
axs[0, 1].axis("off")
# ---------------------------------------------------------
axs[0, 2].imshow(filtered, cmap="gray")
axs[0, 2].set_title(f"{kernel_name} Output")
axs[0, 2].axis("off")
# ---------------------------------------------------------
axs[1, 0].imshow(canny, cmap="gray")
axs[1, 0].set_title("Canny Edge Detection")
axs[1, 0].axis("off")
# ---------------------------------------------------------
axs[1, 1].plot(hist)
axs[1, 1].set_title("Pixel Histogram")
axs[1, 1].set_xlim([0, 256])
# ---------------------------------------------------------
axs[1, 2].imshow(kernel, cmap="coolwarm")
axs[1, 2].set_title(f"{kernel_name} Matrix")
for i in range(kernel.shape[0]):
for j in range(kernel.shape[1]):
axs[1, 2].text(
j,
i,
str(kernel[i, j]),
ha="center",
va="center",
fontsize=12,
fontweight="bold",
color="black"
)
axs[1, 2].set_xticks([])
axs[1, 2].set_yticks([])
plt.tight_layout()
return fig
# =========================================================
# UI
# =========================================================
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# 🧠 Kernel Matrix Visualization
Upload image and visualize:
- Grayscale Conversion
- Convolution Kernels
- Edge Detection
- Histogram
- Kernel Matrix
"""
)
with gr.Row():
input_image = gr.Image(
type="numpy",
label="Upload Image"
)
kernel_dropdown = gr.Dropdown(
choices=list(kernels.keys()),
value="Edge Detection",
label="Select Kernel"
)
output_plot = gr.Plot()
# AUTO RUN
input_image.change(
fn=process_image,
inputs=[input_image, kernel_dropdown],
outputs=output_plot
)
kernel_dropdown.change(
fn=process_image,
inputs=[input_image, kernel_dropdown],
outputs=output_plot
)
demo.launch() |