Running OpenCV through Ros on ROS INDIGO

Installing OpenCv In Ubuntu

Verified on Ubuntu 14.0.4

 

Step 1 : Remove previous installed opencv if it is not OpenCV- 2.4.9 .

Step 2 : If you have OpenCV-2.4.9 , then check whether opencv and opencv2 are present or not in local include directory by writing following command .If not then remove installed OpenCV-2.4.9

open terminal window.
 cd ~
 ls ../../usr/local/include/

Step 3 : OpenCV-2.4.9 installation : Follow below steps :
//Open the terminal and write this:

sudo apt-get update
 sudo apt-get upgrade
 sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev

// Time to get the OpenCV 2.4.9 source code:

cd ~
 wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip
 unzip opencv-2.4.9.zip
 cd opencv-2.4.9
 mkdir build
 cd build
 cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON ..

// Check that the above command produces no error and that in particular it reports FFMPEG as YES in theminal window. If this is not the case you will not be able to read or write videos. Check that Java, Python, TBB, OpenGL, V4L, OpenGL and Qt are all detected correctly.

 

//Now, you are ready to compile and install OpenCV 2.4.9:

 

 

make
sudo make install

// Now you have to configure OpenCV. First, open the opencv.conf file with the following code:

 

sudo gedit /etc/ld.so.conf.d/opencv.conf

//Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

/usr/local/lib

/* you will see some warning in terminal window after doing this. Don’t worry neglect the warning and move to next step. */

//Run the following code :

 
 sudo ldconfig
 sudo gedit /etc/bash.bashrc

 

// Add these two lines at the end of the file and save it:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
 export PKG_CONFIG_PATH

// Finally, close the terminal and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

 

Step 4 : Check OpenCV Is succesfully installed or not by running sample examples :

// open terminal window and write folowing commands

 cd ~/opencv-2.4.9/samples/c
 chmod +x build_all.sh
 ./build_all.sh
~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg

 

/* you must see a pic of lady will pop ups out. This ensures your opencv is working.

 



 

Step 5 : Time for interfacing OpenCV with ROS i.e running OpenCV through ROS .
Create Package (here i named ros_opencv_interface) in your Catkin workspace .Write Following commands in your terminal.

# I am assuming that you have your catkin workspace created already.
if you have any problem in creating catkin workspace refer below link:
http://wiki.ros.org/catkin/Tutorials/create_a_workspace

cd ~/catkin_ws/src
 catkin_create_pkg ros_opencv_interface std_msgs rospy roscpp
 

// you need to build the packages in the catkin workspace:

cd ~/catkin_ws
 catkin_make

 

// add the workspace to your ROS environment you need to source the generated setup file:
. ~/catkin_ws/devel/setup.bash

Step 6 : Changes in CmakeLists.txt .
Write following commands in new terminal, for opening CmakeLists.txt

 cd ~/catkin_ws/src/ros_opencv_interface/
 gedit CMakeLists.txt
 

** Now add dependency OpenCV in line number 7 ;

find_package(catkin REQUIRED COMPONENTS
 roscpp
 rospy
 std_msgs
 OpenCV
 )

** also add following lines at the bottom of your CmakeLists.txt ;

 add_executable(read_image src/read_image.cpp)
 target_link_libraries(read_image ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

 

/* read_image is my node name which i am going to run .I will make this node in step 8 */

important : * save the changes in CmakeLists.txt and close it.
Step 7 : changes in package.xml .
Write following commands in new terminal, for opening package.xml

cd ~/catkin_ws/src/ros_opencv_interface/
 gedit package.xml

** add the line :

 <rosdep name="opencv2"/>

below <!– Other tools can request additional information be placed here –>
which is written in your package.xml and save it and close it.
Step 8 : Now create a empty gedit document in “src” folder of your package ( path /home/user/catkin_ws/src/ros_opencv_interface/src) and name it as read_image.cpp

Now paste the following code in read_image.cpp and save it.

#include<opencv2/highgui/highgui.hpp>
using namespace cv;

int main()
{

Mat img = imread("/home/aniket/Downloads/k.jpg",CV_LOAD_IMAGE_COLOR);
 imshow("opencvtest",img);
 waitKey(0);

return 0;
}

**remember add your image path in imread.

Step 9 : Now open new terminal write following commands.

cd ~/catkin_ws/
 catkin_make

 

** this should build your all files with no error.
then finally run your node by following commands

 source devel/setup.bash
 rosrun ros_opencv_interface read_image

 

// your image should pop ups…

Done … !!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s