-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
157 lines (134 loc) · 6.84 KB
/
app.py
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
import streamlit as st
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageOps
import os
from KMean import KMeans
import meanShift_optimal as MsO
import spectral_threshold as spct
from skimage.io import imread
import Region_Growing as RG
import rgb2luv as luv
import Thresholding as thresh
import agglomerative as agg
option=''
path='images'
st.set_page_config(page_title="Image segmentation",layout="wide")
with open('style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
with st.sidebar:
st.title('Upload an image')
uploaded_file = st.file_uploader("", accept_multiple_files=False, type=['jpg','png','jpeg','webp'])
if uploaded_file is not None:
image = Image.open(uploaded_file)
plt.imread(uploaded_file)
image_path1=os.path.join(path,uploaded_file.name)
st.title("Options")
option = st.selectbox("",["Segmentation using K-means","Segmentation using mean shift","Optimized Thresholding","Spectral Thresholding","Region Growing","RGB to LUV","Manual & Otsu's Thresholding","Agglomerative Segmentation"])
if option == "Segmentation using K-means":
max_iter = st.slider(label="Max number of iterations",min_value=1, max_value=100, step=2)
k = st.slider(label="clusters",min_value=1, max_value=5, step=1)
if option == "Segmentation using mean shift":
max_iter = st.slider(label="Max number of iterations",min_value=50, max_value=1000, step=50)
if option == "Optimized Thresholding":
option1 = st.selectbox("",["Global","Local"])
elif option == "Agglomerative Segmentation":
no_of_clusters = st.slider(label="Number of output clusters",min_value=1,max_value=30,step=1,value=4)
ini_no_of_clusters = st.slider(label="Initial number of clusters", min_value=30,max_value=256,step=1,value=30)
input_img, resulted_img = st.columns(2)
with input_img:
if uploaded_file is not None:
st.title("Input images")
image = Image.open(uploaded_file)
st.image(uploaded_file)
# if (option == 'normalized cross correlations') or (option == 'Sum of Square Difference') or (option == 'Apply SIFT'):
# if second_image is not None:
# image2 = Image.open(second_image)
# st.image(second_image)
with resulted_img:
st.title("output image")
if option == 'Segmentation using K-means':
if uploaded_file is not None:
image=cv2.imread(image_path1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #Convert to RGB
X = image.reshape((-1,3)) #Reshape to (Npts, Ndim = 3)
X = np.float32(X)
km = KMeans(n_clus = k)
km.fit(X,200)
centers = km.getCentroids()
clusters = km.getClusters()
segmented_image = centers[clusters]
segmented_image = segmented_image.reshape((image.shape))
plt.imshow((segmented_image).astype(np.uint8))
# plt.tick_params(labelleft=False, labelbottom=False, labelright=False, labeltop=False)
plt.axis("off")
plt.show()
plt.savefig('./images/output/kmean.jpg')
st.image('./images/output/kmean.jpg')
if option == 'Segmentation using mean shift':
if uploaded_file is not None:
# st.title("output image")
MsO.mean_shift_method(image_path1,max_iter)
st.image('./images/output/mean_shift_out.png')
if option == 'Optimized Thresholding':
if uploaded_file is not None:
out_img = MsO.Optimized_Thresholding(image_path1,option1)
st.image(out_img)
if option == "Spectral Thresholding":
if uploaded_file is not None:
image_ss = cv2.imread(image_path1,0)
resulted_image = spct.spectral_threshold(image_ss)
st.image(resulted_image)
if option == "RGB to LUV":
if uploaded_file is not None:
image_ss = imread(image_path1)
resulted_image = luv.rgb_to_luv_man(image_ss)
st.image(resulted_image)
if option == 'Region Growing':
if uploaded_file is not None:
image = Image.open(uploaded_file)
img = np.array(image)
mask = RG.RegionGrowing(image_path1, (100, 100))
segmented_image_rg =cv2.bitwise_and(img,img, mask=mask)
segmented_image_rg = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
st.image(segmented_image_rg,width=450)
elif option == "Agglomerative Segmentation":
if uploaded_file is not None:
image_ss = cv2.imread(image_path1)
resulted_image = agg.apply_agglomerative_clustering(image_ss, no_of_clusters, ini_no_of_clusters)
st.image(resulted_image)
#---------------------------------Thresholding------------------------------------------
if option == "Manual & Otsu's Thresholding":
if uploaded_file is not None:
threshold = 0; threshold_1 = 0; threshold_2 = 0; threshold_3 = 0; threshold_4 = 0
thresh_mode = 0
image1=cv2.imread(image_path1)
with st.sidebar:
edge_detect = st.selectbox("Thresholding Technique", ["Manual Thresholding", "Otsu's Thresholding"])
if edge_detect == "Manual Thresholding":
thresh_mode = 0
elif edge_detect == "Otsu's Thresholding":
thresh_mode = 1
threshold_type = st.radio("Thresholding Type", ["Local", "Global"], horizontal=True)
if threshold_type == "Local":
if thresh_mode == 0:
threshold_1 = st.slider(label="Threshold Value 1", min_value=0, max_value=255, step=1)
threshold_2 = st.slider(label="Threshold Value 2", min_value=0, max_value=255, step=1)
threshold_3 = st.slider(label="Threshold Value 3", min_value=0, max_value=255, step=1)
threshold_4 = st.slider(label="Threshold Value 4", min_value=0, max_value=255, step=1)
elif threshold_type == "Global":
if thresh_mode == 0:
threshold =st.slider(label="Threshold Value", min_value=0, max_value=255, step=1)
with input_img:
image = Image.open(uploaded_file)
# st.image(uploaded_file)
with resulted_img:
if threshold_type == "Local":
# st.title("Output image")
local_type = thresh.local_thresholding(image1, threshold_1, threshold_2, threshold_3, threshold_4, thresh_mode)
st.image(local_type)
elif threshold_type == "Global":
# st.title("Output image")
global_type = thresh.global_thresholding(image1, threshold, thresh_mode)
st.image(global_type)