- 22 Haz 2020
- 30
- 22
Python:
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
def plaka_konum_don(img):
img_bgr = img
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# islem resmi ir_img
ir_img = cv2.medianBlur(img_gray, 5) # 5x5
ir_img = cv2.medianBlur(ir_img, 5) # 5x5
medyan = np.median(ir_img)
low = 0.67 * medyan
high = 1.33 * medyan
# Jonh f Canny
kenarlik = cv2.Canny(ir_img, low, high)
# np.ones((3,3),np.uint8) -->
kenarlik = cv2.dilate(kenarlik, np.ones((3, 3), np.uint8), iterations=1)
# cv2.RETR_TREE -> hiyeralsık
# CHAIN_APPROX_SIMPLE -> kosegenleri aldık, tum pıkseller yerine
cnt = cv2.findContours(kenarlik, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = cnt[0]
cnt = sorted(cnt, key=cv2.contourArea, reverse=True)
H, W = 500, 500
plaka = None
for c in cnt:
rect = cv2.minAreaRect(c) # dikdortgen yapıda al (1)
(x, y), (w, h), r = rect
if (w > h and w > h * 2) or (h > w and h > w * 2): # oran en az 2 (2)
box = cv2.boxPoints(rect) # [[12,13],[25,13],[20,13],[13,45]]
box = np.int64(box)
minx = np.min(box[:, 0])
miny = np.min(box[:, 1])
maxx = np.max(box[:, 0])
maxy = np.max(box[:, 1])
muh_plaka = img_gray[miny:maxy, minx:maxx].copy()
muh_medyan = np.median(muh_plaka)
kon1 = muh_medyan > 84 and muh_medyan < 200 # yogunluk kontrolu (3)
kon2 = h < 50 and w < 150 # sınır kontrolu (4)
kon3 = w < 50 and h < 150 # sınır kontrolu (4)
print(f"muh_plaka medyan:{muh_medyan} genislik: {w} yukseklik:{h}")
kon = False
if (kon1 and (kon2 or kon3)):
# plaka'dır
# cv2.drawContours(img,[box],0,(0,255,0),2)
plaka = [int(i) for i in [minx, miny, w, h]] # x,y,w,h
kon = True
else:
# plaka değidir
# cv2.drawContours(img,[box],0,(0,0,255),2)
pass
if (kon):
return plaka
return []
Python:
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
from alg1_plaka_tespit import plaka_konum_don
veriler = os.listdir("veriseti")
isim = veriler[3]
img = cv2.imread("veriseti/" + isim)
img = cv2.resize(img, (500, 500))
plaka = plaka_konum_don(img)
x, y, w, h = plaka
if (w > h):
plaka_bgr = img[y:y + h, x:x + w].copy()
else:
plaka_bgr = img[y:y + w, x:x + h].copy()
plt.imshow(plaka_bgr)
plt.show()
H, W = plaka_bgr.shape[:2]
print("orjinal boyut:", W, H)
H, W = H * 2, W * 2
print("yeni boyut:", W, H)
plaka_bgr = cv2.resize(plaka_bgr, (W, H))
plt.imshow(plaka_bgr)
plt.show()
# plaka_resim : islem resmimmiz
plaka_resim = cv2.cvtColor(plaka_bgr, cv2.COLOR_BGR2GRAY)
plt.title("gri format")
plt.imshow(plaka_resim, cmap="gray")
plt.show()
th_img = cv2.adaptiveThreshold(plaka_resim, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
plt.title("eşiklenmiş")
plt.imshow(th_img, cmap="gray")
plt.show()
kernel = np.ones((3, 3), np.uint8)
th_img = cv2.morphologyEx(th_img, cv2.MORPH_OPEN, kernel, iterations=1)
plt.title("Gürültü yok edilmiş")
plt.imshow(th_img, cmap="gray")
plt.show()
cnt = cv2.findContours(th_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = cnt[0]
cnt = sorted(cnt, key=cv2.contourArea, reverse=True)[:15]
for i, c in enumerate(cnt):
rect = cv2.minAreaRect(c)
(x, y), (w, h), r = rect
kon1 = max([w, h]) < W / 4
kon2 = w * h > 200
if (kon1 and kon2):
print("karakter ->", x, y, w, h)
box = cv2.boxPoints(rect)
box = np.int64(box)
# (15,20)
minx = np.min(box[:, 0])
miny = np.min(box[:, 1])
maxx = np.max(box[:, 0])
maxy = np.max(box[:, 1])
odak = 2
minx = max(0, minx - odak)
miny = max(0, miny - odak)
maxx = min(W, maxx + odak)
maxy = min(H, maxy + odak)
kesim = plaka_bgr[miny:maxy, minx:maxx].copy()
try:
cv2.imwrite(f"karakterseti/{isim}_{i}.jpg", kesim)
except:
pass
yaz = plaka_bgr.copy()
cv2.drawContours(yaz, [box], 0, (0, 255, 0), 1)
plt.imshow(yaz)
plt.show()
Python:
import os
import matplotlib.pyplot as plt
import cv2
from alg1_plaka_tespit import plaka_konum_don
#1. alg veri icleme
"""
veri = os.listdir("veriseti")
for image_url in veri:
img= cv2.imread("veriseti/"+image_url) # veriseti/1.jpg bgr degeri gönderiri
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.resize(img ,(500, 500))
plt.imshow(img)
plt.show()
"""
veri = os.listdir("veriseti")
for image_url in veri:
img= cv2.imread("veriseti/"+image_url) # veriseti/1.jpg bgr degeri gönderiri
img = cv2.resize(img ,(500, 500))
plaka = plaka_konum_don(img)
x,y,w,h = plaka
if(w>h):
plaka_bgr = img[y:y+h, x:x+w].copy()
else:
plaka_bgr = img[y:y + w, x:x + h].copy()
img = cv2.cvtColor(plaka_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()