Process and display lidar data - opencv

I am a newbie to lidar processing. I am interested in the depth/range value of the lidar data. I would like to extract these values and display just like the image data as shown in the code. What would be the best way to do it? I have only shown the relevant part of the code
#include <sensor_msgs/PointCloud2.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
static const std::string OPENCV_WINDOW = "Image window";
using namespace sensor_msgs;
using namespace message_filters;
void callback(const ImageConstPtr& image1, const sensor_msgs::PointCloud2ConstPtr& pc1)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(image1, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
// Update GUI Window
cv::imshow(OPENCV_WINDOW, cv_ptr->image);
cv::waitKey(3);

Related

Opencv : No error in code , but webcam not showing in result

I am training out the tutorial in opencv.
it had no error when compile.
I know the code for the tutorial is for opencv2.4 and I had change the coding for cvquery and videoframe.
My output is like this
.
My webcam is working fine but it not showing anything in my result.
If you wish to perform face detection using HaarCascades, you can use this code:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
CascadeClassifier facecascade;
int main()
{
Mat frame;
facecascade.load("haarcascade_frontalface_alt.xml");
if(facecascade.empty())
{
cout<<"Error";
}
VideoCapture cap(0);
int i=0,j=0,k=0;
while(1)
{
cap>>frame;
Mat frame_gray;
cvtColor(frame,frame_gray,CV_BGR2GRAY);
vector<Rect>faces;
facecascade.detectMultiScale(frame_gray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(70,70));
if(faces.size()>0)
{
for(i=0;i<faces.size();i++)
{
rectangle(frame_gray,faces[i],Scalar(200,200,250),2,8,0);
}
char no[5];
sprintf(no,"No. of faces detected = %d",int(faces.size()));
putText(frame_gray,no,Point(10,30),FONT_HERSHEY_TRIPLEX,1,Scalar(255,255,255),1);
imshow("out",frame_gray);
char c= waitKey(5);
if(c=='b')
break;
}
return 0;
}

Any alternative solution for QMessagebox for IOS development (QWidget application only)?

I am using Qt 5.3 and trying to develop application for IOS.
Problem is, QWidget application in a iPhone Retina simulator:
QMessage becomes full-screen.
In Application output panel I see: This plugin does not support
propagateSizeHints().
So looking for alternative solution for QMessageBox. I don't want to learn QML yet.
If you do an overlay on top of your widget you can make something similar to the iOS popups.
Basically you create another widget, and you parent it to the widget you want it to be drawn on top of.
Here are some helpful flags and lines of code to put in your overlay constructor:
setPalette(Qt::transparent);
// if you have buttons on this overlay you probably don't want this one
setAttribute(Qt::WA_TransparentForMouseEvents);
QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(20);
this->setGraphicsEffect(dse);
Then be sure to command a resize of your overlay when the parent widget resizes:
void ParentWidget::resizeEvent(QResizeEvent *event)
{
overlay->resize(event->size());
event->accept();
}
http://www.qtcentre.org/wiki/index.php?title=Widget_Overlay
UPDATE: Awesome example
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.resize(300,600);
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "overlaydialogbox.h"
#include <QResizeEvent>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void resizeEvent(QResizeEvent *event);
private:
OverlayDialogBox * m_overlay;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_overlay = new OverlayDialogBox(this);
}
MainWindow::~MainWindow() { }
void MainWindow::resizeEvent(QResizeEvent *event)
{
m_overlay->resize(event->size());
event->accept();
}
overlaydialogbox.h
#ifndef OVERLAYDIALOGBOX_H
#define OVERLAYDIALOGBOX_H
#include <QWidget>
class OverlayDialogBox : public QWidget
{
Q_OBJECT
public:
explicit OverlayDialogBox(QWidget *parent = 0);
signals:
void accepted();
void rejected();
void finished(int);
public slots:
};
#endif // OVERLAYDIALOGBOX_H
overlaydialogbox.cpp
#include "overlaydialogbox.h"
#include <QGridLayout>
#include <QGraphicsEffect>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QIcon>
OverlayDialogBox::OverlayDialogBox(QWidget *parent) :
QWidget(parent)
{
setPalette(Qt::transparent);
// if you have buttons on this overlay you probably don't want this one
// setAttribute(Qt::WA_TransparentForMouseEvents);
QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(20);
this->setGraphicsEffect(dse);
QGridLayout * grid = new QGridLayout();
this->setLayout(grid);
QMessageBox * msg = new QMessageBox(QMessageBox::Warning,"Testing","This is a test QMessageBox.");
QObject::connect(msg, SIGNAL(accepted()), this, SIGNAL(accepted()));
QObject::connect(msg, SIGNAL(finished(int)), this, SIGNAL(finished(int)));
QObject::connect(msg, SIGNAL(rejected()), this, SIGNAL(rejected()));
QObject::connect(msg, SIGNAL(finished(int)), this, SLOT(close()));
msg->setPalette(Qt::white);
grid->addWidget(msg);
}
Hope that helps.

Read a sequence of frames using OpenCV/C++

I want to read a sequence of frames from any folder using openCV. All frames are in sequence i.e. (1).jpg,(2).jpg,....
I tried
VideoCapture cap;
cap.open("Directory/");
for(;;)
{
Mat frame;
cap >> frame;
}
but it doesn't work.
This question has been asked before but i don't know why this answer doesn't work for me.
OpenCV: Reading image series from a folder
do i need to rename the images?.
cap open should be cap.open("Directory/(%02d).jpg"); and you have to rename your images so that they look like (01).jpg,(02).jpg etc so that they have fixed length. if the images are like (001).jpg then you should use `cap.open("Directory/(%03d).jpg");
edit
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap;
cap.open("imgs/(%02d).jpg");
int i=0;
for(;;)
{
if(i++%37==0)cap=VideoCapture("imgs/(%02d).jpg");//there are 37 frames in the dir
Mat frame;
cap >> frame;
imshow("frame",frame);
if(waitKey(1)==27)
exit(0);
}
return 0;
}
Try to prepare an xml/yaml file with list of names with path to the images, in the desired order. Then load the list as a vector or some resembling structure, and then open them one by one in a loop.
Here the full code to do read a sequence of frames with five zeros in name "frame00000.jpg, frame00001.jpg,.....,frame00010.jpg...) using string concatination idea just like matlab.
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <opencv/cv.h> // include it to used Main OpenCV functions.
#include <opencv/highgui.h> //include it to use GUI functions.
using namespace std;
using namespace cv;
string intToStr(int i,string path){
string bla = "00000";
stringstream ss;
ss<<i;
string ret ="";
ss>>ret;
string name = bla.substr(0,bla.size()-ret.size());
name = path+name+ret+".jpg";
return name;
}
int main(int, char**)
{
string previous_window = "Previous frame";
string current_window = "Current frame ";
int i=0;
for(int i = 1 ; i< 10 ; i++)
{
Mat Current, Previous;
string Curr_name = intToStr(i,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
string Prev_name = intToStr(i-1,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
Current = imread(Curr_name,1);
Previous = imread(Prev_name,1);
namedWindow(current_window,WINDOW_AUTOSIZE);
namedWindow(current_window,WINDOW_AUTOSIZE);
imshow(current_window,Current);
imshow(previous_window,Previous);
waitKey(0);
}
}
Where "D:/NU/Junior Scientist/Datasets/egtest04/frame" is the path sting.

OpenCV 2.4.2 imshow causes crash

I have this nasty problem with opencv 2.4.2.
I use VS 2012 to compile this short test programm.
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace cv;
int main()
{
Mat sudoku = imread("sudoku.jpg",0);
namedWindow("Lines", CV_WINDOW_AUTOSIZE);
imshow("Lines", sudoku);
}
Imshow is the problem. When I remove it, it runs without any problem. I found a tip here which said to use debug libs instead but it didn't help.
First of all, you have to check if image is loaded correctly. To do this just check if image.data is NULL or not.
Secondly, after calling imshow you have to call waitKey to show image:
http://opencv.willowgarage.com/documentation/cpp/user_interface.html#cv-waitkey
Here's the whole code:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat sudoku = imread("sudoku.jpg",0);
if (sudoku.data == NULL)
{
cout << "No image found! Check path." << endl;
return 1;//ERROR
}
else
{
namedWindow("Lines", CV_WINDOW_AUTOSIZE);
imshow("Lines", sudoku);
waitKey();//without this image won't be shown
return 0;//OK
}
}

OPENCV Weird Error

I am using Background Subtraction and want to display the contents. Somehow the code seems to break all the time due to a memory exception. The error seems to be in cvCopy function. Can't figure it out.
#include "cv.h"
#include "highgui.h"
#include "opencv2\core\operations.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\core\types_c.h"
#include "opencv\cxcore.h"
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int, char**)
{
bool flag=0;
VideoCapture cap(0); // open the default camera
VideoCapture cap1(0);
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat gray,bg,result,frame,result1,final,frame1;
//CvMemStorage* contours = NULL;
cap>>frame;
cvtColor(frame,bg,CV_BGR2GRAY);
namedWindow("GRAY",1);
for(;;)
{
//final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
cap >> frame; // get a new frame from camera
cap1 >> frame1;
cvtColor(frame, gray, CV_BGR2GRAY);
absdiff(gray,bg,result);
threshold(result,result1,50,255,THRESH_BINARY);
//cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)ΒΆ
//cvCopy(&frame1, &final, &result1);
//findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//drawContours(final,contours,-1,CV_RGB(0,255,0));
//imshow("GRAY",result1);
//imshow("GRAY", result);
imshow("GRAY1",final);
if(flag)
{
imshow("BG",bg);
}
//if(waitKey(0)==27) break;
if(waitKey(1)==32)
{
cvtColor(frame,bg,CV_BGR2GRAY);
flag=!flag;
}
if(waitKey(1)==27)
{
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Instead of mixing the C and C++ APIs I would recommend you stick to the C++ API where possible. If you merely want to copy a matrix, just use either Mat::clone() or Mat::copyTo(). Since you want to use a mask, use the copyTo member function like this:
Mat final;
frame1.copyTo(final, result1);
Hope that helps!

Resources