get a depth image using a kinect and openCV.
I have a code in C ++ that stores a Kinect's depth image, I wonder if there is any way to get an image, in where I can define the depth of the things captured into this image. for example, I only want the things that are at max at 1 meter of distance of the Kinect's sensor
I found the code into a thread here, so I edited it so I can try to get the image, the code is the next.
```cpp
#include <ros/ros.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
unsigned int cnt = 0;
void RetornoImagen(const sensor_msgs::ImageConstPtr& msg_depth )
{
//convertimos a un tipo de imagen de opencV
cv_bridge::CvImagePtr img_ptr_depth;
try{
img_ptr_depth = cv_bridge::toCvCopy(*msg_depth, sensor_msgs::image_encodings::TYPE_32FC1);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
double minVal, maxVal;
cv::Mat &mat = img_ptr_depth->image;
cv::Mat img2;
cv::Mat invertida;
cv::imshow("windowName",mat );
//mat.convertTo(img2,CV_8U, 255.0/(maxVal - minVal), 100.0 );
minMaxLoc(mat, &minVal, &maxVal,NULL,NULL); //find minimum and maximum intensities
img2=mat;
mat.convertTo(img2,CV_32FC1,65535.0/(maxVal - minVal), -minVal * 65535.0/(maxVal - minVal));
cv::imshow("ventana2",img2);
bbbb
cv::waitKey(3);
//ROS_INFO("asdasd");
char file1[100];
char file2[100];
cnt++;
sprintf( file1, "%04d_depth.png", cnt );
;
//parametros de compresion
std::vector<int> pp;
pp.push_back(CV_IMWRITE_PNG_COMPRESSION);
pp.push_back(0);
//guardamos la imagen en el disco duro, donde se ejecuta el nodo.
imwrite(file1,img2,pp);
//imwrite(file2,img2);
ROS_INFO_STREAM("El minimo es:" << minVal);
ROS_INFO_STREAM("El maximo es:" << maxVal);
}
int main(int argc, char **argv)
{
char a;
//initialize the ROS system and become a node.
ros::init(argc, argv, "guardar_imagen");
ros::NodeHandle nh;
//image_transport::ImageTransport it(nh);
ros::Subscriber image_sub_;
ros::Rate rate(1);
//creamos ventanas para visualizar el contenido
cvNamedWindow("windowName", CV_WINDOW_AUTOSIZE);
cvNamedWindow("ventana2", CV_WINDOW_AUTOSIZE);
cvNamedWindow("ventana3", CV_WINDOW_AUTOSIZE);
image_sub_ = nh.subscribe("/camera/depth_registered/hw_registered/image_rect_raw", 1, &RetornoImagen);
cvStartWindowThread();
while(ros::ok())
{
char c;
ROS_INFO_STREAM("\nIngrese 'a' para guardar\n");
cin.get(c);
cin.ignore();
c = tolower(c);
ROS_INFO_STREAM("You entered " << c << "\n");
if( c == 'a' )
{
unsigned int cnt_init = cnt;
while( cnt - cnt_init < 1 )
{
ros::spinOnce();
}
}
else break;
}
ROS_INFO_STREAM("Cerrando nodo\nClosing node\n");
//Destruimos las ventanas
cvDestroyWindow("windowName");
cvDestroyWindow("ventana2");
cvDestroyWindow("ventana3");
return 0;
}
thank you.
```cpp
Asked by Jluis619 on 2016-06-08 18:35:58 UTC
Comments