forked from Deevoluation/ALPR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocess.py
More file actions
85 lines (61 loc) · 3.67 KB
/
Preprocess.py
File metadata and controls
85 lines (61 loc) · 3.67 KB
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
# Preprocess.py
import cv2
import numpy as np
import math
# module level variables ##########################################################################
GAUSSIAN_SMOOTH_FILTER_SIZE = (5, 5)
ADAPTIVE_THRESH_BLOCK_SIZE = 19
ADAPTIVE_THRESH_WEIGHT = 9
###################################################################################################
def preprocess(imgOriginal,save_intermediate,output_folder,showSteps):
imgGrayscale = extractValue(imgOriginal) # We get the gray scale of the image.
#imgGrayscale = cv2.equalizeHist(imgGrayscale)
if showSteps == True:
print('Now we are in the Process of Preprocessing the image\n\tThe image to Grayscale :')
cv2.imshow('PreProcess',imgGrayscale)
cv2.waitKey(0)
if save_intermediate:
cv2.imwrite('%s/GrayScale.png'%(output_folder),imgGrayscale)
imgMaxContrastGrayscale = maximizeContrast(imgGrayscale) # contrast is the difference between light and dark in an image. High contrast images will have bright highlights and dark shadows,bold colours, and show texture in the subject. Low contrast images will have a narrow range of tones and might therefore feel flat or dull
if showSteps == True:
print('\tThe image to High Contrast:')
cv2.imshow('PreProcess',imgMaxContrastGrayscale)
cv2.waitKey(0)
if save_intermediate:
cv2.imwrite('%s/Contrsat.png'%(output_folder),imgMaxContrastGrayscale)
height,width = imgGrayscale.shape
imgBlurred = np.zeros((height, width, 1), np.uint8)
imgBlurred = cv2.GaussianBlur(imgMaxContrastGrayscale, GAUSSIAN_SMOOTH_FILTER_SIZE, 0) # 2nd parameter is (height,width) of Gaussian kernel,3rd parameter is sigmaX,4th parameter is sigmaY(as not specified it is made same as sigmaX).
if showSteps == True:
print('\tThe image to Blurred:')
cv2.imshow('PreProcess',imgBlurred)
cv2.waitKey(0)
if save_intermediate:
cv2.imwrite('%s/blurred.png'%(output_folder),imgBlurred)
imgThresh = cv2.adaptiveThreshold(imgBlurred, 255.0, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, ADAPTIVE_THRESH_BLOCK_SIZE, ADAPTIVE_THRESH_WEIGHT)
if showSteps == True:
print('\tThe image thresholded:')
cv2.imshow('PreProcess',imgThresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
if save_intermediate:
cv2.imwrite('%s/thresholded.png'%(output_folder),imgThresh)
return imgGrayscale, imgThresh
###################################################################################################
def extractValue(imgOriginal):
height, width, numChannels = imgOriginal.shape
imgHSV = np.zeros((height, width, 3), np.uint8)
imgHSV = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV)
imgHue, imgSaturation, imgValue = cv2.split(imgHSV)
return imgValue
###################################################################################################
def maximizeContrast(imgGrayscale):
height, width = imgGrayscale.shape
imgTopHat = np.zeros((height, width, 1), np.uint8)
imgBlackHat = np.zeros((height, width, 1), np.uint8)
structuringElement = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # Same as np.ones((3,3)
imgTopHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_TOPHAT, structuringElement) # It is difference of input image and Opening of the image
imgBlackHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_BLACKHAT, structuringElement) # it is difference of closing of the input image and input image.
imgGrayscalePlusTopHat = cv2.add(imgGrayscale, imgTopHat)
imgGrayscalePlusTopHatMinusBlackHat = cv2.subtract(imgGrayscalePlusTopHat, imgBlackHat)
return imgGrayscalePlusTopHatMinusBlackHat