Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)

Input : Input imagecv2.destroyAllWindows()

Output: output image

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 : Input imagecv2.destroyAllWindows()

Output: output image

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

cv2 im=cv2.imread('4.jpg')

im=cv2.imread('4.jpg')

imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(imgray,127,255,0)

cv2.threshold(imgray,127,255,0)

contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnt=contours[0]

cnt=contours[0]

cv2.drawContours(thresh,contours,-1,(0,255,0),10) x,y,w,h=cv2.boundingRect(cnt)

cv2.drawContours(thresh,contours,-1,(0,255,0),10)

cv2.rectangle(imgray,(x,y),(x+w,y+h),(0,255,0),10) print x,y,w,h

x,y,w,h=cv2.boundingRect(cnt)

cv2.imshow('image',imgray) cv2.waitKey(0)

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

cv2.destroyAllWindows()

Input : Input image

Output: output image