lab3_cs406 / lab3_cs406.py
tin213's picture
Upload lab3_cs406.py
b97eca5 verified
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import io
def load_image():
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image_bytes = uploaded_file.read()
opencv_image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), 1)
return opencv_image
return None
def denoise(image):
return cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
def sharpen(image):
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
return cv2.filter2D(image, -1, kernel)
def edge_detection(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 1, ksize=5)
sobel = np.uint8(np.absolute(sobel))
prewitt_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
prewitt_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
prewitt = np.sqrt(prewitt_x**2 + prewitt_y**2)
prewitt = np.uint8(prewitt)
canny = cv2.Canny(gray, 100, 200)
return sobel, prewitt, canny
def main():
st.title("Image Enhancement App")
image = load_image()
if image is not None:
st.subheader("Original Image")
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
st.subheader("Enhanced Images")
col1, col2 = st.columns(2)
with col1:
st.write("Denoised Image")
denoised = denoise(image)
st.image(cv2.cvtColor(denoised, cv2.COLOR_BGR2RGB))
with col2:
st.write("Sharpened Image")
sharpened = sharpen(denoised)
st.image(cv2.cvtColor(sharpened, cv2.COLOR_BGR2RGB))
st.subheader("Edge Detection")
sobel, prewitt, canny = edge_detection(sharpened)
col1, col2, col3 = st.columns(3)
with col1:
st.write("Sobel Edge Detection")
st.image(sobel, use_column_width=True)
with col2:
st.write("Prewitt Edge Detection")
st.image(prewitt, use_column_width=True)
with col3:
st.write("Canny Edge Detection")
st.image(canny, use_column_width=True)
if __name__ == "__main__":
main()