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

How to capture multiple images from Kinect V2 (ROS)?

asked 2018-01-23 04:07:16 -0500

hassanyf gravatar image

updated 2018-01-24 12:08:32 -0500

I created a small code that opens the Kinect IR camera. I can see the real time video that it shows, and I can capture one image. But how do I capture multiple images? It keeps on overwriting with image0

Here is my code so far:

#!/usr/bin/python

import roslib
import rospy
import cv2
import sys
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

class PositionTracker:

    def __init__(self):
        # subscribe to kinect image messages
        self.sub = rospy.Subscriber("kinect2/qhd/image_ir", Image, self.image_callback, queue_size=1)
        self.bridge = CvBridge()

    def image_callback(self,data):

    try:
        cv_image = self.bridge.imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)
    cv2.imshow("Image window", cv_image)
c=cv2.waitKey(10)%256
if c == 10:
    h,w=cv_image.shape
    bitmap=cv2.cv.CreateImage((w,h), cv2.cv.IPL_DEPTH_16S, 1)
    cv2.cv.SaveImage('image'+str(click)+'.jpg',bitmap)
    click=click+1   
    #time.sleep(3)

def main(args):
    ic = PositionTracker()
    rospy.init_node('kinect_tracker')
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)

I have tried to use the cv.SaveImage function. But it gives me an error. The above code opens the Kinect IR camera, but I want it to capture images and save it to the file system. How do I do it?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-05-31 05:37:21 -0500

shyamashi gravatar image

updated 2018-05-31 05:42:47 -0500

def image_callback(self,data):

    try:
    global click                                                  //added
        cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")        //added
        filename = 'image'+str(click)+'.jpg'                      //added
        cv2.imwrite(filename, cv_image)                            //added
        click = click+1                                           //added 
        #print (cv_image.shape)
    except CvBridgeError as e:
        print(e)

    cv2.imshow("Image window", cv_image)
    cv2.waitKey(3)

def main(args): ic = PositionTracker() rospy.init_node('kinect_tracker', anonymous=True) try: rospy.spin() except KeyboardInterrupt: print("Shutting down") cv2.destroyAllWindows()

if __name__ == '__main__': global click // added click = 0 // added main(sys.argv)

edit flag offensive delete link more
0

answered 2018-01-23 08:36:56 -0500

JamesDoe gravatar image

updated 2018-01-25 01:51:40 -0500

I don't usually use Python, but in C++ we have an imwrite function in openCV. I just googled it and it seems to be there for opencv for python too.

Try:

cv2.imwrite(FileName, cv_image)

edit flag offensive delete link more

Comments

Thanks! It's working now. I am using cv2.cv.SaveImage('image'+str(click)+'.jpg',bitmap) to save it. but it's not saving images in a loop. It keeps on overwriting.

hassanyf gravatar image hassanyf  ( 2018-01-24 08:16:02 -0500 )edit

What is the filename of the file it keeps overwriting? P.s. I would still recommend trying out imwrite. After googling a bit it seems to be the most standard way to save images.

JamesDoe gravatar image JamesDoe  ( 2018-01-24 09:07:36 -0500 )edit

I am saving it as 'image'+str(click)+'.jpg' where click is a variable and will be incremented. I am expecting image0.jpg, image1.jpg etc. But it keeps on overwriting on the same file i.e image0.jpg.

hassanyf gravatar image hassanyf  ( 2018-01-24 12:08:09 -0500 )edit

Check answer again (couldn't write code in comment properly)

JamesDoe gravatar image JamesDoe  ( 2018-01-24 15:26:48 -0500 )edit

Yes I already did that. But if I add any kind of loop inside the def image_calback function, the Kinect IR camera will lag a lot. It will lag and then eventually freeze. I don't why is this happening.

hassanyf gravatar image hassanyf  ( 2018-01-24 23:12:09 -0500 )edit

Alright. I see what you mean now. Sorry, really not used to reading Python code. It looks like you don't actually need a loop there, since you're working in a callback. It seems like it keeps overwriting the same file because click is defined in the callback here? Try defining it globally.

JamesDoe gravatar image JamesDoe  ( 2018-01-25 01:45:47 -0500 )edit

Or at the very least, define it somewhere else than in your callback.

JamesDoe gravatar image JamesDoe  ( 2018-01-25 01:48:42 -0500 )edit

I have already defined it globally. But it still overwrites it.

hassanyf gravatar image hassanyf  ( 2018-01-25 06:32:13 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-23 04:07:16 -0500

Seen: 784 times

Last updated: May 31 '18