To draw contours around specific object(numbers) and return the respective no. and its position as a list as output

asked 2015-11-28 10:56:37 -0600

Tarun gravatar image

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 description

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 ):

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)

edit retag flag offensive close merge delete

Comments

the reference pic is also attached below --> it is not :), could you please try again?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-30 03:17:33 -0600 )edit

the reference pic is also attached below --> it is not :), could you please try again?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-30 03:17:53 -0600 )edit