File size: 4,372 Bytes
3d85c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70115bb
3d85c80
 
70115bb
3d85c80
 
 
 
 
 
70115bb
 
 
 
3d85c80
 
 
70115bb
3d85c80
70115bb
 
3d85c80
70115bb
3d85c80
 
70115bb
 
 
3d85c80
70115bb
3d85c80
 
 
 
70115bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d85c80
 
 
70115bb
 
 
 
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
import streamlit as st
import cv2
import numpy as np
from PIL import Image

# Título do aplicativo
st.title("Analisador de Imagens Médicas com OpenCV")
st.subheader("Carregue uma imagem médica para análise e melhoria.")

# Carregar a imagem
uploaded_file = st.file_uploader("Envie a imagem médica (formatos aceitos: .jpg, .png, .jpeg)", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    # Carregar a imagem com OpenCV
    image = Image.open(uploaded_file)
    image = np.array(image)

    # Mostrar a imagem original
    st.image(image, caption="Imagem Original", use_column_width=True)

    # Opções de processamento
    st.sidebar.header("Opções de Processamento")
    process_option = st.sidebar.selectbox("Escolha o processamento:", 
                                          ["Conversão para Escala de Cinza", 
                                           "Filtro de Borrão (Blur)", 
                                           "Detecção de Bordas (Canny)", 
                                           "Equalização de Histograma", 
                                           "Ajuste de Brilho e Contraste", 
                                           "Detecção de Contornos"])

    if process_option == "Conversão para Escala de Cinza":
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        st.image(gray_image, caption="Imagem em Escala de Cinza", use_column_width=True, channels="GRAY")

    elif process_option == "Filtro de Borrão (Blur)":
        ksize = st.sidebar.slider("Tamanho do kernel de desfoque:", min_value=3, max_value=15, step=2, value=5)
        blur_image = cv2.GaussianBlur(image, (ksize, ksize), 0)
        st.image(blur_image, caption="Imagem com Filtro de Borrão", use_column_width=True)

    elif process_option == "Detecção de Bordas (Canny)":
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        threshold1 = st.sidebar.slider("Threshold 1:", min_value=50, max_value=150, value=100)
        threshold2 = st.sidebar.slider("Threshold 2:", min_value=100, max_value=300, value=200)
        edges = cv2.Canny(gray_image, threshold1, threshold2)
        st.image(edges, caption="Bordas Detectadas", use_column_width=True, channels="GRAY")

    elif process_option == "Equalização de Histograma":
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        equalized_image = cv2.equalizeHist(gray_image)
        st.image(equalized_image, caption="Imagem com Histograma Equalizado", use_column_width=True, channels="GRAY")

    elif process_option == "Ajuste de Brilho e Contraste":
        brightness = st.sidebar.slider("Brilho:", min_value=-100, max_value=100, value=0)
        contrast = st.sidebar.slider("Contraste:", min_value=-100, max_value=100, value=0)
        adjusted_image = cv2.convertScaleAbs(image, alpha=1 + (contrast / 100), beta=brightness)
        st.image(adjusted_image, caption="Imagem com Ajuste de Brilho e Contraste", use_column_width=True)

    elif process_option == "Detecção de Contornos":
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        threshold = st.sidebar.slider("Threshold para Contornos:", min_value=50, max_value=255, value=127)
        _, thresh = cv2.threshold(gray_image, threshold, 255, cv2.THRESH_BINARY)
        contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contour_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 2)
        st.image(contour_image, caption="Contornos Detectados", use_column_width=True)

    # Exibição do histograma
    st.sidebar.header("Exibir Histograma")
    if st.sidebar.checkbox("Mostrar histograma da imagem"):
        if len(image.shape) == 3:
            colors = ("b", "g", "r")
            for i, color in enumerate(colors):
                hist = cv2.calcHist([image], [i], None, [256], [0, 256])
                st.line_chart(hist[:, 0])
        else:
            hist = cv2.calcHist([image], [0], None, [256], [0, 256])
            st.line_chart(hist[:, 0])

    # Botão para salvar o resultado
    if st.button("Salvar Imagem Processada"):
        result_path = "imagem_processada.png"
        cv2.imwrite(result_path, locals()[process_option.lower().replace(" ", "_") + "_image"])
        st.success(f"Imagem salva como {result_path}")
else:
    st.info("Por favor, envie uma imagem médica para começar.")