# TRy to remove lines
org_image = cv2.imread("/content/sample_to_remove_lines.png")
cv2_show('org_image', org_image)
gray = cv2.cvtColor(org_image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cv2_show('thresh Otsu', thresh)
# removing noise dots.
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, np.ones((2,2),np.uint8))
cv2_show('opening', opening)
thresh = opening.copy()
mask = np.zeros_like(org_image, dtype=np.uint8)
# Extract horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (60 ,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, (255, 255, 255), 8)
# cv2_show('mask extract horizontal lines', mask)
# Extract vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,70))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(mask, [c], -1, (255, 255, 255), 8)
cv2_show('mask extract lines', mask)
result = org_image.copy()
# Loop through the pixels of the original image and modify based on the mask
for y in range(mask.shape[0]):
for x in range(mask.shape[1]):
if np.all(mask[y, x] == 255): # If pixel is white in mask
result[y, x] = [255, 255, 255] # Set pixel to white
cv2_show("result", result2)
gray = cv2.cvtColor(result2,cv2.COLOR_BGR2GRAY)
_, simple_thresh = cv2.threshold(gray, 195, 255, cv2.THRESH_BINARY)
cv2_show('simple_thresh', simple_thresh)