我需要在sobel运算符之后发布图像,但是ROS消息不支持浮点数。那么如何发布边缘检测消息?
pub = rospy.Publisher('detector',Image,queue_size=10)
def image_callback(msg):
print("Received an image")
try:
cv2_img = bridge.imgmsg_to_cv2(msg,"bgr8")
#print(msg)
sobel(cv2_img)
except CvBridgeError,e:
print(e)
def sobel (photo):
gray = cv2.cvtColor(photo,cv2.COLOR_BGR2GRAY)
im_original = np.array(gray)
#im_original = np.array(photo.convert('L'))
imy = np.zeros(im_original.shape)
imx = np.zeros(im_original.shape)
# Run the Sobel operator
filters.sobel(im_original,1,imx,cval=0.0) # axis 1 is x
filters.sobel(im_original,imy,cval=0.0) # axis 0 is y
magnitude = np.sqrt(imx**2+imy**2)
#cv2.imshow('lol',magnitude)
print(magnitude.shape)
#imgMsg = bridge.cv2_to_imgmsg(np.array[magnitude],"bgr8")
message = bridge.cv2_to_imgmsg(magnitude,"64FC1")
pub.publish(message)
# Construct the plot
def main():
rospy.init_node('image_listener')
image_topic = "/usb_cam/image_raw"
rospy.Subscriber(image_topic,image_callback)
rospy.spin()
if __name__ == '__main__':
main()
如果我输入“ bgr8”,则表示指定为bgr8的编码,但是图像具有不兼容的类型64FC1。 我该如何克服这个问题?
Received an image
[[ 0. 4. 1.41421356 ...,0. 3.16227766
4.24264069]
[ 0. 4. 3.16227766 ...,0. 1.41421356
3.16227766]
[ 0. 3.16227766 4.47213595 ...,0. 0. 0. ]
...,[ 3.16227766 4.47213595 2. ...,51.86520992 30.46309242
11.66190379]
[ 3.16227766 4.47213595 2. ...,57.00877125 39.2173431
15.23154621]
[ 1.41421356 4.47213595 2. ...,59.22837158 45.54119015
18.11077028]]