Skip to content

Commit

Permalink
update opencv tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoran-Zhao committed Mar 25, 2018
1 parent 0a48e58 commit 6fe4531
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,9 @@ rosservice call /gazebo/set_model_state '{model_state: { model_name: red_box, po

- [`OpenCV moment
`](https://docs.opencv.org/3.1.0/dd/d49/tutorial_py_contour_features.html)

- [`OpenCV moment
`](https://docs.opencv.org/3.4.0/dd/d49/tutorial_py_contour_features.html)

- [`cv2.contourArea() examples
`](https://www.programcreek.com/python/example/86843/cv2.contourArea)
22 changes: 20 additions & 2 deletions testvisionplanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,38 @@ def image_callback(self,msg):
# END BRIDGE
# BEGIN HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# END HSV
# BEGIN FILTER
lower_red = np.array([ 0, 100, 100])
upper_red = np.array([10, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)

(_, cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#area = cv2.contourArea(cnts)
h, w, d = image.shape
# print h, w, d (800,800,3)
#BEGIN FINDER
M = cv2.moments(mask)
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

# cx range (55,750) cy range( 55, ~ )
# END FINDER
# Isolate largest contour
# contour_sizes = [(cv2.contourArea(contour), contour) for contour in cnts]
# biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
for i, c in enumerate(cnts):
area = cv2.contourArea(c)
if area > 7500:
cv2.circle(image, (cx, cy), 10, (0,0,0), -1)
cv2.putText(image, "({}, {})".format(int(cx), int(cy)), (int(cx-5), int(cy+15)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.drawContours(image, cnts, -1, (255, 255, 255),1)
# BEGIN circle
cv2.circle(image, (cx, cy), 10, (0,0,0), -1)

# print max(contour_sizes)[0]
#area = cv2.contourArea(cnts)
#print area
#END circle

cv2.namedWindow("window", 1)
Expand Down

0 comments on commit 6fe4531

Please sign in to comment.