python - Removing Horizontal Lines in image (OpenCV, Pyhton, Matplotlib) -


using following code can remove horizontal lines in images. see result below.

import cv2 matplotlib import pyplot plt  img = cv2.imread('image.png',0)  laplacian = cv2.laplacian(img,cv2.cv_64f) sobelx = cv2.sobel(img,cv2.cv_64f,1,0,ksize=5)  plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray') plt.title('original'), plt.xticks([]), plt.yticks([]) plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray') plt.title('laplacian'), plt.xticks([]), plt.yticks([]) plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray') plt.title('sobel x'), plt.xticks([]), plt.yticks([])  plt.show() 

result

the result pretty good, not perfect good. want achieve 1 showed here. using this code.

source image.. source

one of questions is: how save sobel x without grey effect applied ? original processed..

also, there better way ?

edit

using following code source image good. works pretty well.

import cv2 import numpy np  img = cv2.imread("image.png") img=cv2.cvtcolor(img,cv2.color_bgr2gray)  img = cv2.bitwise_not(img) th2 = cv2.adaptivethreshold(img,255, cv2.adaptive_thresh_mean_c,cv2.thresh_binary,15,-2) cv2.imshow("th2", th2) cv2.imwrite("th2.jpg", th2) cv2.waitkey(0) cv2.destroyallwindows()  horizontal = th2 vertical = th2 rows,cols = horizontal.shape  #inverse image, lines black masking horizontal_inv = cv2.bitwise_not(horizontal) #perform bitwise_and mask lines provided mask masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv) #reverse image normal masked_img_inv = cv2.bitwise_not(masked_img) cv2.imshow("masked img", masked_img_inv) cv2.imwrite("result2.jpg", masked_img_inv) cv2.waitkey(0) cv2.destroyallwindows()  horizontalsize = int(cols / 30) horizontalstructure = cv2.getstructuringelement(cv2.morph_rect, (horizontalsize,1)) horizontal = cv2.erode(horizontal, horizontalstructure, (-1, -1)) horizontal = cv2.dilate(horizontal, horizontalstructure, (-1, -1)) cv2.imshow("horizontal", horizontal) cv2.imwrite("horizontal.jpg", horizontal) cv2.waitkey(0) cv2.destroyallwindows()  verticalsize = int(rows / 30) verticalstructure = cv2.getstructuringelement(cv2.morph_rect, (1, verticalsize)) vertical = cv2.erode(vertical, verticalstructure, (-1, -1)) vertical = cv2.dilate(vertical, verticalstructure, (-1, -1)) cv2.imshow("vertical", vertical) cv2.imwrite("vertical.jpg", vertical) cv2.waitkey(0) cv2.destroyallwindows()  vertical = cv2.bitwise_not(vertical) cv2.imshow("vertical_bitwise_not", vertical) cv2.imwrite("vertical_bitwise_not.jpg", vertical) cv2.waitkey(0) cv2.destroyallwindows()  #step1 edges = cv2.adaptivethreshold(vertical,255, cv2.adaptive_thresh_mean_c,cv2.thresh_binary,3,-2) cv2.imshow("edges", edges) cv2.imwrite("edges.jpg", edges) cv2.waitkey(0) cv2.destroyallwindows()  #step2 kernel = np.ones((2, 2), dtype = "uint8") dilated = cv2.dilate(edges, kernel) cv2.imshow("dilated", dilated) cv2.imwrite("dilated.jpg", dilated) cv2.waitkey(0) cv2.destroyallwindows()  # step3 smooth = vertical.copy()  #step 4 smooth = cv2.blur(smooth, (4,4)) cv2.imshow("smooth", smooth) cv2.imwrite("smooth.jpg", smooth) cv2.waitkey(0) cv2.destroyallwindows()  #step 5 (rows, cols) = np.where(img == 0) vertical[rows, cols] = smooth[rows, cols]  cv2.imshow("vertical_final", vertical) cv2.imwrite("vertical_final.jpg", vertical) cv2.waitkey(0) cv2.destroyallwindows() 

result

but if have image ?

example

i tried execute code above , result poor...

result3

other images working on these...

enter image description here

enter image description here

enter image description here


Comments

Popular posts from this blog

swift - Button on Table View Cell connected to local function -

dns - Dokku server hosts two sites with TLD's, both domains are landing on only one app -

c# - ajax - How to receive data both html and json from server? -