Spaces:
Sleeping
Sleeping
Upload lab3_cs406.py
Browse files- lab3_cs406.py +79 -0
lab3_cs406.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
def load_image():
|
8 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
9 |
+
if uploaded_file is not None:
|
10 |
+
image_bytes = uploaded_file.read()
|
11 |
+
opencv_image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), 1)
|
12 |
+
return opencv_image
|
13 |
+
return None
|
14 |
+
|
15 |
+
def denoise(image):
|
16 |
+
return cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
|
17 |
+
|
18 |
+
def sharpen(image):
|
19 |
+
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
20 |
+
return cv2.filter2D(image, -1, kernel)
|
21 |
+
|
22 |
+
def edge_detection(image):
|
23 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
24 |
+
|
25 |
+
sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 1, ksize=5)
|
26 |
+
sobel = np.uint8(np.absolute(sobel))
|
27 |
+
|
28 |
+
prewitt_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
|
29 |
+
prewitt_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
|
30 |
+
prewitt = np.sqrt(prewitt_x**2 + prewitt_y**2)
|
31 |
+
prewitt = np.uint8(prewitt)
|
32 |
+
|
33 |
+
canny = cv2.Canny(gray, 100, 200)
|
34 |
+
|
35 |
+
return sobel, prewitt, canny
|
36 |
+
|
37 |
+
def main():
|
38 |
+
st.title("Image Enhancement App")
|
39 |
+
|
40 |
+
image = load_image()
|
41 |
+
|
42 |
+
if image is not None:
|
43 |
+
st.subheader("Original Image")
|
44 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
45 |
+
|
46 |
+
st.subheader("Enhanced Images")
|
47 |
+
|
48 |
+
col1, col2 = st.columns(2)
|
49 |
+
|
50 |
+
with col1:
|
51 |
+
st.write("Denoised Image")
|
52 |
+
denoised = denoise(image)
|
53 |
+
st.image(cv2.cvtColor(denoised, cv2.COLOR_BGR2RGB))
|
54 |
+
|
55 |
+
with col2:
|
56 |
+
st.write("Sharpened Image")
|
57 |
+
sharpened = sharpen(denoised)
|
58 |
+
st.image(cv2.cvtColor(sharpened, cv2.COLOR_BGR2RGB))
|
59 |
+
|
60 |
+
st.subheader("Edge Detection")
|
61 |
+
|
62 |
+
sobel, prewitt, canny = edge_detection(sharpened)
|
63 |
+
|
64 |
+
col1, col2, col3 = st.columns(3)
|
65 |
+
|
66 |
+
with col1:
|
67 |
+
st.write("Sobel Edge Detection")
|
68 |
+
st.image(sobel, use_column_width=True)
|
69 |
+
|
70 |
+
with col2:
|
71 |
+
st.write("Prewitt Edge Detection")
|
72 |
+
st.image(prewitt, use_column_width=True)
|
73 |
+
|
74 |
+
with col3:
|
75 |
+
st.write("Canny Edge Detection")
|
76 |
+
st.image(canny, use_column_width=True)
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
main()
|