import rospy
from sensor_msgs.msg import Image
import numpy as np
import cv2
from cv_bridge import CvBridge
class CaptureImage(object):
def __init__(self):
self.sub_img = rospy.Subscriber("/camera/projected_image", Image, self.get_img)
self.cvbridge = CvBridge()
self.num = 12
def get_img(self, img):
cv_img = self.cvbridge.imgmsg_to_cv2(img, "bgr8")
print("cv_img shape", cv_img.shape)
cv2.imshow("img", cv_img)
key = cv2.waitKey(33)
if key == 97:
print("capture image")
cv2.imwrite("./images/"+str(self.num)+".jpg", cv_img)
self.num += 1
def spin(self):
rospy.spin()
if __name__ == "__main__":
rospy.init_node("test")
CaptureImage().spin()