Tag Archives: compilation and execution in opencv c++

OPENCV AND PCL(FILTERING A PCD FILE)

Hello friends,

After doing a lot of research in point cloud library,I came up with the successful integration of computer vision with point cloud library.

I hope you have opencv and pcl installed by now.If not see:
1)https://abhishek4273.wordpress.com/2014/02/05/install-opencv/
2)https://abhishek4273.wordpress.com/2014/05/26/install-point-cloud-library/

This post is the result of inspiration from http://ramsrigoutham.com/2012/06/28/integrating-pcl-and-opencv-passthrough-filter-example/

Since I was not successful in compiling and running the code from the blog mentioned above in ubuntu 12.04 I came up with another solution.

The code below filters the point cloud from a PCD file and visualizes it.

The code will work properly on every PCD file after you make sure the filter_limit parameters are set in a proper way.

Download the code from here : https://github.com/abhi-kumar/OPENCV_MISC/blob/master/opencv_pcl_filter.cpp

Here goes the explaination:

#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/passthrough.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkSmartPointer.h>

These are the required PCL libraries

int a = 22;
int b = 12;
int c=  10;

These are pre-set values for the trackbar

pcl::visualization::PCLVisualizer viewer ("Get the view here");

This is the window to display/visualize stuff

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);

Here we have declared two point clouds of type POINTXYZ,you can change the type according to your PCD file.

pcl::io::loadPCDFile (argv[1], *cloud);
pcl::copyPointCloud( *cloud,*cloud_filtered);

Now we have loaded the file into the cloud and then copied into the other cloud.

cv::namedWindow( "picture");
cvCreateTrackbar("X_limit", "picture", &a, 30, NULL);
cvCreateTrackbar("Y_limit", "picture", &b, 30, NULL);
cvCreateTrackbar("Z_limit", "picture", &c, 30, NULL);

This will look familiar to OpenCV users.For those who dont know this,it is a way of creating trackbars to control any parameter,here,filter limits.

pcl::PassThrough<pcl::PointXYZ> pass;

Creating an object of class PassThrough filter

while (!viewer.wasStopped ())
             {

		pcl::copyPointCloud(*cloud_filtered, *cloud);

        i = 0.1*((float)a);
        j = 0.1*((float)b);
        k = 0.1*((float)c);

//        cout << "i = " << i << " j = " << j << " k = " << k << endl;

        pass.setInputCloud (cloud);
	pass.setFilterFieldName ("y");
        pass.setFilterLimits (-j, j);
        pass.filter (*cloud);

        pass.setInputCloud (cloud);
        pass.setFilterFieldName ("x");
        pass.setFilterLimits (-i, i);
        pass.filter (*cloud);

	pass.setInputCloud (cloud);
        pass.setFilterFieldName ("z");
        pass.setFilterLimits (-k,k);
        pass.filter (*cloud);

	viewer.addPointCloud (cloud, "scene_cloud");
	viewer.spinOnce ();
	viewer.removePointCloud("scene_cloud");
	waitKey(10);
	}

Iterating though the cloud,setting filter parameters and linking it to the trackbars.

Now for the compiling part.
1)Download this file and save it as pcl.sh in the same folder where you have saved opencv_pcl_filter.cpp : https://github.com/abhi-kumar/OPENCV_MISC/blob/master/pcl.sh

2)Download this sample PCD file and save as test.pcd in the same folder : https://github.com/abhi-kumar/OPENCV_MISC/blob/master/test.pcd

3) open a terminal and type
a)chmod +x pcl.sh
b)./pcl.sh opencv_pcl_filter.cpp
c)./opencv_pcl_filter test.pcd

And you will see something like this:
Screenshot from 2014-07-01 17:29:43

And thats it,we have successfully created a filter in PCL.

Thanks 🙂

Advertisements

CONTRAST AND BRIGHTNESS VARIATION OF IMAGES

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets see how do you change contrast and brightness in opencv

Open a c++ file named contrast.cpp and paste the following code in it

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.

**********************CODE STARTS HERE*******************************

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

double alpha; /**< Simple contrast control */
int beta;  /**< Simple brightness control */

int main( int argc, char** argv )
{

Mat image = imread( argv[1] );
Mat new_image = Mat::zeros( image.size(), image.type() );

std::cout<<" Basic Linear Transforms "<<std::endl;
std::cout<<"-------------------------"<<std::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;

for( int y = 0; y < image.rows; y++ )
{ for( int x = 0; x < image.cols; x++ )
{ for( int c = 0; c < 3; c++ )
{
new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
}
}
}

namedWindow("Original Image", 1);
namedWindow("New Image", 1);

imshow("Original Image", image);
imshow("New Image", new_image);

return 0;
}

**************************CODE ENDS HERE*******************************

Compilation:

1. chmod +x contrast.cpp
2. g++ -ggdb `pkg-config --cflags opencv` -o `basename contrast.cpp .cpp` contrast.cpp `pkg-config --libs opencv`

Usage:

./contrast IMAGE.EXTENSION CONTRAST_INCREASE_VALUE BRIGHTNESS_ADDING_VALUE

(eg: – ./contrast cat.jpg 2 100)

OPENCV READ IMAGE AND VIDEO

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets look into a way of opening image/video/camera with opencv

Reading an image:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.


#include &lt;highgui.h&gt;     //main header file

int main( int argc,char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
//IplImage is an old way of reading,Mat can also be used

cvNamedWindow("example" ,CV_WINDOW_AUTOSIZE);
//naming a window to show image

cvShowImage("example,img);
//Tranferring the image read to the window

cvWaitKey(0);
//creating a waitkey to put up a stable window

cvReleaseImage( &amp;img);
cvDestroyWindow("example");
}


3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp

Note:FILENAME is the name given to the file,use the one you used

b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`
c)./FILENAME

Reading a video/camera input:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

#include <highgui.h>

CvCapture* capture = NULL;
int main( int argc, char** argv )
{
cvNamedWindow( "example5", CV_WINDOW_AUTOSIZE );

if(argc == 1)
{
capture = cvCreateCameraCapture(0);
}
else
{
capture = cvCreateFileCapture(argv[1]);
}
assert(capture != NULL);
IplImage* frame;
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "example5", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "example5" );
}

3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp
Note:FILENAME is the name given to the file,use the one you used
b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`

c)./FILENAME (for video)

d)./FILENAME _____________ (for video,the video file name goes in the blank,eg:- ./FILENAME video.mkv and the video file should be in same folder of the cpp file otherwise whole path should me mentioned while execution eg:- ./FILENAME /home/USER/Desktop/video.mkv)

Thank you 🙂

See also :- http://blindperception.wordpress.com/