Objects Detection in python opencv
I am trying to detect the objects in an image and then making a rectangle around it. I followed the opencv docs and didn't got the desire output.
My code is :
import numpy as nm
import cv2
im=cv2.imread('4.jpg')
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt=contours[0]
cv2.drawContours(thresh,contours,-1,(0,255,0),10)
x,y,w,h=cv2.boundingRect(cnt)
cv2.rectangle(imgray,(x,y),(x+w,y+h),(0,255,0),10)
print x,y,w,h
cv2.imshow('image',imgray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Input :
Output: