| | |
| | | |
| | | def find_biggest_blob(image, original_image, sid_mask): |
| | | if sid_mask[0] == "1": |
| | | move_left = 35 |
| | | move_left = 45 |
| | | elif sid_mask[0] == "x": |
| | | move_left = 40 |
| | | move_left = 50 |
| | | else: |
| | | move_left = 0 |
| | | # Remove noise |
| | |
| | | original_image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3 |
| | | ) |
| | | # find biggest block of pixels |
| | | image1 = cv2.morphologyEx(image2, cv2.MORPH_DILATE, kernel(5, 25), iterations=4) |
| | | image1 = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 25), iterations=3) |
| | | image1 = img_as_ubyte(image1 > 50) |
| | | cv2.imwrite("/tmp/sidblock1.png", image1) |
| | | im2, ctrs, hier = cv2.findContours( |
| | |
| | | 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 - move_left : x + w - 40] # +25,-25 |
| | | image = image[y : y + h, x + 25 - move_left : x + w - 30] # +25,-25 |
| | | return image |
| | | |
| | | |
| | |
| | | # Get bounding box |
| | | x, y, w, h = cv2.boundingRect(ctr) |
| | | # Getting ROI |
| | | if w < h / 2: |
| | | if w < h / 3: |
| | | sid_no = sid_no + "1" |
| | | continue |
| | | roi = image[y : y + h, x : x + w] |
| | |
| | | |
| | | # cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2) |
| | | cv2.imwrite("/tmp/sid_no_{}.png".format(i), roi) |
| | | sid_no = sid_no + str(classifier.predict(roi.reshape(1, -1) / 255.0)[0]) |
| | | sid_currno= str(classifier.predict(roi.reshape(1, -1) / 255.0)[0]) |
| | | sid_no = sid_no + sid_currno |
| | | fname="/tmp/SID/"+str(sid_currno)+"/sid"+str(np.random.randint(0,1000000))+".png" |
| | | print("Writing all the sid images ", fname) |
| | | cv2.imwrite(fname, roi) |
| | | return sid_no |
| | | |
| | | |
| | |
| | | cv2.imwrite("/tmp/enSID0.png", image) |
| | | |
| | | # Remove noise |
| | | # image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2, 2), iterations=3) |
| | | image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=2) |
| | | |
| | | # Closing. Connect non connected parts |
| | | image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 1), 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) |
| | | #image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(3, 3), iterations=2) |
| | | |
| | | # Skeletonization |
| | | #image = img_as_ubyte(morphology.skeletonize(image > 128)) |
| | | image = img_as_ubyte(morphology.thin(image > 128)) |
| | | cv2.imwrite("/tmp/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_DILATE, kernel(5, 2), iterations=1) |
| | | image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(10, 10)) |
| | | |
| | | # Thining again |
| | |
| | | |
| | | if not sid_compare(sid_no, sid_mask): |
| | | sid_err = ["Wrong SID!"] |
| | | |
| | | cv2.imwrite("/tmp/SID_"+sid_no+".png", image) |
| | | return sid_no, sid_err, sid_warn |