Compile and Run OpenCV2 on Banana Pi

About OpenCV

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. Wikipedia OpenCV

Install and use OpenCV library is very easy. In this article, I will use Banana Pi M2 with Debian system to demonstrate how to install OpenCV library , Writing and running a program use OpenCV library.

Install OpenCV Only two commands to install OpenCV.

    sudo apt-get update
    sudo apt-get install libcv-dev

If there is no problem OpenCV will be installed to your Banana Pi.

Programming and run

  1. A display OpenCV library version example

     /*Display OpenCV library version*/
     #include <opencv2/core/core.hpp>
     #include <stdio.h>
     using namespace cv;
     int main()
     {
         printf("OpenCV version is:%s\n",CV_VERSION);
         return 0;
     }
    

Save the above code to “opencv_ver.cpp” file. Then,Run the following command to compile this program.

g++ -o opencv_ver ./opencv_ver.cpp -lopencv_core

If no error will generate an executable file.

Run “opencv_ver”

The program output the OpenCV library version.

  1. Output camera image example

/Use the camera,the image is displayed on the window/ #include<opencv2/highgui/highgui.hpp> #include<opencv2/core/core.hpp>

        using namespace cv;
        int main()
        {
        VideoCapture cap(0);//Open camera
        if(!cap.isOpened())return -1;
        Mat frame;
        while(1){
        cap>>frame;//Read a frame
        imshow("XLu OpenCV CAM", frame);
        if(waitKey(20) >=0) break;
        }
        return 0;
        }

Save as “opencv_cam.cpp” and compile.

g++ -o opencv_cam ./opencv_cam.cpp -lopencv_core -lopencv_highgui

Notice:This program can not be run on the SSH client!

This program must be run in support of the window that displays the desktop environment.

Screenshot

  1. Blur filter and canny edge detection

#include<opencv2/highgui/highgui.hpp> #include<opencv2/core/core.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace cv; int main() { VideoCapture cap(0);//Open camera if(!cap.isOpened())return -1; Mat frame; while(1){ cap>>frame;//Read a frame cvtColor(frame,edges,CV_BGR2GRAY);//Convert the image to grayscale //cvtColor(frame,edges,COLOR_BGR2GRAY);//For OpenCV3 library blur(edges,edges,Size(7,7));//Noise Reduction Canny(edges,edges,3,9,3);//Canny edge detection imshow(“XLu OpenCV Canny”, edges); if(waitKey(20) >=0) break; } return 0; }

Save as “opencv_canny.cpp” and compile it.

g++ -o opencv_canny ./opencv_canny.cpp -lopencv_core -lopencv_highgui -opencv_imgproc

Run run run~~!

A book! Thank you for reading:relaxed:

2 Likes

so cool, and you can format you codes