ROS旋律图片来自网络摄像头



我正在使用ROS旋律。该项目是Python中的线条检测。我从网络摄像头logitech C390E获取图像。如何在publisher&订阅者中的图像处理?

代码如下:

发布服务器
class Camera(object):
def __init__(self):
self.height = 640
self.width = 480
self.fps = 10
self.bridge = CvBridge()
self.pubstr = "/Visualization/camera/raw"
self.rawpub = rospy.Publisher(self.pubstr, Image, queue_size=10)
self.cap = cv2.VideoCapture(0)
def run(self):
if not self.cap.isOpened():
print("Camera is not openedn")
ret, cv_image = self.cap.read()
if not ret:
print("Detect no Imagen")
else:
self.rawpub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
if __name__ == '__main__':
rospy.init_node("Publish_camera")
camera = Camera()
r = rospy.Rate(10)
while not rospy.is_shutdown():
camera.run()
r.sleep()
订阅服务器
class Camera(object):
def __init__(self):
self.height = 640
self.width = 480
self.cnt = 1
self.rawcnt = 1
self.fps = 10
self.bridge = CvBridge()
self.processbridge = CvBridge()
self.rospack = rospkg.RosPack()
self.cv2image = Image()
self.substr = "/Visualization/camera/raw"
self.pubstr = "/Visualization/camera/processed"
self.save_path = self.rospack.get_path("graduate_project") + "/camera"
self.pub = rospy.Publisher(self.pubstr, Image, queue_size=1)
def callback(self, rawimage):
try:
src = self.bridge.imgmsg_to_cv2(rawimage, "bgr8")
except CvBridgeError as e:
print(e)
# Bird Wide View
#
#
if src.data is '':  
pass
else:
dst = cv2.Canny(src, 50, 200, None, 3)
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLines(dst, 0.8, np.pi / 180, 150, None, min_theta = (np.pi/2) - 1, max_theta = (np.pi/2) + 1)
if lines is not None:
for i in range(0, len(lines)):
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))

cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA)
imgmsg = self.processbridge.cv2_to_imgmsg(cdst, "bgr8")
self.pub.publish(imgmsg)

def run(self):
self.sub = rospy.Subscriber(self.substr, Image, self.callback, queue_size=100)
if __name__ == '__main__': 
rospy.init_node("PC_process")
camera = Camera()
r = rospy.Rate(10)
while not rospy.is_shutdown():
camera.run()
r.sleep()

您应该只订阅一次并调用rospy.spin(),这样节点就不会退出。试试这个:

if __name__ == '__main__': 
rospy.init_node("PC_process")
camera = Camera()
camera.run() # Subscribe once, callback will be called on each new message
rospy.spin() # Keep node from exiting

最新更新