Skip to content

Commit b83f8d6

Browse files
committed
0.2: variable filename
1 parent 74b0ec6 commit b83f8d6

File tree

2 files changed

+147
-101
lines changed

2 files changed

+147
-101
lines changed

LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Public Domain Mark 1.0
2+
No Copyright
3+
4+
This work has been identified as being free of known restrictions
5+
under copyright law, including all related and neighboring rights.
6+
7+
You can copy, modify, distribute and perform the work, even for
8+
commercial purposes, all without asking permission. See Other
9+
Information below.
10+
11+
Other Information
12+
13+
The work may not be free of known copyright restrictions in all
14+
jurisdictions.
15+
16+
Persons may have other rights in or related to the work, such as
17+
patent or trademark rights, and others may have rights in how the
18+
work is used, such as publicity or privacy rights.
19+
20+
In some jurisdictions moral rights of the author may persist beyond
21+
the term of copyright. These rights may include the right to be
22+
identified as the author and the right to object to derogatory
23+
treatments.
24+
25+
Unless expressly stated otherwise, the person who identified the work
26+
makes no warranties about the work, and disclaims liability for all
27+
uses of the work, to the fullest extent permitted by applicable law.
28+
29+
When using or citing the work, you should not imply endorsement by
30+
the author or the person who identified the work.

img2txt.py

100644100755
Lines changed: 117 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,117 @@
1-
import cv2
2-
import numpy as np
3-
#import pytesseract
4-
#pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
5-
#from PIL import Image
6-
#import image
7-
image = cv2.imread('test.jpg')
8-
#cv2.imshow('orig',image)
9-
#cv2.waitKey(0)
10-
11-
#Initial Processing of the image starts...!!!!!!!!!!!
12-
13-
#grayscale
14-
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
15-
cv2.imshow('gray',gray)
16-
cv2.waitKey(0)
17-
18-
#binary
19-
ret,thresh = cv2.threshold(gray,160,255,cv2.THRESH_BINARY_INV)
20-
cv2.imshow('Binarized',thresh)
21-
cv2.waitKey(0)
22-
23-
#detecting edges>>>>
24-
25-
edges=cv2.Canny(thresh,140,200)
26-
#cv2.imshow("edges",edges)
27-
28-
#dilation
29-
kernel = np.ones((5,5), np.uint8)
30-
img_dilation = cv2.dilate(edges, kernel, iterations=1)
31-
cv2.imshow('dilated',img_dilation)
32-
cv2.waitKey(0)
33-
34-
#Localization Horizontal projection
35-
(x,y)=img_dilation.shape
36-
z=[sum(y) for y in img_dilation]
37-
Tx=(((sum(z))/(len(z))/20))
38-
#print Tx
39-
40-
41-
#Vertical Projection
42-
ndilation=zip(*img_dilation)
43-
X=[sum(row) for row in ndilation]
44-
mean=(sum(X))/(len(X))
45-
maximofX=(max(X)/10)
46-
Ty=mean+maximofX
47-
#print Ty
48-
49-
50-
#Adaptive threashold for horizontal projection
51-
th1 = cv2.adaptiveThreshold(img_dilation,Tx,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2) #block size and constant.
52-
53-
54-
#Adaptive threashold for vertical projection
55-
th2 = cv2.adaptiveThreshold(img_dilation,Ty,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
56-
fthreashold=cv2.add(th1,th2)
57-
cv2.imshow("threashold1",fthreashold)
58-
59-
60-
61-
#Initial Processing of the image finishes....!!!!!
62-
63-
64-
#find contours
65-
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
66-
67-
#sort contours
68-
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
69-
70-
for i, ctr in enumerate(sorted_ctrs):
71-
# Get bounding box
72-
#(x,y) be the top-left coordinate of the rectangle and (w,h) be its width and height
73-
x, y, w, h = cv2.boundingRect(ctr)
74-
75-
#Removing the false area that are not textes.
76-
if w<35 and h<35:
77-
continue
78-
79-
# Getting ROI
80-
roi = image[y:y+h, x:x+w]
81-
82-
#result = pytesseract.image_to_string(Image.open(roi))
83-
84-
# show ROI
85-
#cv2.imshow(roi)
86-
cv2.imshow('segment no:'+str(i),roi)
87-
cv2.waitKey(0)
88-
cv2.rectangle(image,(x,y),( x + w, y + h ),(90,0,255),1)
89-
90-
#print result
91-
92-
'''cv2.imshow('marked areas',image)
93-
cv2.waitKey(0)
94-
cv2.imwrite('final.jpg', image)
95-
96-
97-
result = pytesseract.image_to_string(Image.open('final.jpg'))
98-
with open('fiel12.txt',mode='w') as file:
99-
file.write(result)
100-
print("Done")
101-
'''
1+
#!/usr/bin/env python
2+
import sys
3+
import cv2
4+
import numpy as np
5+
#import pytesseract
6+
#pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
7+
#from PIL import Image
8+
#import image
9+
10+
def imagetoregion(imagename, imagedest, segmentview):
11+
image = cv2.imread(imagename)
12+
cv2.imshow('orig',image)
13+
cv2.waitKey(0)
14+
15+
#Initial Processing of the image starts...!!!!!!!!!!!
16+
17+
#grayscale
18+
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
19+
cv2.imshow('gray',gray)
20+
cv2.waitKey(0)
21+
22+
#binary
23+
ret,thresh = cv2.threshold(gray,160,255,cv2.THRESH_BINARY_INV)
24+
cv2.imshow('Binarized',thresh)
25+
cv2.waitKey(0)
26+
27+
#detecting edges>>>>
28+
29+
edges=cv2.Canny(thresh,140,200)
30+
cv2.imshow("edges",edges)
31+
cv2.waitKey(0)
32+
33+
#dilation
34+
kernel = np.ones((15,15), np.uint8)
35+
img_dilation = cv2.dilate(edges, kernel, iterations=1)
36+
cv2.imshow('dilated',img_dilation)
37+
cv2.waitKey(0)
38+
39+
#Localization Horizontal projection
40+
(x,y)=img_dilation.shape
41+
z=[sum(y) for y in img_dilation]
42+
Tx=(((sum(z))/(len(z))/20))
43+
#print Tx
44+
45+
#Vertical Projection
46+
ndilation=zip(*img_dilation)
47+
X=[sum(row) for row in ndilation]
48+
mean=(sum(X))/(len(X))
49+
maximofX=(max(X)/10)
50+
Ty=mean+maximofX
51+
#print Ty
52+
53+
#Adaptive threashold for horizontal projection
54+
th1 = cv2.adaptiveThreshold(img_dilation,Tx,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2) #block size and constant.
55+
56+
#Adaptive threashold for vertical projection
57+
th2 = cv2.adaptiveThreshold(img_dilation,Ty,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
58+
fthreashold=cv2.add(th1,th2)
59+
cv2.imshow("threashold1",fthreashold)
60+
cv2.waitKey(0)
61+
62+
#Initial Processing of the image finishes....!!!!!
63+
64+
#find contours
65+
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
66+
67+
#sort contours
68+
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
69+
70+
for i, ctr in enumerate(sorted_ctrs):
71+
# Get bounding box
72+
#(x,y) be the top-left coordinate of the rectangle and (w,h) be its width and height
73+
x, y, w, h = cv2.boundingRect(ctr)
74+
75+
#Removing the false area that are not textes.
76+
if w<35 and h<35:
77+
continue
78+
79+
if segmentview:
80+
# Getting ROI
81+
roi = image[y:y+h, x:x+w]
82+
83+
#result = pytesseract.image_to_string(Image.open(roi))
84+
85+
# show ROI
86+
#cv2.imshow(roi)
87+
cv2.imshow('segment no:'+str(i),roi)
88+
cv2.waitKey(0)
89+
90+
cv2.rectangle(image,(x,y),( x + w, y + h ),(90,0,255),1)
91+
92+
#print result
93+
94+
cv2.namedWindow('marked areas', cv2.WINDOW_NORMAL)
95+
cv2.resizeWindow('marked areas', 800, 600)
96+
cv2.imshow('marked areas',image)
97+
cv2.waitKey(0)
98+
if not(segmentview):
99+
cv2.imwrite(imagedest, image)
100+
101+
'''
102+
result = pytesseract.image_to_string(Image.open('final.jpg'))
103+
with open('fiel12.txt',mode='w') as file:
104+
file.write(result)
105+
print("Done")
106+
'''
107+
108+
if __name__ == '__main__':
109+
segmentview = True
110+
filename = 'test.jpg'
111+
if len(sys.argv) > 1:
112+
filename = sys.argv[1]
113+
filedest = 'final.jpg'
114+
if len(sys.argv) > 2:
115+
filedest = sys.argv[2]
116+
segmentview = False
117+
imagetoregion(filename, filedest, segmentview)

0 commit comments

Comments
 (0)