Created
May 21, 2015 16:37
-
-
Save awesomebytes/e02ad0778dfea1692450 to your computer and use it in GitHub Desktop.
rotate ros compressed image image and republish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Created on 5/21/15 | |
@author: sampfeiffer | |
test_rotate_img.py contains a snippet of code | |
that subscribes to a compressed image and republishes it | |
rotated 180 deg. Useful for surveillance cameras | |
that may be hanging rotated | |
""" | |
__author__ = 'sampfeiffer' | |
import rospy | |
import cv2 | |
import numpy as np | |
from sensor_msgs.msg import CompressedImage | |
COMPRESSED_IMAGE_TOPIC = '/axis214/image_raw/compressed' | |
if __name__ == "__main__": | |
rospy.init_node('tesdfadfsf') | |
rotated_publisher = rospy.Publisher(COMPRESSED_IMAGE_TOPIC+'/rotated/compressed', CompressedImage) | |
# Get the image | |
img_ = rospy.wait_for_message(COMPRESSED_IMAGE_TOPIC, CompressedImage) | |
# Convert it to something opencv understands | |
img_np_arr = np.fromstring(img_.data, np.uint8) | |
encoded_img = cv2.imdecode(img_np_arr, cv2.CV_LOAD_IMAGE_COLOR) | |
# Show it | |
#cv2.imshow("image_axis", encoded_img) | |
#cv2.waitKey(0) # Press 'q' to exit | |
# grab the dimensions to calculate the center | |
(h, w) = encoded_img.shape[:2] | |
center = (w / 2, h / 2) | |
# rotate the image by 180 degrees | |
M = cv2.getRotationMatrix2D(center, 180, 1.0) | |
rotated = cv2.warpAffine(encoded_img, M, (w, h)) | |
# show the rotated image | |
cv2.imshow("rotated", rotated) | |
cv2.waitKey(0) | |
# publish the image | |
msg = CompressedImage() | |
msg.header.stamp = rospy.Time.now() | |
msg.format = "jpeg" | |
msg.data = np.array(cv2.imencode('.jpg', rotated)[1]).tostring() | |
rotated_publisher.publish(msg) | |
rospy.sleep(3) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment