kernel = np.ones((9,9),np.uint8) # default
def preprocessor_img(path, image_shape):
image = cv2.imread(path,0) # read in a grey scale
cv2_imshow(image) # show
print("="*50) #print endline
blured = cv2.GaussianBlur(image, (9,9), 0) # Remove noise by gaussian bluring
cv2_imshow(blured) # show
print("="*50) # endline
threshold, binary = cv2.threshold(blured, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # binarizaton (black and white)
print("threshold: ",threshold) # value
cv2_imshow(binary) # show
print("="*50) # endline
closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel, iterations=10) # Closing the area (segmentation)
cv2_imshow(closing) #show
print("="*50) #print
contours, hierarchies = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #Finding contour
print("numbers of contours: ", len(contours))
contour_image = image.copy() # clone and edit
the_biggest_contour_by_area = max(contours, key=cv2.contourArea) # get max contour area
cv2.drawContours(contour_image, [the_biggest_contour_by_area], -1, (0,255,255), 2) # draw to detect
cv2_imshow(contour_image)
print("="*50)
Rect_image = image.copy() # clone to edit
x,y,w,h = cv2.boundingRect(the_biggest_contour_by_area)
cv2.rectangle(Rect_image, (x, y), (x+w, y+h), (0,255,255), 3) # draw rectanagle
cv2_imshow(Rect_image)
print("="*50)
cropped = image[y:y+h, x:x+w]
cv2_imshow(cropped)
print("="*50)
resized = cv2.resize(cropped, (img_w,img_h), interpolation=cv2.INTER_LANCZOS4)
print(resized.shape)
cv2_imshow(resized)
print("="*50)
# resized_blured = cv2.GaussianBlur(resized, (9,9), 0)
threshold, resized_binary = cv2.threshold(resized, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2_imshow(resized_binary)
print("="*50)