Raspberry Pi Learning notes-source code mode installation OpenCV

Source: Internet
Author: User
Tags config mkdir zip

0. Preface

This article describes how to install OpenCV in the Raspberry Pi by compiling the source code, and a simple example of how to use OpenCV. For more information, please refer to-"Raspberry Pi Study notes-index Blog"
1. Download several dependencies it is a good idea to update the Raspberry Pi software source before starting the installation. If the update time is too long, please refer to the blog post to modify the software source network address--"Raspberry Pi Study notes-Modify the Raspberry Pi software source" sudo apt-get update
Install the following dependencies in turn, which must be installed on the OPENCV official website, which can be installed in most Debian systems. In fact, Build-essential has been installed by default in Raspberry Pi, but other content such as CMake still needs to be installed manually. sudo apt-get install build-essential sudo apt-get install cmake sudo apt-get install libgtk2.0-dev sudo apt-get install pk G-config sudo apt-get install python-dev python-numpy sudo apt-get install Libavcodec-dev libavformat-dev libswscale-dev l Ibjpeg-dev Libpng-dev Libtiff-dev Libjasper-dev
2. Get the source code to create the OpenCV folder in the Raspberry Pi user directory mkdir OpenCV
To get OpenCV source code from sourceforge website, this article chooses 2.4.9 version. The download process may be slow, please wait patiently. wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download Opencv-2.4.9.zip
Extract the source code and get a folder named opencv-2.4.9 unzip Opencv-2.4.9.zip
3. Compile the source code into the OpenCV source directory and create a new folder named release in the directory. CD ~/opencv/opencv-2.4.9 mkdir release CD release use the CMake tool to generate makefile files in this folder, specify OPENCV installation directory for/USR/LOCAL,OPENCV source code located on A level directory.     Cmake-d cmake_build_type=release-d cmake_install_prefix=/usr/local. Compile the source code and install it (compile the source code for a very long time, I will sleep after make, do install the next morning) make sudo make install update search dynamic link library sudo ldconfig
4. Example "C + +"-load a picture by code, convert the color picture to black and white by OPENCV, and output the original image and the converted picture to the screen.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ highgui/highgui.hpp>
#include <iostream>
using namespace CV;
using namespace std;
int main (int argc, char **argv)
{
    Mat image, Image_gray;
    Image = Imread (argv[1], cv_load_image_color);
    if (argc! = 2 | |!image.data) {
        cout << "No image data\n";
        return-1;
    }
   
    Cvtcolor (image, Image_gray, cv_rgb2gray);
    Namedwindow ("image", cv_window_autosize);
    Namedwindow ("Image Gray", cv_window_autosize);
   
    Imshow ("image", image);
    Imshow ("Image Gray", image_gray);
   
    Waitkey (0);
    return 0;
}
"Makefile"
INCLUDE = $ (Shell pkg-config--cflags opencv)
LIBS = $ (Shell pkg-config--libs opencv)
SOURCES = test.cpp
# target File
OBJECTS = $ (SOURCES:.CPP=.O)
# executable file
TARGET = Test
$ (target): $ (OBJECTS)
    g++-O $ (TARGET) $ ( OBJECTS)-I $ (INCLUDE) $ (LIBS)
$ (OBJECTS): $ (SOURCES)
    g++-C $ (SOURCES) Clean
:
    RM $ (OBJECTS) $ ( Target)
# Compile Rule $@ on behalf of the target file $< represents the first dependent file
%.o:%.cpp
    g++-I $ (INCLUDE)-O-$@-C $<
"Brief description" Get OPENCV related header file path through Pkg-config--cflags OpenCV, Pkg-config--libs OpenCV by OpenCV to learn more makefile knowledge See-"L Inux study Notes--an example of Makefile index post "compiling and executing" make./test raspberry.jpg example loads a picture named Raspberry.jpg. In this example, the picture and the executable file test should be in the same directory. The result of the operation is as shown in the image below, the original picture on the left and the converted picture on the right. The selected picture is not rich enough, the contrast between the color picture and the black and white picture is not strong enough, but it can also prove that the OPENCV is running normally in the Raspberry Pi.
Figure 1 Running results 5. Summary Raspberry Pi installation OpenCV is not difficult, traps are not many but take longer, need to maintain enough patience.
Reference "Installation in Linux" "Installation of OpenCV Raspberry PI"


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.