File size: 2,310 Bytes
b97eca5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()