Ask Your Question
1

Trouble detecting hand drawn shapes

asked 2016-06-28 04:12:38 -0600

riya1405 gravatar image

updated 2016-06-28 04:52:59 -0600

I have written a basic code to detect hand drawn shapes i.e. drawn on paper and captured with a camera.[p1.png] and this is the output that i am getting.

import numpy as np
import cv2

im = cv2.imread('c:/p1.png')
cv2.imshow('original image',im)
cv2.waitKey()

imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray image',imgray)
cv2.waitKey()

ret,thresh = cv2.threshold(imgray,80,255,0)
cv2.imshow('thresh image',thresh)
cv2.waitKey()

image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow('cont image',image)
cv2.waitKey()
ctt=0
for cnt in contours:
     approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
     print len(approx)
     if len(approx)==5:
         print "pentagon"
         cv2.drawContours(im,[cnt],0,255,-1)
         ctt+=1
     elif len(approx)==3:
         print "triangle"
         cv2.drawContours(im,[cnt],0,(0,255,0),-1)
         ctt+=1
     elif len(approx)==4:
         print "square"
         cv2.drawContours(im,[cnt],0,(0,0,255),-1)
         ctt+=1
     elif len(approx) == 9:
         print "half-circle"
         cv2.drawContours(im,[cnt],0,(255,255,0),-1)
         ctt+=1
     elif len(approx) > 15:
         print "circle"
         cv2.drawContours(im,[cnt],0,(0,255,255),-1)
         ctt+=1

cv2.imshow('img',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
print "Total no.= ",ctt
edit retag flag offensive close merge delete

Comments

the indentation inside the for loop is off, correct ?

berak gravatar imageberak ( 2016-06-28 04:26:00 -0600 )edit

yes. it works perfectly on a paint image.

riya1405 gravatar imageriya1405 ( 2016-06-28 04:34:00 -0600 )edit

wait, again, - do you think, above code is correct, or not ?

(i think, it's not. the pront, if and elif things should go into the for loop, else you only use the very last contour)

berak gravatar imageberak ( 2016-06-28 04:43:26 -0600 )edit
1

I had copied the code here but there were some identation problems. I have corrected it.

riya1405 gravatar imageriya1405 ( 2016-06-28 04:54:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-06-28 04:56:08 -0600

berak gravatar image

updated 2016-06-28 05:00:46 -0600

findContours() expects light shapes on dark background, so you have to invert the binary image, it should look like this:

image description

then, you want the approx checks to happen for each of the contours, not only for the last.

import numpy as np
import cv2

im = cv2.imread('c:/img.png')
cv2.imshow('original image',im)
cv2.waitKey()

imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray image',imgray)
cv2.waitKey()

ret,thresh = cv2.threshold(imgray,80,255,1) ### !! inverse binary !! ###
cv2.imshow('thresh image',thresh)
cv2.waitKey()

image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.imshow('cont image',image)
cv2.waitKey()
ctt=0
for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    print len(approx)  ### !! fix indentation !! ###
    if len(approx)==5:
        print "pentagon"
        cv2.drawContours(im,[cnt],0,255,-1)
        ctt+=1
    elif len(approx)==3:
        print "triangle"
        cv2.drawContours(im,[cnt],0,(0,255,0),-1)
        ctt+=1
    elif len(approx)==4:
        print "square"
        cv2.drawContours(im,[cnt],0,(0,0,255),-1)
        ctt+=1
    elif len(approx) == 9:
        print "half-circle"
        cv2.drawContours(im,[cnt],0,(255,255,0),-1)
        ctt+=1
    elif len(approx) > 15:
        print "circle"
        cv2.drawContours(im,[cnt],0,(0,255,255),-1)
        ctt+=1
    else:
        print "unknown"

cv2.imshow('img',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
print "Total no.= ",ctt

which leads to this result ( yea, lot of room to improve ;-] )

image description

edit flag offensive delete link more

Comments

Okay Thanks a lot!

riya1405 gravatar imageriya1405 ( 2016-06-28 05:05:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-28 04:07:30 -0600

Seen: 1,477 times

Last updated: Jun 28 '16