From 9efc18e0eed88475c205abf9fc564ee0f5c73cb6 Mon Sep 17 00:00:00 2001 From: Samo Penic <samo.penic@gmail.com> Date: Fri, 16 Nov 2018 17:28:44 +0000 Subject: [PATCH] sid enhancement works --- aoiOcr.py | 1 sid_process.py | 83 +++++++++++++++++++++++++++++++++++++++++ .idea/sonarIssues.xml | 15 +++++++ Ocr.py | 5 ++ 4 files changed, 104 insertions(+), 0 deletions(-) diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml index c750c34..c014979 100644 --- a/.idea/sonarIssues.xml +++ b/.idea/sonarIssues.xml @@ -3,6 +3,16 @@ <component name="issues"> <option name="index"> <map> + <entry key="/Dummy.txt"> + <value> + <set /> + </value> + </entry> + <entry key="/Python Console"> + <value> + <set /> + </value> + </entry> <entry key="$PROJECT_DIR$/Ocr.py"> <value> <set /> @@ -13,6 +23,11 @@ <set /> </value> </entry> + <entry key="$PROJECT_DIR$/sid_process.py"> + <value> + <set /> + </value> + </entry> </map> </option> </component> diff --git a/Ocr.py b/Ocr.py index ee715f0..0da4497 100644 --- a/Ocr.py +++ b/Ocr.py @@ -1,4 +1,5 @@ from pyzbar.pyzbar import decode +from sid_process import enhanceSID import cv2 import numpy as np import math @@ -210,3 +211,7 @@ black = totpx - cv2.countNonZero(roi) oneline.append(black / totpx) self.answerMatrix.append(oneline) + + def get_enhanced_sid(self): + es= enhanceSID(self.img[int(0.04*self.imgHeight):int(0.08*self.imgHeight), int(0.7*self.imgWidth):int(0.99*self.imgWidth)]) + cv2.imwrite("enhancedSID.png",es) \ No newline at end of file diff --git a/aoiOcr.py b/aoiOcr.py index 25b2c47..c8bc97b 100644 --- a/aoiOcr.py +++ b/aoiOcr.py @@ -10,3 +10,4 @@ print(p.locateUpMarkers()) print(p.locateRightMarkers()) print(p.answerMatrix) +p.get_enhanced_sid() diff --git a/sid_process.py b/sid_process.py new file mode 100644 index 0000000..210cfe7 --- /dev/null +++ b/sid_process.py @@ -0,0 +1,83 @@ +import cv2 +import numpy as np +from skimage import morphology,img_as_ubyte + +""" + (1) The text is an array of chars (in row-major order) where + * each char can be one of the following: + * 'x': hit + * 'o': miss + * ' ': don't-care + * (2) When the origin falls on a hit or miss, use an upper case + * char (e.g., 'X' or 'O') to indicate it. When the origin + * falls on a don't-care, indicate this with a 'C'. + * The string must have exactly one origin specified. + * (3) The advantage of this method is that the text can be input + * in a format that shows the 2D layout of the Sel; e.g., + + + :::: AND :::: + + + (10) The sequence string is formatted as follows: + * ~ An arbitrary number of operations, each separated + * by a '+' character. White space is ignored. + * ~ Each operation begins with a case-independent character + * specifying the operation: + * d or D (dilation) + * e or E (erosion) + * o or O (opening) + * c or C (closing) + * r or R (rank binary reduction) + * x or X (replicative binary expansion) + * b or B (add a border of 0 pixels of this size) + * ~ The args to the morphological operations are bricks of hits, + * and are formatted as a.b, where a and b are horizontal and + * vertical dimensions, rsp. + * ~ The args to the reduction are a sequence of up to 4 integers, + * each from 1 to 4. + * ~ The arg to the expansion is a power of two, in the set + * {2, 4, 8, 16}. + * (11) An example valid sequence is: + * "b32 + o1.3 + C3.1 + r23 + e2.2 + D3.2 + X4" + * In this example, the following operation sequence is carried out: + * * b32: Add a 32 pixel border around the input image + * * o1.3: Opening with vert sel of length 3 (e.g., 1 x 3) + * * C3.1: Closing with horiz sel of length 3 (e.g., 3 x 1) + * * r23: Two successive 2x2 reductions with rank 2 in the first + * and rank 3 in the second. The result is a 4x reduced pix. + * * e2.2: Erosion with a 2x2 sel (origin will be at x,y: 0,0) + * * d3.2: Dilation with a 3x2 sel (origin will be at x,y: 1,0) + * * X4: 4x replicative expansion, back to original resolution + +""" + + +def kernel(x, y): + return np.ones((x, y), np.uint8) + + +def enhanceSID(image): + image=255-image + image=img_as_ubyte(image>100) + cv2.imwrite("enSID0.png", image) + # Remove noise + image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2,2), iterations=1) + # Closing. Connect non connected parts + image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 5), iterations=2) + # Again noise removal after closing + image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8,8), iterations=1) + # Skeletonization + ##For thinning I am using erosion + ##image = cv2.erode(image,kernel(4,4),iterations = 40) + image = img_as_ubyte(morphology.thin(image>128)) + cv2.imwrite("enSID1.png",image) + # Stub removal (might not be necessary if thinning instead of skeletonize is used above + # Making lines stronger + image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 5), iterations=1) + + image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(10, 10)) + # Thining again + image = img_as_ubyte(morphology.skeletonize(image>0.5)) + image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(10, 10)) + return image -- Gitblit v1.9.3