import cv2
import os
import numpy as np

# Path folder gambar
image_folder = 'D:/Skripsi/ekstraksi fitur/Streptococcus Pneumoniae'

# Cek apakah folder gambar ada
if not os.path.isdir(image_folder):
    print(f"Folder {image_folder} tidak ditemukan.")
    exit()

# Jumlah gambar dalam folder
image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg')]
num_images = len(image_files)

# Baris citra
row = 1
for i in range(num_images):
    file_name = f'{image_folder}/{image_files[i]}'

    # Preprocessing
    img = cv2.imread(file_name, 1)
    if img is None:
        print(f"Gagal membaca file: {file_name}")
        continue

    blue, green, red = cv2.split(img)

    # Thresholding
    ret, img1 = cv2.threshold(green, 110, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    img1 = cv2.erode(img1.copy(),None, iterations=1)
    img1 = cv2.dilate(img1.copy(),None, iterations=1)

    # Display hasilnya
    cv2.imshow("Original", img1)
    cv2.waitKey(800)
    cv2.destroyAllWindows()