Ask Your Question
0

Objects Detection in python opencv

asked 2015-01-19 05:57:47 -0600

Vivek gravatar image

updated 2015-01-19 06:23:09 -0600

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 image

Output: output image

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-01-19 12:04:38 -0600

I am not familiar with the Python API, but drawing a coloured rectangle in a single channel image will not work properly. Try to change the image back to RGB before drawing the rectangle.

edit flag offensive delete link more

Comments

3

true.

when drawing to a 1 channel image, only the 1st part of the Scalar, 0 will be used. black rect on black bg. in this case

berak gravatar imageberak ( 2015-01-19 13:14:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-19 05:57:47 -0600

Seen: 585 times

Last updated: Jan 19 '15