-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidate.py
More file actions
112 lines (98 loc) · 3.58 KB
/
Validate.py
File metadata and controls
112 lines (98 loc) · 3.58 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import cv2
import numpy as np
import imutils
threshold = 60 # 60 for optimal results
class Detection:
def __init__(self):
self.image = cv2.imread('Testcases/Test3.png')
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
(self.h, self.w) = self.getDimensions(self.image)
self.errors = list()
def validate(self):
self.pixelDropout()
if (len(self.errors) > 0):
for e in self.errors:
self.lineDropout(e[2], e[1])
def getDimensions(self, image):
(h, w) = image.shape[:2]
return (h, w)
def adjustResolution(self):
self.image = imutils.resize(self.image, width=int(self.w))
self.image = imutils.resize(self.image, height=int(self.h))
def getNeighbors(self, x, y):
ops = [-1, 0, +1]
pixels = list()
for opy in ops:
for opx in ops:
if (opy == y and opx == x):
continue
try:
pixels.append(self.image[x + opx][y + opy])
except:
pass
return pixels
def pixelDropout(self):
data = self.image
for r in range(self.w):
for c in range(self.h):
if (r == 0 or c == 0 or r == self.h-1 or c == self.w-1):
continue
try:
pixel = data[r, c]
neighbors = self.getNeighbors(r, c)
average = sum(neighbors)/len(neighbors)
except:
pixel = average
if (abs(average - pixel) > threshold):
self.errors.append(['Pixel Dropout', r, c])
def lineDropout(self, r, c):
data = self.image
(einl, einc) = (0, 0)
for i in range(self.w):
try:
neighbors = self.getNeighbors(r, i)
average = sum(neighbors)/len(neighbors)
except:
continue
if (abs(average - data[r, i]) > threshold):
einl += 1 # Error in Line
for i in range(self.h):
try:
neighbors = self.getNeighbors(i, c)
average = sum(neighbors)/len(neighbors)
except:
continue
if (abs(average - data[i, c]) > threshold):
einc += 1 # Error in Column
if (einl != 0 or einc != 0):
self.replaceErrors(einl, einc, r, c)
def replaceErrors(self, line, coln, r, c):
temp = list()
if (line/self.w > 0.75):
for i, e in enumerate(verify.errors):
if e[1] != r: # Eliminate close errors
if e[1] != r-1:
if e[1] != r+1:
temp.append(e)
verify.errors = temp
self.errors.append(['Line Dropout', c, None])
if (coln/self.h > 0.75):
for i, e in enumerate(verify.errors):
if e[2] != c: # Eliminate close errors
if e[2] != c-1:
if e[2] != c+1:
temp.append(e)
verify.errors = temp
self.errors.append(['Column Dropout', None, r])
del temp
def printErrors(self):
for i in self.errors:
print(i)
verify = Detection()
verify.adjustResolution()
verify.validate()
verify.printErrors()
cv2.imshow('Image', verify.image)
cv2.imwrite("Result.png", verify.image)
cv2.waitKey(0)
cv2.destroyAllWindows()