File size: 3,604 Bytes
ed03306 b29dbd3 ed03306 b29dbd3 04fa94a ed03306 |
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 |
import streamlit as st
import cv2
import numpy as np
from PIL import Image
st.title("Image Processing\n(Comparison View)")
# Upload image in the sidebar
uploaded_file = st.sidebar.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
img_array = np.array(image)
# Select an operation using a dropdown
option = st.selectbox("Choose an comparison:", [
"None", "Convert to Grayscale", "Rotate Image", "Blur Image", "Convert to Color Space", "Edge Detection"
])
# Convert to Grayscale
if option == "Convert to Grayscale":
gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.image(gray_image, caption="Grayscale Image", use_container_width=True)
# Rotate Image
elif option == "Rotate Image":
angle = st.slider("Select Rotation Angle", -180, 180, 0)
(h, w) = img_array.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(img_array, matrix, (w, h))
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.image(rotated_image, caption=f"Rotated by {angle}°", use_container_width=True)
# Blur Image
elif option == "Blur Image":
blur_level = st.slider("Select Blur Level", 1, 20, 5)
kernel_size = (blur_level * 2 + 1, blur_level * 2 + 1) # Ensure it's always an odd number
blurred_image = cv2.GaussianBlur(img_array, kernel_size, 0)
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.image(blurred_image, caption=f"Blurred (Level {blur_level})", use_container_width=True)
# Convert to Color Space (Fixed BGR to RGB)
elif option == "Convert to Color Space":
color_space = st.selectbox("Choose a color space:", ["RGB", "BGR2RGB", "Grayscale"])
converted_image = None # Initialize to avoid `NoneType` errors
if color_space == "RGB":
converted_image = img_array # Already in RGB format
elif color_space == "BGR2RGB":
converted_image = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB) # Fixed BGR to RGB conversion
elif color_space == "Grayscale":
converted_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
if converted_image is not None:
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.image(converted_image, caption=f"{color_space} Image", use_container_width=True)
# Edge Detection (Canny)
elif option == "Edge Detection":
low_threshold = st.slider("Lower Threshold", 0, 255, 50)
high_threshold = st.slider("Upper Threshold", 0, 255, 150)
gray_image = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY) # Convert to grayscale first
edges = cv2.Canny(gray_image, low_threshold, high_threshold) # Apply Canny Edge Detection
col1, col2 = st.columns(2)
with col1:
st.image(image, caption="Original Image", use_container_width=True)
with col2:
st.image(edges, caption="Edge Detection (Canny)", use_container_width=True)
|