-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (90 loc) · 3.8 KB
/
app.py
File metadata and controls
119 lines (90 loc) · 3.8 KB
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
import os
import cv2
import numpy as np
import streamlit as st
import tempfile
class BinaryThresholdingApp:
"""
A Streamlit application for image thresholding using streamlit.
Attributes:
title (str): Title of the Streamlit application
threshold_range (tuple): Range for threshold slider [min, max]
"""
def __init__(self):
"""Initialize the application with Streamlit configuration."""
st.set_page_config(
page_title="Image Thresholding Tool",
layout="wide"
)
self.title = "Image Binary Thresholding Tool: Segment Foreground from Background"
self.threshold_range = (0, 255)
def _show_title(self):
"""Display the application title with explanatory text."""
st.title(self.title)
st.markdown("""
### 🧠 Threshold Value Explanation
- **Higher Values**: Smaller, less detailed foreground
- **Lower Values**: Larger, more detailed foreground
Adjust the slider below to experiment with different threshold values.
⚠️ The original image is displayed as-is. Thresholding only affects pixel intensities.
""")
def _process_upload(self):
"""Handle image upload and temporary file management."""
uploaded_file = st.file_uploader(
"Upload an image...", type=["jpg", "jpeg", "png"]
)
if uploaded_file is None:
return None
# Create temporary file for image processing
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
temp_filename = temp_file.name
temp_file.write(uploaded_file.getvalue())
return temp_filename
def _apply_thresholding(self, image_path, threshold_value):
"""
Apply binary thresholding to an image.
Args:
image_path (str): Path to the input image file
threshold_value (int): Threshold value for binarization
Returns:
numpy.ndarray: Thresholded image as a NumPy array
"""
# Read the image in BGR format (OpenCV's default)
img = cv2.imread(image_path)
if img is None:
st.error("Invalid image format. Please upload a JPG or PNG.")
return None
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply thresholding
_, thresh = cv2.threshold(
gray, threshold_value, 255, cv2.THRESH_BINARY)
return thresh
def main(self):
"""Run the Streamlit application logic."""
self._show_title()
temp_filename = self._process_upload()
if temp_filename:
threshold_value = st.slider(
"Threshold Value",
min_value=self.threshold_range[0],
max_value=self.threshold_range[1],
value=127
)
thresholded_image = self._apply_thresholding(
temp_filename, threshold_value)
if thresholded_image is not None:
# Display original image (converted to RGB)
rgb_original = cv2.cvtColor(
cv2.imread(temp_filename), cv2.COLOR_BGR2RGB)
st.image(rgb_original, caption="Original Image",
use_container_width=True)
# Display thresholded image (already grayscale)
st.image(thresholded_image, caption="Thresholded Image",
use_container_width=True)
# Optional: Save thresholded image (in BGR format)
cv2.imwrite("thresholded_image.jpg", thresholded_image)
os.unlink(temp_filename)
if __name__ == "__main__":
app = BinaryThresholdingApp()
app.main() # Call the `main()` function to start the Streamlit application