I'm trying to visualize a point cloud using PCL CloudViewer. The problem is that I'm quite new to C++ and I have found two tutorials first demonstrating the creation of PointCloud and second demonstrating the visualization of a PointCloud. However, I'm not able to combine these two tutorials.
Here is what I have come up with:
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
int main (int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ> cloud;
// Fill in the cloud data
cloud.width = 5;
cloud.height = 1;
cloud.is_dense = false;
cloud.points.resize (cloud.width * cloud.height);
for (size_t i = 0; i < cloud.points.size (); ++i)
{
cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
}
pcl::visualization::CloudViewer viewer ("Simple Cloud Viewer");
viewer.showCloud (cloud);
while (!viewer.wasStopped ())
{
}
return (0);
}
but that even do not compile:
error: no matching function for call to
‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’
Your error message tells you what you need to do:
error: no matching function for call to ‘pcl::visualization::CloudViewer::showCloud(pcl::PointCloud<pcl::PointXYZ>&)’
So go to the documentation for CloudViewer and see what arguments this member function takes: http://docs.pointclouds.org/1.5.1/classpcl_1_1visualization_1_1_cloud_viewer.html
There we see that it takes a const MonochromeCloud::ConstPtr &cloud not the raw reference that you are passing in. This is a typedef of a smart pointer from boost:
typedef boost::shared_ptr<const PointCloud<PointT> > pcl::PointCloud< PointT >::ConstPtr
So when you create your cloud you need to wrap it in one of these smart pointers instead of making it a local variable. Something like (untested):
pcl::MonochromeCloud::ConstPtr cloud(new pcl::PointCloud<pcl::PointXYZ>());
Then, when you pass in the variable cloud, it will have the correct type and you won't get the error that you report. You will also have to change your cloud.foos to cloud->foos.
Looking at the second example you give, they do this as well:
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
To give the answer straight away:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr ptrCloud(&cloud);
Then put in ptrCloud in the viewer, it's what it expects:
viewer.showCloud (ptrCloud);
If anybody else is just searching how to do this in ROS it can be simply done using:
#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
class cloudHandler
{
public:
cloudHandler():viewer("Cloud Viewer")
{
pcl_sub = nh.subscribe("/camera/depth_registered/points", 10, &cloudHandler::cloudCB, this);
viewer_timer = nh.createTimer(ros::Duration(0.1), &cloudHandler::timerCB,this);
}
void cloudCB(const sensor_msgs::PointCloud2 &input)
{
pcl::PointCloud<pcl::PointXYZRGB> cloud;
pcl::fromROSMsg (input, cloud);
viewer.showCloud(cloud.makeShared());
}
void timerCB(const ros::TimerEvent&)
{
if(viewer.wasStopped())
{
ros::shutdown();
}
}
protected:
ros::NodeHandle nh;
ros::Subscriber pcl_sub;
pcl::visualization::CloudViewer viewer;
ros::Timer viewer_timer;
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "pcl_visualize");
cloudHandler handler;
ros::spin();
return 0;
}
The includes could probably be cleaned more :)
The tutorial for CloudViewer pcl cloudviewer tutorialhttp://pointclouds.org/documentation/tutorials/cloud_viewer.php#cloud-viewer defines the point cloud as follows:
pcl::PointCloud<pcl::PointXYZRGB>**::Ptr** cloud;
But you have written:
pcl::PointCloud<pcl::PointXYZ> cloud;
So try passing the cloud as &cloud instead of cloud, or declare it as a pointer.
Related
I want to write a C++ program to get associated applications which are suitable to open specified file. I find the LSCopyApplicationURLsForURL API, and create a command line C++ application by XCode.
But after running this program, I always get segment fault. XCode shows EXEC_BAD_ACCESS(code=1, address....) error.
I also tryied running it from sudo, but the same result. What is the problem?
The code:
#include <iostream>
#include <objc/objc.h>
#include <objc/objc-runtime.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
using namespace std;
int main(int argc, const char * argv[]) {
auto url = CFURLRef("file:///Users/efan/src/a.cpp");
auto ret = LSCopyApplicationURLsForURL(url, kLSRolesAll);
cout << ret << endl;
return 0;
}
Try creating your CFURLRef using one of the proper CFURLCreate* methods. See "Creating a CFURL" here.
For example:
auto tempStringURL = CFStringCreateWithCString(nullptr, "/Users/efan/src/a.cpp", kCFStringEncodingUTF8);
auto url = CFURLCreateWithFileSystemPath(nullptr, tempStringURL, kCFURLPOSIXPathStyle, FALSE);
auto ret = LSCopyApplicationURLsForURL(url, kLSRolesAll);
You need to Release the "Created" variables to clean up memory.
I am trying to get a QGeoLocation. My version of Qt is 5.7.1, btw, and I am running it on Debian.
I saw this post how to get latitude/longitude from one geo address using Qt c++ on windows?
I copied and pasted the working solution from Scheff's answer, but still got not error and 0 locations. Does this have to do with my setup/environment?
This shorter code has the same effect:
#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>
int main( int argc, char **argv)
{
QCoreApplication app( argc, argv );
QGeoServiceProvider geoSrv( "osm" );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
QGeoAddress addr;
addr.setCountry( "China" );
QGeoCodeReply *geoCode = geoCoder->geocode( addr );
if ( geoCode->error() )
qDebug() << "error";
qDebug() << geoCode->locations().length();
return app.exec();
}
I found your post while struggling with the same problem. For me the QGeoServiceProvider code suddenly stopped working with OpenStreetmap. I quickly tried out the "here" api and that seems to work with exactly the same code. With some quick inspection with wireshark I found the problem easily.
QGeoServiceProvider tries to connect to the OpenStreetMap api at this url: http://nominatim.openstreetmap.org where it gets a redirected via a HTTP 303 to https://nominatim.openstreetmap.org. Apparently, the QGeoServiceProvider isn't able to handle this redirection correctly. I fixed it by providing the new url in the osm.geocoding.host parameter. Using your code this would look like this:
#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>
int main( int argc, char **argv)
{
QCoreApplication app( argc, argv );
//Add this
QMap<QString,QVariant> params;
params["osm.geocoding.host"] = "https://nominatim.openstreetmap.org";
QGeoServiceProvider geoSrv( "osm", params );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
QGeoAddress addr;
addr.setCountry( "China" );
QGeoCodeReply *geoCode = geoCoder->geocode( addr );
if ( geoCode->error() )
qDebug() << "error";
qDebug() << geoCode->locations().length();
return app.exec();
}
Hope this helps!
Instead of using
QGeoServiceProvider geoSrv( "osm", params );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
If you use a pointer instead:
QGeoServiceProvider* geoSrv = new QGeoServiceProvider( "osm", params );
QGeoCodingManager *geoCoder = geoSrv->geocodingManager();
It should work(did for me at least)
I am new to C++ and i am getting error like
error: no matching function for call to 'FaceDetector::FaceDetector(std::__cxx11::string)'
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
and i am attaching my code and error log how to fix this please guide me
#define DEFAULT_CASCADE_PATH "cascades/haarcascade_frontalface_default.xml"
#define ORIGINALS_LIST "obama_raw/list"
#define OUTPUT_DIR "obama_faces"
#define OUTPUT_LIST "list"
#define FACE_SIZE Size(150,150)
#include <cstdlib>
#include <fstream>
#include "cv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "FaceDetector.h"
using namespace std;
using namespace cv;
void read_input_list(const string &list_path, vector<Mat> &images) {
ifstream file(list_path.c_str());
string path;
while (getline(file, path)) {
images.push_back(imread(path));
}
}
int main(int argc, char** argv) {
FaceDetector fd(string(DEFAULT_CASCADE_PATH));
vector<Mat> raw_faces;
ofstream out_list(format("%s/%s", OUTPUT_DIR, OUTPUT_LIST).c_str());
read_input_list(string(ORIGINALS_LIST), raw_faces);
int img_c = 0; //images counter
//now detect the faces in each of the raw images:
for (vector<Mat>::const_iterator raw_img = raw_faces.begin() ; raw_img != raw_faces.end() ; raw_img++){
vector<Rect> faces;
//detect faces in the image (there should be only one):
fd.findFacesInImage(*raw_img, faces);
//cut each face and write to disk:
for (vector<Rect>::const_iterator face = faces.begin() ; face != faces.end() ; face++){
int edge_size = max(face->width, face->height);
Rect square(face->x, face->y, edge_size, edge_size);
Mat face_img = (*raw_img)(square);
//resize:
resize(face_img, face_img, FACE_SIZE);
//write to disk:
string face_path = format("%s/%d.jpg", OUTPUT_DIR, img_c++);
imwrite(face_path,face_img);
out_list << face_path << endl;
}
}
out_list.close();
return 0;
}
and i am attaching my error log.Please can any one help.
Thanks in advance
Error : https://i.stack.imgur.com/RZXXK.jpg
From GCC 5, A new ABI is enabled by default. In that new ABI, std::__cxx11 namesapce was introduced.
According to your error message, It seems that your program and OpenCV library you want to link with were build with different GCC version, which made incompatible binary.
For more information, you can read the following page:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
I am trying to create a class where I shall find KeyPoints in an image. However, I get a ridiculous error I cannot figure out how to solve.
The problem is now that
this->detector
is zero, so it is for some reason not initialized properly. I have searched on google but I haven't found anything.
My headerfile looks as follows
/*
* FindKeyPoints.h
*
* Created on: Jan 21, 2015
* Author: erikbylow
*/
#ifndef FINDKEYPOINTS_H_
#define FINDKEYPOINTS_H_
#include <vector>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
using namespace std;
class FindKeyPoints {
private:
// Create vector of KeyPoints to store the detected keypoint in
vector<cv::KeyPoint> keyPointsOld;
vector<cv::KeyPoint> keyPointsNew;
// Threshold for similarity
const int THRESHOLD = 10;
// Instance(?) DescriptionExtractor and descriptor
cv::Ptr<cv::DescriptorExtractor> descriptionExtractor;
cv::Ptr<cv::FeatureDetector> detector;
public:
FindKeyPoints(const string &METHOD);
virtual ~FindKeyPoints();
void detectKeypoints(cv::Mat& imgNew, cv::Mat& imgOld);
};
#endif /* FINDKEYPOINTS_H_ */
And constructor looks as
FindKeyPoints::FindKeyPoints(const string &METHOD) {
cout<<METHOD<<endl;
cv::initModule_features2d();
this->detector = cv::FeatureDetector::create("SURF");
//this->descriptionExtractor = cv::DescriptorExtractor::create(METHOD);
}
and a function I will use looks as:
// Input: The new image and the old image. Find Keypoints and extract the descriptors.
void FindKeyPoints::detectKeypoints(cv::Mat& imgNew,
cv::Mat& imgOld) {
if (this->detector == 0){
cout<<"Hej"<<endl;
}
// this->detector->detect(imgNew, keyPointsNew);
// this->detector->detect(imgOld, keyPointsOld);
}
I use cmake and (part of) my CMakeLists.txt looks like:
TARGET_LINK_LIBRARIES(${PROJECT_NAME} groundtruth ${OpenCV_LIBS} ${QT_LIBRARIES}
${QGLViewer_LIBRARIES} ${OPENGL_gl_LIBRARY} GL
glut)
Can it be that
${OpenCV_LIBS}
does not include libopencv_nonfree.so ?
Regards
I suspect the error is that I had only included
opencv2/nonfree/features2d.hpp
and not also
opencv2/nonfree/nonfree.hpp.
I'm using ROS and OpenCV in C++ environment in order to acquire a video (gray-scale) from a ROS node, convert the data through cv_bridge (in order to elaborate it through OpenCV), extract some data and publish them on a topic as ROS messages.
My problem is that I don't know how to send the array frame to the main function in order to elaborate it! I cannot elaborate out of it, because I need to distinguish between different data of different frames.
This is my code:
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include "rd_ctrl/proc_data.h"
#include <cv.h>
using namespace std;
namespace enc = sensor_msgs::image_encodings;
rd_ctrl::proc_data points;
ros::Publisher data_pub_;
static const char WINDOW[] = "Image window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
ImageConverter()
: it_(nh_)
{
image_pub_ = it_.advertise("out", 1);
image_sub_ = it_.subscribe("/vrep/visionSensorData", 1, &ImageConverter::imageCb, this);
cv::namedWindow(WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(WINDOW);
}
void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
CvMat frame= cv_ptr->image; //definition of "frame"
cvSmooth(&frame, &frame, CV_MEDIAN);
cvThreshold(&frame, &frame,200, 255,CV_THRESH_BINARY);
cv::imshow(WINDOW, cv_ptr->image);
cv::waitKey(3);
image_pub_.publish(cv_ptr->toImageMsg());
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_proc");
ImageConverter ic;
ros::NodeHandle n;
//....elaboration of "frame" and production of the data "points"
data_pub_.publish(points);
data_pub_ = n.advertise<rd_ctrl::proc_data>("/data_im", 1);
ros::spin();
return 0;
}
I hope that the question is clear enough. Can you help me please?
Now if I understand you correctly you want to call imageCb and have the frame it creates passed back to main. If that is indeed the case then you can modify imageCb as follows:
void imageCb(const sensor_msgs::ImageConstPtr& msg, cv::Mat frame )
You will need to create the frame in main and pass it imageCb:
cv::Mat frame ;
ic.imageCb( ..., frame ) ;
This will use the copy constructor, it will just copy the header and use a pointer to the actual data, so it won't be very expensive.
This previous thread has a much more detailed elaboration on cv::Mat copy constructor.