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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from skimage.filters import threshold_sauvola
|
| 5 |
+
from skimage.morphology import opening, closing, disk
|
| 6 |
+
|
| 7 |
+
def preprocess_image(image):
|
| 8 |
+
"""
|
| 9 |
+
Görüntüyü gri tonlamaya dönüştürür ve gürültüyü azaltmak için
|
| 10 |
+
Gauss bulanıklığı uygular.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
image: İşlenecek renkli görüntü.
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
Gri tonlamalı ve bulanıklaştırılmış görüntü.
|
| 17 |
+
"""
|
| 18 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 19 |
+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 20 |
+
return blur
|
| 21 |
+
|
| 22 |
+
def sauvola_thresholding(image):
|
| 23 |
+
"""
|
| 24 |
+
Görüntüye Sauvola eşikleme algoritmasını [1] uygular ve
|
| 25 |
+
negatifini alır.
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
image: Eşiklenecek gri tonlamalı görüntü.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
Eşiklenmiş ikili görüntü.
|
| 32 |
+
|
| 33 |
+
Referanslar:
|
| 34 |
+
[1] Sauvola, J., and Pietikäinen, M. (2000). Adaptive document image
|
| 35 |
+
binarization. Pattern recognition, 33(2), 225-236.
|
| 36 |
+
"""
|
| 37 |
+
thresh_sauvola = threshold_sauvola(image, window_size=25)
|
| 38 |
+
binary_sauvola = image > thresh_sauvola
|
| 39 |
+
binary_sauvola = np.invert(binary_sauvola)
|
| 40 |
+
return (binary_sauvola.astype(np.uint8) * 255).astype(np.uint8)
|
| 41 |
+
|
| 42 |
+
def morphological_operations(image):
|
| 43 |
+
"""
|
| 44 |
+
Görüntüye morfolojik açma ve kapama işlemlerini [2] uygular.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
image: İşlenecek ikili görüntü.
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
Açma ve kapama işlemleri uygulanmış görüntü.
|
| 51 |
+
|
| 52 |
+
Referanslar:
|
| 53 |
+
[2] Gonzalez, R. C., Woods, R. E., and Eddins, S. L. (2004). Digital
|
| 54 |
+
image processing using MATLAB. Pearson Education India.
|
| 55 |
+
"""
|
| 56 |
+
selem = disk(2) # Yapılandırıcı eleman
|
| 57 |
+
opened = opening(image, selem)
|
| 58 |
+
closed = closing(opened, selem)
|
| 59 |
+
return closed
|
| 60 |
+
|
| 61 |
+
def detect_defect(image, area_threshold=50):
|
| 62 |
+
"""
|
| 63 |
+
Kaynak dikişindeki kusurları tespit etmek için kontur analizi [3]
|
| 64 |
+
kullanır ve kontur alanına göre filtreleme yapar.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
image: Kusur tespiti yapılacak ikili görüntü.
|
| 68 |
+
area_threshold: Kusur olarak kabul edilecek minimum kontur alanı.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
Kusur tespit sonucu ("Kusur tespit edildi" veya "Kusur tespit edilmedi")
|
| 72 |
+
ve tespit edilen kusurların konturları (varsa).
|
| 73 |
+
|
| 74 |
+
Referanslar:
|
| 75 |
+
[3] Suzuki, S. (1985). Topological structural analysis of digitized
|
| 76 |
+
binary images by border following. Computer vision, graphics, and
|
| 77 |
+
image processing, 30(1), 32-46.
|
| 78 |
+
"""
|
| 79 |
+
contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 80 |
+
defects = [cnt for cnt in contours if cv2.contourArea(cnt) > area_threshold]
|
| 81 |
+
if len(defects) > 0:
|
| 82 |
+
return "Kusur tespit edildi", defects
|
| 83 |
+
return "Kusur tespit edilmedi", None
|
| 84 |
+
|
| 85 |
+
def threshold_yenileri(image, threshold_method="sauvola"):
|
| 86 |
+
"""
|
| 87 |
+
Farklı eşikleme yöntemlerini uygular.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
image: Eşiklenecek gri tonlamalı görüntü.
|
| 91 |
+
threshold_method: Kullanılacak eşikleme yöntemi ("sauvola", "adaptive" veya "sauvola_adaptive").
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Eşiklenmiş ikili görüntü.
|
| 95 |
+
"""
|
| 96 |
+
if threshold_method == "sauvola":
|
| 97 |
+
return sauvola_thresholding(image)
|
| 98 |
+
elif threshold_method == "adaptive":
|
| 99 |
+
thresholded = cv2.adaptiveThreshold(
|
| 100 |
+
image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
|
| 101 |
+
)
|
| 102 |
+
return cv2.bitwise_not(thresholded)
|
| 103 |
+
elif threshold_method == "sauvola_adaptive":
|
| 104 |
+
# Sauvola ile eşikleme yap
|
| 105 |
+
sauvola_thresh = sauvola_thresholding(image)
|
| 106 |
+
# Adaptive eşikleme yap ve tersini al
|
| 107 |
+
adaptive_thresh = cv2.adaptiveThreshold(
|
| 108 |
+
image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
|
| 109 |
+
)
|
| 110 |
+
adaptive_thresh = cv2.bitwise_not(adaptive_thresh)
|
| 111 |
+
# İki sonucu birleştir (mantıksal VEYA işlemi)
|
| 112 |
+
combined_thresh = cv2.bitwise_or(sauvola_thresh, adaptive_thresh)
|
| 113 |
+
return combined_thresh
|
| 114 |
+
else:
|
| 115 |
+
raise ValueError("Geçersiz eşikleme yöntemi.")
|
| 116 |
+
|
| 117 |
+
def predict(image, area_threshold=50, threshold_method="sauvola"):
|
| 118 |
+
"""
|
| 119 |
+
Verilen görüntünün kaynak dikişindeki kusurları tahmin eder.
|
| 120 |
+
|
| 121 |
+
Args:
|
| 122 |
+
image: İşlenecek kaynak görüntüsü.
|
| 123 |
+
area_threshold: Kusur olarak kabul edilecek minimum kontur alanı.
|
| 124 |
+
threshold_method: Kullanılacak eşikleme yöntemi ("sauvola", "adaptive" veya "sauvola_adaptive").
|
| 125 |
+
|
| 126 |
+
Returns:
|
| 127 |
+
İşlem çıktıları (gri tonlamalı, eşiklenmiş, bölümlenmiş ve kusurlar
|
| 128 |
+
vurgulanmış görüntüler) ve kusur tespit sonucu.
|
| 129 |
+
"""
|
| 130 |
+
processed_image = preprocess_image(image)
|
| 131 |
+
thresholded_image = threshold_yenileri(processed_image, threshold_method)
|
| 132 |
+
segmented_image = morphological_operations(thresholded_image)
|
| 133 |
+
result, defects = detect_defect(segmented_image, area_threshold)
|
| 134 |
+
|
| 135 |
+
if defects is not None:
|
| 136 |
+
for defect in defects:
|
| 137 |
+
cv2.drawContours(image, [defect], -1, (0, 0, 255), 2)
|
| 138 |
+
|
| 139 |
+
image_outputs = [
|
| 140 |
+
(processed_image, "Gri Tonlamalı Görüntü"),
|
| 141 |
+
(thresholded_image, "Eşiklenmiş Görüntü"),
|
| 142 |
+
(segmented_image, "Açma ve Kapama ile Bölümlenmiş Görüntü"),
|
| 143 |
+
(image, "Kusurlar Vurgulanmış Görüntü"),
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
return image_outputs, result
|
| 147 |
+
|
| 148 |
+
# Gradio arayüzü
|
| 149 |
+
demo = gr.Interface(
|
| 150 |
+
fn=predict,
|
| 151 |
+
inputs=[
|
| 152 |
+
gr.Image(type="numpy"),
|
| 153 |
+
gr.Slider(
|
| 154 |
+
minimum=10,
|
| 155 |
+
maximum=500,
|
| 156 |
+
value=50,
|
| 157 |
+
step=10,
|
| 158 |
+
label="Kontur Alan Eşiği",
|
| 159 |
+
),
|
| 160 |
+
gr.Dropdown(
|
| 161 |
+
choices=["sauvola", "adaptive", "sauvola_adaptive"],
|
| 162 |
+
value="sauvola",
|
| 163 |
+
label="Eşikleme Yöntemi",
|
| 164 |
+
),
|
| 165 |
+
],
|
| 166 |
+
outputs=[
|
| 167 |
+
gr.Gallery(
|
| 168 |
+
label="İşlem Çıktıları",
|
| 169 |
+
show_label=True,
|
| 170 |
+
elem_id="gallery",
|
| 171 |
+
columns=[1],
|
| 172 |
+
object_fit="contain",
|
| 173 |
+
height="auto",
|
| 174 |
+
),
|
| 175 |
+
gr.Textbox(label="Sonuç"),
|
| 176 |
+
],
|
| 177 |
+
examples=[
|
| 178 |
+
["W0001_0000.png", 50, "sauvola"],
|
| 179 |
+
["W0001_0001.png", 100, "adaptive"],
|
| 180 |
+
["W0001_0002.png", 75, "sauvola_adaptive"],
|
| 181 |
+
],
|
| 182 |
+
title="Kaynak Kusur Tespit Uygulaması",
|
| 183 |
+
description="Bir kaynak görüntüsü yükleyin ve kusur olup olmadığını kontrol edin.",
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
demo.launch()
|