Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Recently I have started learning Python and opencv to learn image processing. Recently I was assigned a task as given below and the reference pic is also attached below:

image link:http://postimg.org/image/4kcxi87v3/[^]

Task : A set of test images, each containing

• Arena having two divisions: Division 1 (D1), and Division 2 (D2).

• Division D1 is a grid having 12 Cells numbered from 0 to 11 and D2 is a grid having 24 Cells numbered from 0 to 23 as shown in Figure 1.

• Each Cell in D1 contains a one digit number (i.e. number from 0 to 9) while any one or two digits numbers (i.e. numbers from 0 to 99) are present in some of the Cells in D2.

Objective: refer image as input and return number and their Cell position in D1(first box) and D2(second box) on Python IDLE console. Also show image with green contours around detected numbers as shown in Figure 3. Note: For green color take BGR value as (0,255,0)

Sample output : The output on the Python IDLE console will look like:

D1 = [8,6,1,5,2,0,7,2,3,9,1,3]
D2 = [ [2,10], [6,12], [22,14] ]
Now at start, I thought of learning 'machine learning' to detect the text, just to realize that the first part of drawing "specific" contours can be done by hit and trail method too . I had to draw contours only around the no. and the output should of the form as give above:

Below is code that I typed (only for the contours) , plz suggest a much robust method which will work any other similar images ( as this code will work for this specific question ):

Python
import cv2

import numpy

image = cv2.imread("test_image1.jpg")

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale

_,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY_INV) # threshold
cv2.imwrite("thresh.jpg",thresh)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 5) 
cv2.imwrite("dilate.jpg",dilated)

contours,hierarchy= cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) # get     contours

cv2.drawContours(image,contours,23(0,255,0),3)
cv2.drawContours(image,contours,22,(0,255,0),3)
cv2.drawContours(image,contours,19,(0,255,0),3)
cv2.drawContours(image,contours,17,(0,255,0),3)
cv2.drawContours(image,contours,16,(0,255,0),3)
cv2.drawContours(image,contours,15,(0,255,0),3)
cv2.drawContours(image,contours,14,(0,255,0),3)
cv2.drawContours(image,contours,13,(0,255,0),3)
cv2.drawContours(image,contours,12,(0,255,0),3)
cv2.drawContours(image,contours,10,(0,255,0),3)
cv2.drawContours(image,contours,9,(0,255,0),3)
cv2.drawContours(image,contours,8,(0,255,0),3)
cv2.drawContours(image,contours,7,(0,255,0),3)
cv2.drawContours(image,contours,5,(0,255,0),3)
cv2.drawContours(image,contours,4,(0,255,0),3) 
cv2.drawContours(image,contours,3,(0,255,0),3)
cv2.drawContours(image,contours,2,(0,255,0),3)
cv2.drawContours(image,contours,0,(0,255,0),3)  
cv2.imshow("contoured.jpg", image)
Posted
Updated 28-Nov-15 4:43am
v2
Comments
Richard MacCutchan 28-Nov-15 11:11am    
Do not use fixed values in your code; that means it will only work for this set. Calculate the values based on the input data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900