forked from MuhammeedAlaa/OMReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemRemoval.py
75 lines (64 loc) · 2.49 KB
/
stemRemoval.py
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
import numpy as np
from skimage.draw import line
from itertools import groupby
from commonfunctions import rle
def stemRemoval(img, staffLineSpacing):
outputImg = np.copy(img)
height, width = outputImg.shape
stemsPositions, stemsWidths = StemDetection(img, staffLineSpacing)
# margin of error of the stem width as it may have some small thick part
stemWidthErrorMargin = 1.5
maxStemWidth = int(max(stemsWidths, default=0)*stemWidthErrorMargin)
for stem in stemsPositions:
for y in range(0, height, 1):
if outputImg[y, stem] != 0:
for j in range(1, maxStemWidth//2):
if stem + j < width and outputImg[y, stem + j] == 0:
stem = stem + j
break
if stem - j > -1 and outputImg[y, stem - j] == 0:
stem = stem - j
break
horizontalThresholdResult = testHorizontalThreshold(
outputImg, stem, y, maxStemWidth)
if(horizontalThresholdResult[0]):
rr, cc = line(
y, horizontalThresholdResult[1], y, horizontalThresholdResult[2])
outputImg[rr, cc] = 255
return outputImg, list(zip(stemsPositions, stemsWidths))
# returns the run lengths of the ones in the input array
def runs_of_ones_list(bits):
return [sum(g) for b, g in groupby(bits) if b]
def StemDetection(img, staffLineSpacing):
height, width = img.shape
maxVertical = 7 * staffLineSpacing / 2
RLEImg = (255 - np.copy(img))/255
candidateStemsPos = []
stemsWidths = []
stemsPositions = []
for i in range(width):
runlengths, startpositions, values = rle(RLEImg[:, i])
objectRuns = runlengths[np.nonzero(values)[0]]
if any(y >= maxVertical for y in objectRuns):
if len(candidateStemsPos) > 0 and candidateStemsPos[-1] + 1 == i:
stemsWidths[-1] += 1
else:
stemsWidths.append(1)
stemsPositions.append(i)
candidateStemsPos.append(i)
return (stemsPositions, stemsWidths)
def testHorizontalThreshold(img, x, y, threshold):
width = img.shape[1]
leftX = x
rightX = x
while (leftX > 0):
if (img[y, leftX-1] == 0):
leftX -= 1
else:
break
while (rightX < width-1):
if (img[y, rightX+1] == 0):
rightX += 1
else:
break
return (rightX - leftX <= threshold), leftX, rightX