ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

cant see point markers on rviz

asked 2022-03-08 12:34:34 -0600

Gzd gravatar image

Hi, my aim is to follow a green ball with the camera and show the movements of this ball in the view. To do this, I marked the middle of the ball with a marker, but I can't see it in the rviz. The code or rviz does not give any errors, what could be the problem? I am using ros neotic Thank you

#!/usr/bin/env python3
import rospy
from sensor_msgs.msg import Image
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
import sys

from collections import deque
from import VideoStream
from visualization_msgs.msg import Marker, MarkerArray
from geometry_msgs.msg import Point

import gc
import argparse
import time
import imutils

def camdet():
    bridge = CvBridge()
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video", help="Path to the (optional) video file")
    ap.add_argument("-b", "--buffer", default=64, type=int, help="max buffer size")
    args = vars(ap.parse_args())

greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
pts = deque(maxlen=args["buffer"])

if not args.get("video", False):
    vs = VideoStream(src=0).start()
    vs = cv2.VideoCapture(args["video"])


while True:
    frame =
    frame = frame[1] if args.get("video", False) else frame
    if frame is None:

    frame = imutils.resize(frame, width=600)
    blurred = cv2.GaussianBlur(frame, (11, 11), 0)
    hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

    mask = cv2.inRange(hsv, greenLower, greenUpper)
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)

    cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    center = None
    v = 0
    if len(cnts) > 0:
        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)

        M = cv2.moments(c)

        center = (int(M['m10']/M['m00']), int(M['m01']/M['m00']))

        if radius > 10:
  , (int(x), int(y)), int(radius), (0, 255, 255), 2)
  , center, 5, (0, 0, 255), -1)
            v += 1

    marker = Marker()
    marker.header.frame_id = "map"
    marker.ns = "marked" = 0
    marker.type = marker.POINTS
    marker.action = marker.ADD
    marker.pose.orientation.w = 1
    points = [0,0]
    for i in  range(v):
        p = Point()
        p.x = int(M['m10']/M['m00'])
        p.y = int(M['m01']/M['m00'])

        marker.points = points

    marker.scale.x = 0.5
    marker.scale.y = 0.5
    marker.color.g = 0.5
    marker.color.a = 1.0
    marker.color.r = 0.5
    marker.color.b = 1.0
    marker.lifetime = rospy.Duration()

    msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")
    cv2.imshow("Frame", frame)
    img_pub = rospy.Publisher('webcam/image_raw', Image, queue_size=10)
    rate = rospy.Rate(10)
    marker_pub = rospy.Publisher("/marker", Marker, queue_size = 100)

    key = cv2.waitKey(1) & 0xFF

    if key == ord('q'):

if not args.get("video", False):


if __name__ == '__main__':
    except rospy.ROSInterruptException:
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-03-08 15:51:23 -0600

robustify gravatar image

I think the problem is that you put the rospy and publisher initialization inside your while loop. You should only call rospy.init_node once, and the Publisher objects should only be initialized once. If you move these things above where you start the while loop, it should work, or at least properly publish messages.

edit flag offensive delete link more


Thank you for your answer. But when I moved this things above while loop it gives "UnboundLocalError: local variable 'msg' referenced before assignment" error. So, I cant really do that. When I look at rostopic list I can see /marker and /image_raw topics and I can visualize image_raw topic on rviz. Do you have any other suggestion?

Gzd gravatar image Gzd  ( 2022-03-09 02:44:41 -0600 )edit

Question Tools

1 follower


Asked: 2022-03-08 12:34:34 -0600

Seen: 121 times

Last updated: Mar 08 '22