Difference between revisions of "Explorer/RaspberryPi/Visualprocessing/Opencv"

From PaparazziUAV
Jump to navigation Jump to search
Line 53: Line 53:
  '4.3.0'
  '4.3.0'


create test.cpp
  #include <opencv2/opencv.hpp>
  #include <opencv2/opencv.hpp>
  #define WIDTH 640
  #define WIDTH 640

Revision as of 10:27, 26 June 2020

  • Installation

PIZero installation > 10 hours
PI3 installation > 4 hours

sudo apt-get install -y cmake

sudo apt-get install -y python-numpy python3-numpy libpython-dev libpython3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libtiff-dev zlib1g-dev libjpeg-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

sudo apt-get install -y libatlas-base-dev gfortran
for raspberry pi3
sudo apt-get install -y libgtk2.0-dev
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git 
cd ~/opencv 
git checkout 4.3.0
cd ~/opencv_contrib
git checkout 4.3.0
mkdir ~/opencv/build
cd ~/opencv/build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=OFF \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
   -D OPENCV_GENERATE_PKGCONFIG=YES \
   -D BUILD_TESTS=OFF \
   -D BUILD_PERF_TESTS=OFF \
   -D BUILD_EXAMPLES=OFF ..

=> GStreamer: YES (1.14.4)

/etc/dphys-swapfile
CONF_SWAPSIZE=2048

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
make & 
for raspberrypi3: make -j4&
sudo make install
sudo ldconfig -v
  • Check installation
python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.3.0'

create test.cpp

#include <opencv2/opencv.hpp>
#define WIDTH 640
#define HEIGHT 480
#define FPS 30
#define SCALE 3/2
using namespace cv;
using namespace std;
int main(int, char**)
{
 unsigned int dataSize = sizeof(unsigned char)*WIDTH*HEIGHT*SCALE;
 Mat imageIn(WIDTH*SCALE, HEIGHT, CV_8UC1);
 Mat imageOut(WIDTH,HEIGHT,CV_8UC3,Scalar(0,0,0));
 cout << getBuildInformation() << endl;
 string streamInGstStr="shmsrc socket-path=/tmp/camera2 ! video/x-raw,width="+to_string(WIDTH)+
  ",height="+to_string(HEIGHT)+",framerate="+to_string(FPS)+"/1,format=I420 ! appsink sync=true";
 string streamOutGstStr="appsrc ! shmsink socket-path=/tmp/camera3 wait-for-connection=false async=false sync=false";
 VideoCapture streamIn(streamInGstStr,CAP_GSTREAMER);
 VideoWriter  streamOut(streamOutGstStr,0,FPS/1,Size(WIDTH,HEIGHT),true);
 if (streamIn.isOpened() && streamOut.isOpened()) {
   while (true) {
     streamIn.read(imageIn);
     if (!imageIn.empty()) {
       memcpy(imageOut.data,imageIn.data,dataSize);
       streamOut.write(imageOut);
     }
   }
 }
 return 0;
}

g++ -g test.cpp -o test `pkg-config --cflags --libs opencv4` rm /tmp/camera3;./test