Trouble detecting hand drawn shapes
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
the indentation inside the for loop is off, correct ?
yes. it works perfectly on a paint image.
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)
I had copied the code here but there were some identation problems. I have corrected it.