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

Kinect V2 depth

asked 2020-02-08 04:31:04 -0600

GGR007 gravatar image

updated 2022-01-22 16:10:19 -0600

Evgeny gravatar image

Hello, my name is giggi, i'm begginer in ros: I studied some examples online. I use a kinectv2 to identify a purple disk in RGB image. Now I would like to try to find the distance between kinectv2 and this purple disk. The problem is that I I don't know where I could find this data. I attach some images.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-02-08 11:23:07 -0600

GGR007 gravatar image
    #!/usr/bin/env python

import roslib
import sys
import rospy
import cv2
import math, time
import numpy as np

from std_msgs.msg import String
from sensor_msgs.msg import Image
from geometry_msgs.msg import Point
from cv_bridge import CvBridge, CvBridgeError


class image_converter_depth :
    def is_detected(self):
            return(time.time() - self._time_detected < 1.0)



    def __init__(self):

        self._t0 = time.time()
        self.obj_point = Point()

        self.blob_x         = 0.0
        self.blob_y         = 0.0
        self._time_detected = 0.0

        self.blob_sub = rospy.Subsciber("blob/point_blob",Point, self.update_blob)
        rospy.loginfo("Subsciber to blob point set")
        self.image_pub = rospy.Publisher("Depth_image",Image)
        rospy.loginfo("Publisher with circle in  depth image ")
        self.obj_pub = rospy.Publisher("blob/obj_point",Point)
        self.bridege = CVBridge()
        self.image_sub = rospy.Subsciber("/kinect2/sd/image_depth",Image,self.callback)
        rospy.loginfo("Subsciber sd to depth image ")
    def update_blob(self,message):
        self.blob_x = message.x
        self.blob_y = message.y
        self._time_detected = time.time()

    def callback(self,data):
        try:
            cv_image=self.bridge.image_to_cv2(data,"mono16")
        except CVBridegeError as e :
            print(e)
            (rows,cols,channels)=cv_image.shape
            if ((int(rows/2))<int(self.blob_x)<(1920-int(rows/2))):
                if (int(cols/2)<int(self.blob_y)<1080-int(cols/2)):
                    x_depth = self.blob_x-(960-int(rows/2))
                    y_depth = self.blob_y-(540-int(cols/2))
                    self.obj_point.z = cv_image[x_depth,y_depth]
                    self.obj_pub.publish(self.obj_point)
    try:
        self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image,"mono16"))
    except CVBridegeError as e :
        print(e)



def main (args):
    ic = image_converter_depth()
    rospy.init("Get_Obj_Pos", anononymus=True)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")
    cv2.destroyAllWindows()

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

but I have this error

rosrun Get_Obj_Pos Get_Pos.py 
  File "/home/finokkio/catkin_ws/src/Get_Obj_Pos/src/Get_Pos.py", line 41
    self.blob_y = message.y
                          ^
IndentationError: unindent does not match any outer indentation level
edit flag offensive delete link more

Comments

which IDE you are using ? check the code in any normal text editor like gedit and check if the indentation is correct.

Abhishekpg gravatar image Abhishekpg  ( 2020-02-09 12:26:27 -0600 )edit

OK now i use the ATOM ide and this problem was solved but i have another error

GGR007 gravatar image GGR007  ( 2020-02-09 15:22:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-02-08 04:31:04 -0600

Seen: 232 times

Last updated: Feb 08 '20