Commit 1483eda2 authored by Gijs van der Voort's avatar Gijs van der Voort

Omzetting van karakters naar zwart wit

parent 0ca94529
from copy import deepcopy
from GrayscaleImage import GrayscaleImage
from LetterCropper import LetterCropper
from GaussianFilter import GaussianFilter
from numpy import resize, array, append, vstack, hstack, zeros
class CharacterImageNormalizer:
def __init__(self, size=(60, 40), blur=1.1, crop_threshold=126):
self.size = size
self.gausse_filter = GaussianFilter(blur)
self.cropper = LetterCropper(crop_threshold)
def normalize(self, image):
self.gausse_filter.filter(image)
GrayscaleImage.resize(image, (60, 60 / image.height * image.width))
self.increase_contrast(image)
self.to_black_white(image, 30)
self.cropper.crop_to_letter(image)
self.add_padding(image)
def add_padding(self, image):
data = array(image.data)
top_bottom_padding = zeros((3, data.shape[1]))
data = vstack((top_bottom_padding, data))
data = vstack((data, top_bottom_padding))
left_right_padding = zeros((data.shape[0], 3))
data = hstack((left_right_padding, data))
data = hstack((data, left_right_padding))
extra_left_padding = zeros((data.shape[0], data.shape[0] - data.shape[1]))
data = hstack((extra_left_padding, data))
image.data = data
def increase_contrast(self, image):
image.data -= image.data.min()
image.data /= image.data.max() / 255.0
def to_black_white(self, image, bla):
for y, x, value in image:
image.data[y, x] = 255 if value < bla else 0
\ No newline at end of file
from Rectangle import Rectangle
import sys
class LetterCropper:
def __init__(self, threshold = 0.9):
def __init__(self, threshold = 0.5):
self.threshold = threshold
def crop_to_letter(self, image):
......@@ -17,7 +18,7 @@ class LetterCropper:
max_y = 0
for y, x, value in self.image:
if value < self.threshold:
if value > self.threshold:
if x < min_x: min_x = x
if y < min_y: min_y = y
if x > max_x: max_x = x
......@@ -26,6 +27,6 @@ class LetterCropper:
self.letter_bounds = Rectangle(
min_x,
min_y,
max_x - min_x ,
max_y - min_y
max_x - min_x + 1,
max_y - min_y + 1
)
......@@ -3,11 +3,12 @@ from xml.dom.minidom import parse
from Point import Point
from Character import Character
from GrayscaleImage import GrayscaleImage
from NormalizedCharacterImage import NormalizedCharacterImage
from CharacterImageNormalizer import CharacterImageNormalizer
class LicensePlate:
def __init__(self, folder_nr, file_nr):
self.character_normalizer = CharacterImageNormalizer(size=(60, 40), blur=1.1, crop_threshold=125)
filename = '%04d/00991_%04d%02d' % (folder_nr, folder_nr, file_nr)
self.dom = parse('../images/Infos/%s.info' % filename)
......@@ -89,6 +90,17 @@ class LicensePlate:
x2, y2 = corners[2].to_tuple()
x3, y3 = corners[3].to_tuple()
extra_space = 3
x0 -= extra_space
y0 -= extra_space
x1 += extra_space
y1 -= extra_space
x2 += extra_space
y2 += extra_space
x3 -= extra_space
y3 += extra_space
M = max(x0, x1, x2, x3) - min(x0, x1, x2, x3)
N = max(y0, y1, y2, y3) - min(y0, y1, y2, y3)
......@@ -193,7 +205,9 @@ class LicensePlate:
value = self.get_node("char", character).firstChild.data
corners = self.get_corners(character)
data = self.retrieve_data(corners)
image = NormalizedCharacterImage(data=data)
image = GrayscaleImage(data=data)
self.character_normalizer.normalize(image)
self.characters.append(Character(value, corners, image))
else:
......
from copy import deepcopy
from GrayscaleImage import GrayscaleImage
from LetterCropper import LetterCropper
from GaussianFilter import GaussianFilter
class NormalizedCharacterImage(GrayscaleImage):
def __init__(self, image=None, data=None, size=(60, 40), blur=1.1, crop_threshold=0.9):
if image != None:
GrayscaleImage.__init__(self, data=deepcopy(image.data))
elif data != None:
GrayscaleImage.__init__(self, data=deepcopy(data))
self.blur = blur
self.crop_threshold = crop_threshold
self.size = size
self.gausse_filter()
self.increase_contrast()
self.crop_to_letter()
self.resize()
def increase_contrast(self):
self.data -= self.data.min()
self.data /= self.data.max()
def gausse_filter(self):
filter = GaussianFilter(1.1)
filter.filter(self)
def crop_to_letter(self):
cropper = LetterCropper(0.9)
cropper.crop_to_letter(self)
def resize(self):
GrayscaleImage.resize(self, self.size)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment