sreekiranar / box_detection_opencv Goto Github PK
View Code? Open in Web Editor NEWThis Repository contains a Jupyter notebook explaining how to detect checkboxes/table cells from a scanned image
This Repository contains a Jupyter notebook explaining how to detect checkboxes/table cells from a scanned image
How we identify that check boz is checked or not .
hi,
I want to read a tambola ticket
I tried to use your code as follows
Saved above image as digital.jpg
Then ran
[code]
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
image_path='digital.jpg'
plot_flag=True
save_output=True
out_folder='outs'
os.makedirs(out_folder,exist_ok=True)
image=cv2.imread(image_path)
gray_scale=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
th1,img_bin = cv2.threshold(gray_scale,150,225,cv2.THRESH_BINARY)
img_bin=~img_bin
cv2.imwrite(os.path.join(out_folder,f'bin_{image_path}'),img_bin)
line_min_width = 15
kernal_h = np.ones((1,line_min_width), np.uint8)
kernal_v = np.ones((line_min_width,1), np.uint8)
img_bin_h = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_h)
cv2.imwrite(os.path.join(out_folder,f'hor_{image_path}'),img_bin_h)
img_bin_v = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, kernal_v)
cv2.imwrite(os.path.join(out_folder,f'ver_{image_path}'),img_bin_v)
img_bin_final=img_bin_h|img_bin_v
cv2.imwrite(os.path.join(out_folder,f'merge_{image_path}'),img_bin_final)
final_kernel = np.ones((3,3), np.uint8)
img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)
cv2.imwrite(os.path.join(out_folder,f'final_bin_{image_path}'),img_bin_final)
ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
def imshow_components(labels):
### creating a hsv image, with a unique hue value for each label
label_hue = np.uint8(179labels/np.max(labels))
### making saturation and volume to be 255
empty_channel = 255np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, empty_channel, empty_channel])
### converting the hsv image to BGR image
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
labeled_img[label_hue==0] = 0
### returning the color image for visualising Connected Componenets
return labeled_img
for x,y,w,h,area in stats[2:]:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite(os.path.join(out_folder,f'output_{image_path}'),image)
def detect_box(image,line_min_width=15):
gray_scale=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
th1,img_bin = cv2.threshold(gray_scale,150,225,cv2.THRESH_BINARY)
kernal6h = np.ones((1,line_min_width), np.uint8)
kernal6v = np.ones((line_min_width,1), np.uint8)
img_bin_h = cv2.morphologyEx(~img_bin, cv2.MORPH_OPEN, kernal6h)
img_bin_v = cv2.morphologyEx(~img_bin, cv2.MORPH_OPEN, kernal6v)
img_bin_final=img_bin_h|img_bin_v
final_kernel = np.ones((3,3), np.uint8)
img_bin_final=cv2.dilate(img_bin_final,final_kernel,iterations=1)
ret, labels, stats,centroids = cv2.connectedComponentsWithStats(~img_bin_final, connectivity=8, ltype=cv2.CV_32S)
return stats,labels
stats,labels=detect_box(image)
cc_out=imshow_components(labels)
for x,y,w,h,area in stats[2:]:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1)
cv2.imwrite(os.path.join(out_folder,f'cc_{image_path}'),cc_out)
cv2.imwrite(os.path.join(out_folder,f'out_{image_path}'),image)
[/code]
Note that I did not know about jupyter so I simply ran above code (call it make-box.py) using 'python make-box.py' .
How can I extract the top left and bottom right coordinates of boxes found in the image so that I can pass it to image processing utility that saves each square as individual image . Then , I will try to read number in that small box image .
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.