| | |
| | | import cv2 |
| | | import numpy as np |
| | | from skimage import morphology, img_as_ubyte |
| | | from sklearn import svm |
| | | from sklearn.externals import joblib |
| | | |
| | | |
| | | """ |
| | |
| | | return sid_no |
| | | |
| | | |
| | | def segment_by_sid_len(image,sid_len, classifier): |
| | | sid_no="" |
| | | #find biggest block of pixels |
| | | def segment_by_sid_len(image, sid_mask, classifier): |
| | | sid_no = "" |
| | | sid_len = len(sid_mask) |
| | | if sid_mask[0] == "1": |
| | | move_left = 45 |
| | | elif sid_mask[0] == "x": |
| | | move_left = 55 |
| | | else: |
| | | move_left = 0 |
| | | # find biggest block of pixels |
| | | |
| | | image1=cv2.morphologyEx(image,cv2.MORPH_DILATE, kernel(5,25), iterations=3) |
| | | cv2.imwrite("sidblock1.png",image1) |
| | | image1 = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 25), iterations=4) |
| | | cv2.imwrite("sidblock1.png", image1) |
| | | im2, ctrs, hier = cv2.findContours( |
| | | image1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
| | | ) |
| | | sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.contourArea(ctr)) #get bigges contour |
| | | sorted_ctrs = sorted( |
| | | ctrs, key=lambda ctr: cv2.contourArea(ctr) |
| | | ) # get bigges contour |
| | | x, y, w, h = cv2.boundingRect(sorted_ctrs[-1]) |
| | | image=image[y:y+h,x+25:x+w-25] |
| | | cv2.imwrite("sidblock2.png",image) |
| | | image = image[y : y + h, x + 25 - move_left : x + w - 25] |
| | | cv2.imwrite("sidblock2.png", image) |
| | | imgHeight, imgWidth = image.shape[0:2] |
| | | numWidth=int(imgWidth/(sid_len)) |
| | | for i in range(0,sid_len): |
| | | num=image[:,i*numWidth:(i+1)*numWidth] |
| | | numWidth = int(imgWidth / (sid_len)) |
| | | for i in range(0, sid_len): |
| | | num = image[:, i * numWidth : (i + 1) * numWidth] |
| | | num = img_as_ubyte(num < 128) |
| | | num = cv2.resize(num, (32, 32)) |
| | | |
| | |
| | | |
| | | |
| | | def getSID(image, classifier, sid_mask): |
| | | sid_warn = [] |
| | | image = 255 - image |
| | | image = img_as_ubyte(image > 100) |
| | | cv2.imwrite("enSID0.png", image) |
| | |
| | | image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 3), iterations=4) |
| | | # Again noise removal after closing |
| | | |
| | | #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8, 8), iterations=1) |
| | | # image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8, 8), iterations=1) |
| | | # don't do too much noise removal. |
| | | image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=1) |
| | | |
| | | # Skeletonization |
| | |
| | | # Thining again |
| | | image = img_as_ubyte(morphology.skeletonize(image > 0.5)) |
| | | image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(10, 10)) |
| | | cv2.imwrite("enhancedSID.png",image) |
| | | cv2.imwrite("enhancedSID.png", image) |
| | | im2, ctrs, hier = cv2.findContours( |
| | | image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
| | | ) |
| | | sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0]) |
| | | |
| | | sid_no = "" |
| | | #sid_len = len(sid_mask) |
| | | #sid_no = segment_by_sid_len(image, sid_len, classifier) |
| | | #if sid_mask is not None: |
| | | print(len(sid_mask),len(sorted_ctrs)) |
| | | #if len(sid_mask)==len(sorted_ctrs): |
| | | sid_no=segment_by_contours(image,sorted_ctrs[1:],classifier) |
| | | print(len(sid_mask), len(sorted_ctrs)) |
| | | sid_no = segment_by_contours( |
| | | image, sorted_ctrs[1:], classifier |
| | | ) # we remove largest contour that surrounds whole image |
| | | print(sid_no) |
| | | if(len(sid_no)!=len(sid_mask)): |
| | | print("Ooops have to find another way") |
| | | sid_no=segment_by_sid_len(image,len(sid_mask),classifier) |
| | | return sid_no |
| | | if len(sid_no) != len(sid_mask): |
| | | #print("Ooops have to find another way") |
| | | sid_warn.append("Trying second SID algorithm.") |
| | | sid_no = segment_by_sid_len(image, sid_mask, classifier) |
| | | return (sid_no, [], sid_warn) |