Littered with robots and python code
Your code does not work, but mine doesimport cvdef find_car(image): size = cv.GetSize(image) #prepare memory car = cv.CreateImage(size, 8, 1) red = cv.CreateImage(size, 8, 1) hsv = cv.CreateImage(size, 8, 3) sat = cv.CreateImage(size, 8, 1) #split image into hsv, grab the sat cv.CvtColor(image, hsv, cv.CV_BGR2HSV) cv.Split(hsv, None, sat, None, None) #split image into rgb cv.Split(image, None, None, red, None) #find the car by looking for red, with high saturation cv.Threshold(red, red, 128, 255, cv.CV_THRESH_BINARY) cv.Threshold(sat, sat, 128, 255, cv.CV_THRESH_BINARY) #AND the two thresholds, finding the car cv.Mul(red, sat, car) #remove noise, highlighting the car cv.Erode(car, car, iterations=5) cv.Dilate(car, car, iterations=5) storage = cv.CreateMemStorage(0) obj = cv.FindContours(car, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) cv.ShowImage('A', car) if not obj: return(0, 0, 0, 0) else: return cv.BoundingRect(obj) points = capture = cv.CaptureFromCAM(0)if not capture: print "Error opening capture device" sys.exit(1)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)while 1: original = cv.QueryFrame(capture) car_rect = find_car(original) print car_rect middle = (car_rect + (car_rect / 2), car_rect + (car_rect/2)) if points == : points.append(middle) else: if abs(points[-1] - middle) > 5 and abs(points[-1] - middle) > 10: points.append(middle) cv.Rectangle(original, (car_rect, car_rect), (car_rect + car_rect, car_rect + car_rect), (255, 0, 0), 1, 8, 0) for point in points: cv.Circle(original, point, 1, (0, 0, 255), -1, 8, 0) cv.ShowImage('Analysed', original) k = cv.WaitKey(33)
can you please tell me how to track 2 or more objects?
Hi,The key to finding multiple objects is the cv.FindContours method that robby_rails used. This can be used to find multiple areas of interest. My method was very limited in that it only found a single area of interest, in specific lighting conditions.Cheers,Robert
can you please send me the c++ code,beacuse I am working in c++ my email is [email protected]
Hi Sekhar, thank you for your comment but unfortunately I don't have a lot of c++ experience so I wouldn't the best person to help here. I can only suggest having a look over Stack Overflow for helpful examples. Cheers, Robert
It give me this errorAttributeError: 'module' object has no attribute 'CaptureFromCAM'AttributeError: 'module' object has no attribute 'NamedWindow'AttributeError: 'module' object has no attribute 'SetCaptureProperty'How can i solve them?
Im working with cv2. What I need to change in the code?
I ported this code to cv2. You can find it on my github here:https://github.com/carolinux/opencv_experimentsIt works for a frame I screenshoted from the car video.I think the first contour (contour) is the 'negative' of the object in that case. I am returning the second one (countour) If we had multiple red objects we'd get more than two contours as the result from this bit:_, contours,hierarchy = cv2.findContours(car.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)return cv2.boundingRect(contours)
Disregard what I said about the contours. There was a bug in my code. Fixed it now and made it possible to create as many bounding boxes as objects detected.