There are 2 steps. In cv.GaussianBlur
, You can change odd number btween 15 to 21, but not even numbers to suit our needed.
Step 1:
#!/usr/bin/env python3
#OpenCV 4.0.1
#Date: 20th April, 2019
import cv2 as cv
import numpy as np
img = cv.imread('lines.jpg')
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(img, (19, 19), 0)
img = cv.bitwise_not(gray)
adp_thresh = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,15,-2)
horizontal_inv = cv.bitwise_not(adp_thresh.copy())
masked = cv.bitwise_and(img, img, mask=horizontal_inv)
inv_masked = cv.bitwise_not(masked)
cv.imshow('Masked', inv_masked)
cv.imwrite('masked.jpg', inv_masked)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Step 2:
#!/usr/bin/env python3
#OpenCV 4.0.1
#Date: 20th April, 2019
import cv2 as cv
import numpy as np
img = cv.imread('lines.jpg')
img=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(img, (19, 19), 0)
img = cv.bitwise_not(gray)
adapt_thresh = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 15, -2)
cv.imshow('adapt_thresh', adp_thresh)
#cv.imwrite('adapt_thresh.jpg', adp_thresh=)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
If you have
threshold
. Then increasing value between 50 to 127. Then another one isdilate
anderode
.If you could post orignal image.The current image uses dilate en erode and it does work as you could see the noise isn't that bad between the lines, I'm wondering if there is a way so I could remove the remaining noise between the lines