Setup Ubuntu OpenCV 2 4 9

15 389 0
Setup Ubuntu OpenCV 2 4 9

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Setup OpenCV 2.4.9 Eclipse Ubuntu. Install tool for compile OpenCV 2. Setup Environment variables for OpenCV 3. Configure in Visual Studio and simple example. 4. Result   I. Install tools for compile OpenCV 1. Visual Studio Download and install Eclipse Ubuntu . It’s free and choosing all default options will work fine 2. OpenCV 3.1.0 Goto http:opencv.org and download the OpenCV latest version 2.4.9 for Windows. Then set Extract to: to your C: directory. II. Setup Environment variables for OpenCV Goto “My Computer”, click ringt mouse select “Properties

Note: I have noticed some copies of my posts elsewhere, so make sure that you are reading this from the original source, at samontab dot com, accessible from here so that you don’t miss the comments OK, so the first step is to make sure that everything in the system is updated and upgraded Open the terminal and write this: sudo apt-get update sudo apt-get upgrade Now, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, other libraries, etc… This step is very easy, you only need to write the following command in the Terminal: sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libj Time to get the OpenCV 2.4.9 source code: cd ~ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/open unzip opencv-2.4.9.zip cd opencv-2.4.9 Now we have to generate the Makefile by using cmake In here we can define which parts of OpenCV we want to compile Since we want to use the viz module, Python, Java, TBB, OpenGL, Qt, work with videos, etc, here is where we need to set that Just execute the following line at the terminal to create the appropriate Makefile Note that there are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent) mkdir build cd build cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C Check that the above command produces no error and that in particular it reports FFMPEG as YES 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 Make sure you scroll up and check that the modules that are going to be built are these: core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib java python stitching superres ts videostab viz If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again 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 Run the following code to configure the library: sudo ldconfig Now you have to open another file: 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 console and open a new one, restart the computer or logout and then login again OpenCV will not work correctly until you this Now you have OpenCV 2.4.9 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support Check out the cool Qt interface which provides image viewing capabilities with zoom, as well as the ability to save the current image with just one click If you zoom in enough, you can see the RGB (or intensity) values for each pixel Now let’s build some samples included in OpenCV: cd ~/opencv-2.4.9/samples/c chmod +x build_all.sh /build_all.sh These examples use the old C interface: /facedetect cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_fronta /facedetect cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_fronta The following examples use the new C++ interface: ~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jp ~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial Now let’s run some Python code: python ~/opencv-2.4.9/samples/python2/turing.py Now you can build a Java sample using ant for example (Make sure that you change /home/samontab/ with your actual home directory): cd ~/opencv-2.4.9/samples/java/ant ant -DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samonta Now let’s read a video and use OpenGL with Qt through this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses OpenGL to draw in 3D (great sample Javier): cd ~/opencv-2.4.9/samples/cpp/Qt_sample mkdir build cd b cmak make /OpenGL_Qt_Binding And finally, let’s build a sample using the 3D visualization module viz: cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz g++ -o widget_pose `pkg-config opencv cflags` widget_pose.cpp `pkg-config ope /widget_pose As you can see, now you can use OpenCV with C++, C, Python, and Java The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module Multi threading functionality is enabled using TBB Also, video support is enabled as well If you correctly finished this tutorial, you are now ready to some nice computer vision work Making a project Start Eclipse Just run the executable that comes in the folder Go to File -> New -> C/C++ Project Choose a name for your project (i.e DisplayImage) An Empty Project should be okay for this example Leave everything else by default Press Finish Your project (in this case DisplayImage) should appear in the Project Navigator (usually at the left side of your window) 6 Now, let’s add a source file using OpenCV: o Right click on DisplayImage (in the Navigator) New -> Folder o Name your folder src and then hit Finish o Right click on your newly created src folder Choose New source file: o Call it DisplayImage.cpp Hit Finish So, now you have a project with a empty cpp file Let’s fill it with some sample code (in other words, copy and paste the snippet below): #include #include 10 11 using 12 namespace cv; 13 int main( int argc, char** 14 { 15 Mat image; 16 image = imread( argv[1], argv ) ); 17 18 19 20 if( argc != || !image.data ) { printf( "No image data \n" ); 21 22 return -1; } 23 24 25 namedWindow( "Display Image", CV_WINDOW_AUTOSIZE ); imshow( "Display Image", image ); 26 27 waitKey(0); 28 29 30 } return 0; 31 We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are For this, the following: o Go to Project–>Properties o In C/C++ Build, click on Settings At the right, choose the Tool Settings Tab Here we will enter the headers and libraries info: In GCC C++ Compiler, go to Includes In Include paths(-l) you should include the path of the folder where opencv was installed In our example, this is /usr/local/include/opencv Note If you not know where your opencv files are, open the Terminal and type: pkg-config cflags opencv For instance, that command gave me this output: -I/usr/local/include/opencv -I/usr/local/include Now go to GCC C++ Linker,there you have to fill two spaces: First in Library search path (-L) you have to write the path to where the opencv libraries reside, in my case the path is: /usr/local/lib Then in Libraries(-l) add the OpenCV libraries that you may need Usually just the first on the list below are enough (for simple applications) In my case, I am putting all of them since I plan to use the whole bunch: opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann If you don’t know where your libraries are (or you are just psychotic and want to make sure the path is fine), type in Terminal: pkg-config libs opencv My output (in case you want to check) was: code-block:: bash -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann Now you are done Click OK o Your project should be ready to be built For this, go to Project->Build all In the Console you should get something like If you check in your folder, there should be an executable there Running the executable So, now we have an executable ready to run If we were to use the Terminal, we would probably something like: cd cd src /DisplayImage /images/HappyLittleFish.png Assuming that the image to use as the argument would be located in /images/HappyLittleFish.png We can still this, but let’s it from Eclipse: Go to Run->Run Configurations Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Application a couple of times) Select the name (in this case DisplayImage Debug) Now, in the right side of the window, choose the Arguments Tab Write the path of the image file we want to open (path relative to the workspace/DisplayImage folder) Let’s use HappyLittleFish.png: Click on the Apply button and then in Run An OpenCV window should pop up with the fish image (or whatever you used) 5 Congratulations! You are ready to have fun with OpenCV using Eclipse V2: Using CMake+OpenCV with Eclipse (plugin CDT) Say you have or create a new file, helloworld.cpp in a directory called foo: #include #include int main ( int argc, char **argv ) { cvNamedWindow( "My Window", ); IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, ); CvFont font; double hScale = 1.0; double vScale = 1.0; int lineWidth = 1; cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth ); cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font, cvScalar( 255, 255, ) ); cvShowImage( "My Window", img ); cvWaitKey(); return 0; } Create a build directory, say, under foo: mkdir /build Then cd build Put a CmakeLists.txt file in build: PROJECT( helloworld_proj ) FIND_PACKAGE( OpenCV REQUIRED ) ADD_EXECUTABLE( helloworld helloworld.cxx ) TARGET_LINK_LIBRARIES( helloworld ${OpenCV_LIBS} ) Run: cmake-gui and make sure you fill in where opencv was built Then click configure and then generate If it’s OK, quit cmake-gui Run make -j4 (the ``-j4`` is optional, it just tells the compiler to build in threads) Make sure it builds Start eclipse Put the workspace in some directory but not in foo or foo\\build Right click in the Project Explorer section Select Import And then open the C/C++ filter Choose Existing Code as a Makefile Project`` Name your project, say helloworld Browse to the Existing Code location foo\\build (where you ran your cmake-gui from) Select Linux GCC in the “Toolchain for Indexer Settings” and press Finish Right click in the Project Explorer section Select Properties Under C/C++ Build, set the build directory: from something like ${workspace_loc:/helloworld} to $ {workspace_loc:/helloworld}/build since that’s where you are building to You can also optionally modify the Build command: from make to something like make VERBOSE=1 -j4 which tells the compiler to produce detailed symbol files for debugging and also to compile in parallel threads Done! [...].. .21 22 return -1; } 23 24 25 namedWindow( "Display Image", CV_WINDOW_AUTOSIZE ); imshow( "Display Image", image ); 26 27 waitKey(0); 28 29 30 } return 0; 31 We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are For this, do the following: o Go to Project–>Properties o In... opencv_ contrib opencv_ legacy opencv_ flann If you don’t know where your libraries are (or you are just psychotic and want to make sure the path is fine), type in Terminal: pkg-config libs opencv My output (in case you want to check) was: code-block:: bash -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib... the opencv libraries reside, in my case the path is: /usr/local/lib Then in Libraries(-l) add the OpenCV libraries that you may need Usually just the 3 first on the list below are enough (for simple applications) In my case, I am putting all of them since I plan to use the whole bunch: opencv_ core opencv_ imgproc opencv_ highgui opencv_ ml opencv_ video opencv_ features2d opencv_ calib3d opencv_ objdetect opencv_ contrib... mkdir /build Then cd build 2 Put a CmakeLists.txt file in build: PROJECT( helloworld_proj ) FIND_PACKAGE( OpenCV REQUIRED ) ADD_EXECUTABLE( helloworld helloworld.cxx ) TARGET_LINK_LIBRARIES( helloworld $ {OpenCV_ LIBS} ) 1 Run: cmake-gui and make sure you fill in where opencv was built 2 Then click configure and then generate If it’s OK, quit cmake-gui 3 Run make -j4 (the ``-j4`` is optional, it just... cvNamedWindow( "My Window", 1 ); IplImage *img = cvCreateImage( cvSize( 640 , 48 0 ), IPL_DEPTH_8U, 1 ); CvFont font; double hScale = 1.0; double vScale = 1.0; int lineWidth = 1; cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, hScale, vScale, 0, lineWidth ); cvPutText( img, "Hello World!", cvPoint( 20 0, 40 0 ), &font, cvScalar( 25 5, 25 5, 0 ) ); cvShowImage( "My Window", img ); cvWaitKey(); return... Include paths(-l) you should include the path of the folder where opencv was installed In our example, this is /usr/local/include /opencv Note If you do not know where your opencv files are, open the Terminal and type: pkg-config cflags opencv For instance, that command gave me this output: -I/usr/local/include /opencv -I/usr/local/include 2 Now go to GCC C++ Linker,there you have to fill two spaces: First... relative to the workspace/DisplayImage folder) Let’s use HappyLittleFish.png: 4 Click on the Apply button and then in Run An OpenCV window should pop up with the fish image (or whatever you used) 5 Congratulations! You are ready to have fun with OpenCV using Eclipse V2: Using CMake +OpenCV with Eclipse (plugin CDT) Say you have or create a new file, helloworld.cpp in a directory called foo: #include ... Configurations 2 Under C/C++ Application you will see the name of your executable + Debug (if not, click over C/C++ Application a couple of times) Select the name (in this case DisplayImage Debug) 3 Now, in the right side of the window, choose the Arguments Tab Write the path of the image file we want to open (path relative to the workspace/DisplayImage folder) Let’s use HappyLittleFish.png: 4 Click on... check) was: code-block:: bash -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann Now you are done Click OK o Your project should be ready to be built For this, go to Project->Build all In the Console you should get something like If you check in your folder, there... sure you fill in where opencv was built 2 Then click configure and then generate If it’s OK, quit cmake-gui 3 Run make -j4 (the ``-j4`` is optional, it just tells the compiler to build in 4 threads) Make sure it builds 4 Start eclipse Put the workspace in some directory but not in foo or foo\\build 5 Right click in the Project Explorer section Select Import And then open the C/C++ filter Choose Existing

Ngày đăng: 23/03/2016, 15:20

Từ khóa liên quan

Mục lục

  • Making a project

  • Running the executable

    • V2: Using CMake+OpenCV with Eclipse (plugin CDT)

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan