File was renamed from sid_process.py |
| | |
| | | import numpy as np |
| | | from skimage import morphology, img_as_ubyte |
| | | |
| | | import pkg_resources |
| | | |
| | | templatefile = '/template-8.png' # always use slash |
| | | template8 = pkg_resources.resource_filename(__name__, templatefile) |
| | | |
| | | |
| | | def kernel(x, y): |
| | | """ |
| | |
| | | # find biggest block of pixels |
| | | image1 = cv2.morphologyEx(image2, cv2.MORPH_DILATE, kernel(5, 25), iterations=4) |
| | | image1 = img_as_ubyte(image1 > 50) |
| | | cv2.imwrite("sidblock1.png", image1) |
| | | cv2.imwrite("/tmp/sidblock1.png", image1) |
| | | im2, ctrs, hier = cv2.findContours( |
| | | image1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE |
| | | ) |
| | |
| | | roi = cv2.resize(roi, (32, 32)) |
| | | |
| | | # cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2) |
| | | cv2.imwrite("sid_no_{}.png".format(i), roi) |
| | | cv2.imwrite("/tmp/sid_no_{}.png".format(i), roi) |
| | | sid_no = sid_no + str(classifier.predict(roi.reshape(1, -1) / 255.0)[0]) |
| | | return sid_no |
| | | |
| | |
| | | sid_no = "" |
| | | sid_len = len(sid_mask) |
| | | image=find_biggest_blob(image,original_image,sid_mask) |
| | | cv2.imwrite("sidblock2.png", image) |
| | | cv2.imwrite("/tmp/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] |
| | | num = img_as_ubyte(num < 128) |
| | | num = cv2.resize(num, (32, 32)) |
| | | cv2.imwrite("sid_no_{}.png".format(i), num) |
| | | cv2.imwrite("/tmp/sid_no_{}.png".format(i), num) |
| | | sid_no = sid_no + str(classifier.predict(num.reshape(1, -1) / 255.0)[0]) |
| | | return sid_no |
| | | |
| | |
| | | original_image, cv2.MORPH_CLOSE, kernel(2, 2), iterations=10 |
| | | ) |
| | | block_image = img_as_ubyte(block_image < 50) |
| | | cv2.imwrite("sid_3rd1.png", block_image) |
| | | template = cv2.imread("template-8.png", 0) |
| | | cv2.imwrite("/tmp/sid_3rd1.png", block_image) |
| | | template = cv2.imread(template8, 0) |
| | | w, h = template.shape[::-1] |
| | | res = cv2.matchTemplate(block_image, template, cv2.TM_CCOEFF_NORMED) |
| | | loc = np.where(res >= 0.75) |
| | |
| | | points = [loc_filtered_y[a], loc_filtered_x[a]] |
| | | for pt in zip(*points[::-1]): |
| | | cv2.rectangle(cimg, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2) |
| | | cv2.imwrite("sid_3rd2.png", cimg) |
| | | cv2.imwrite("/tmp/sid_3rd2.png", cimg) |
| | | |
| | | sid_no = "" |
| | | for i, pt in enumerate(zip(*points[::-1])): |
| | |
| | | num = cv2.resize(num, (32, 32)) |
| | | except: |
| | | return "" |
| | | cv2.imwrite("sid_3no_{}.png".format(i), num) |
| | | cv2.imwrite("/tmp/sid_3no_{}.png".format(i), num) |
| | | sid_no = sid_no + str(classifier.predict(num.reshape(1, -1) / 255.0)[0]) |
| | | |
| | | return sid_no |
| | |
| | | image = 255 - image |
| | | image_original = image.copy() |
| | | image = img_as_ubyte(image > 100) |
| | | cv2.imwrite("enSID0.png", image) |
| | | cv2.imwrite("/tmp/enSID0.png", image) |
| | | |
| | | # Remove noise |
| | | image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3) |
| | |
| | | |
| | | # Skeletonization |
| | | image = img_as_ubyte(morphology.thin(image > 128)) |
| | | cv2.imwrite("enSID1.png", image) |
| | | cv2.imwrite("/tmp/enSID1.png", image) |
| | | |
| | | # Stub removal (might not be necessary if thinning instead of skeletonize is used above |
| | | # Making lines stronger |
| | |
| | | # 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("/tmp/enhancedSID.png", image) |
| | | |
| | | sid_no = segment_by_contours(image, image_original, classifier, sid_mask) |
| | | |