You are on page 1of 10

Program 1:

import cv2

import numpy as np

img = cv2.imread(r'C:\Users\CS0C81405\Desktop\savedimg.jpg',0)

img = cv2.bitwise_not(img)

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,-2)

cv2.imshow("th2", th2)

cv2.imwrite("th2.jpg", th2)

horizontal = th2

vertical = th2

rows,cols = horizontal.shape

#inverse the image, so that lines are black for masking

horizontal_inv = cv2.bitwise_not(horizontal)

horizontalsize = int(cols/20)

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)

vertical=th2

rows,cols=vertical.shape

verticalsize = int(rows/ 2)

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)
image=horizontal+vertical

cv2.imshow("image",image)

final=cv2.subtract(img,image)

cv2.imshow("final",~final)

output:
Program 2:

import cv2

import numpy as np

img = cv2.imread(r'C:\Users\CS0C81405\Desktop\page.png',0)

img = cv2.bitwise_not(img)

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,-2)

cv2.imshow("th2", th2)

cv2.imwrite("th2.jpg", th2)

horizontal = th2

vertical = th2

rows,cols = horizontal.shape

#inverse the image, so that lines are black for masking

horizontal_inv = cv2.bitwise_not(horizontal)

horizontalsize = int(cols/20)

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)

vertical=th2

rows,cols=vertical.shape

verticalsize = int(rows/ 2)

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)
image=horizontal+vertical

cv2.imshow("image",image)

final=cv2.subtract(img,image)

cv2.imshow("final",~final)

output
Program 3:

import cv2

import numpy as np

img = cv2.imread(r'C:\Users\CS0C81405\Desktop\newpic.png',0)

img = cv2.bitwise_not(img)

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,-2)

cv2.imshow("th2", th2)

cv2.imwrite("th2.jpg", th2)

horizontal = th2

vertical = th2

rows,cols = horizontal.shape

#inverse the image, so that lines are black for masking

horizontal_inv = cv2.bitwise_not(horizontal)

horizontalsize = int(cols/20)

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)

vertical=th2

rows,cols=vertical.shape

verticalsize = int(rows/ 2)

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)
image=horizontal+vertical

cv2.imshow("image",image)

final=cv2.subtract(img,image)

cv2.imshow("final",~final)
Program 4:

import cv2

import numpy as np

img = cv2.imread(r'C:\Users\CS0C81405\Desktop\capture20.png',0)

img = cv2.bitwise_not(img)

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,-2)

cv2.imshow("th2", th2)

cv2.imwrite("th2.jpg", th2)

horizontal = th2

vertical = th2

rows,cols = horizontal.shape

#inverse the image, so that lines are black for masking

horizontal_inv = cv2.bitwise_not(horizontal)

horizontalsize = int(cols/20)

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)

vertical=th2

rows,cols=vertical.shape

verticalsize = int(rows/ 2)

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)

image=horizontal+vertical

cv2.imshow("image",image)

final=cv2.subtract(img,image)

cv2.imshow("final",~final)

Output:
Program 5:

import cv2

import numpy as np

img = cv2.imread(r'C:\Users\CS0C81405\Desktop\capture30.png',0)

img = cv2.bitwise_not(img)

th2 = cv2.adaptiveThreshold(img,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,-2)

cv2.imwrite("th2.jpg", th2)

horizontal = th2

vertical = th2

rows,cols = horizontal.shape

#inverse the image, so that lines are black for masking

horizontal_inv = cv2.bitwise_not(horizontal)

horizontalsize = int(cols/20)

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize,1))

horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1))

horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1))

cv2.imwrite("horizontal.jpg", horizontal)

vertical=th2

rows,cols=vertical.shape

verticalsize = int(rows/ 2)

verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,verticalsize))

vertical = cv2.erode(vertical, verticalStructure, (-1, -1))


vertical = cv2.dilate(vertical, verticalStructure, (-1, -1))

cv2.imwrite("vertical.jpg", vertical)

image=horizontal+vertical

final=cv2.subtract(img,image)

cv2.imshow("final",~final)

Output:

You might also like