Page 1 of 1

How to install OpenCV on Raspberry Pi?

Posted: Thu Jan 16, 2020 2:31 pm
by haroldd
Hello, I am new with OpenCV and I would like use this with a Raspberry Pi 3 B+ with Raspbian OS with a camera module or a USB-camera. I plan to use this to process live video, preferably using C++ instead of Python. I have attempted to follow these different tutorials (links)

https://docs.opencv.org/master/d7/d9f/t ... stall.html

https://www.learnopencv.com/install-ope ... pberry-pi/

https://www.reddit.com/r/RASPBERRY_PI_P ... 4_model_b/

but there are too many holes to fill in, so they are confusing and thus it's still not clear to me. I don't know how to verify if I installed it right. I have not yet been able to successfully build and run any OpenCV-program. I don't have experience with CMake or with library installation in Linux since I come from Windows. I really appreciate if someone can provide some concise and easy-to-follow instructions. Thank you

Re: How to install OpenCV on Raspberry Pi?

Posted: Thu Jan 16, 2020 9:22 pm
by Chris D
I just installed it a few weeks ago and now I am kicking myself for not taking notes!!!!

I believe this link is the one I used.

https://www.raspberrypi.org/forums/view ... 3&t=259426

This is the specific command I believe I used..

sudo apt install libopencv-dev opencv-doc

I recall there were some very convoluted installation methods, one of which I tried and it didn't work. I then found a suggestion (hopefully this one) that was a one-line installation and bingo, worked great.

If I find anything more substantial (or if my memory has a spark) I will update again.

Chris D

Re: How to install OpenCV on Raspberry Pi?

Posted: Thu Jan 16, 2020 10:25 pm
by ElEscalador
From your first link should work, with some minor tweaks.

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Then make a directory for it... from your /home/yourName directory would work:

mkdir opencv
cd opencv
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

now from this directory make a new one called build - that's where you build from:

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/yourName/opencv/opencv_contrib ..
make -j4
sudo make install

the last couple are going to take some time..have a coffee.

Once it's done (or to test - maybe your current installation worked!) make a directory wherever you want to keep your projects. Add a photo to it... I'll call it photo.jpg in the hello_cv.cpp below:

Code: Select all

#include <opencv2/core/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"

#include <opencv2/videoio.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

Mat get_frame();

int main()
{

  cout << "OpenCV version : " << CV_VERSION << endl;
  cout << "Major version : " << CV_MAJOR_VERSION << endl;
  cout << "Minor version : " << CV_MINOR_VERSION << endl;
  cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;
  std::cout << "Compiled at least" << std::endl;

    Mat image;

    image = imread("/home/yourpath/photo.jpg");
    
    //get_frame grabs from my usb cam inastead of reading jpg
    //image = get_frame();
    imwrite("/home/lloyd/yourpath/grabbed_image.jpg", image);

	//shows your image
    cv::imshow("windowName", image);
    
    //waitkey keeps window open for x milliseconds. 0 means until you press a key
    waitKey(0);


    return 0;
}

Mat get_frame()
{

    //use your /dev/videoDevice number - picam is 0, usb cam might be 0, 1, or 2
    VideoCapture video_capture(2);
    if (!video_capture.isOpened())
    {
        std::cout << "open failed!" << std::endl;
    }

    Mat frame;

    video_capture.read(frame);

    video_capture.release();
    return frame;
}
So that's the cpp code - forgive the messy cut and paste but it should work. Also in the same folder make your CMakeLists.txt file - case-sensitive: This is the contents of my test one:

Code: Select all

cmake_minimum_required(VERSION 2.8)
project( hello_cv )
find_package( OpenCV REQUIRED )

add_executable( hello_cv hello_cv.cpp )
target_link_libraries( hello_cv ${OpenCV_LIBS} )

#uncomment and use as template for future programs
#add_executable( my_other_file hello_my_other_file.cpp )
#target_link_libraries( my_other_file ${OpenCV_LIBS} )
Now - from whatever folder this .cpp and CMakeLsts.txt is in, make a new folder called build then run "make" from it:

mkdir build
cd build
make

That hopefully compiles the executables declared in your cmakelists.txt. It should generate file (in the build folder) with the same name, and you can run them from the build folder with ./hello_cv.

I hope that part about the building with cmake is all correct - it should be but I usually do my opencv stuff in ROS - that's a little different.

Re: How to install OpenCV on Raspberry Pi?

Posted: Tue Jan 28, 2020 2:54 am
by MacAttack
I am also trying to install OpenCV on a PI3B+ and had some issues. I followed the process that is detailed below.

https://www.pyimagesearch.com/2018/09/1 ... ll-opencv/

Aside from some small changes needed, no doubt due to time/software changes, it seems to work until I get to the part where the author recommends setting up virtual environments to run OpenCV in.

Code: Select all

[email protected]:~ $ sudo apt-get install libhdf5-dev libhdf5-dev libhdf5-103
I had to change the last bit to -103 from -100 (in the article).

Still it seems to work with a change needed to get things running (virtualenvwrapper.sh was not installed where it says in the article). I found my virtualenvwrapper.sh was placed in ...

Code: Select all

/home/pi/.local/bin/virtualenvwrapper.sh
My .bashrc script was changed accordingly.

The needed support packages in the article above seem much the same as in Chris D's link. Perhaps Chris D's method is the newer way to install OpenCV without the virtual environment option (that is the same as Option A, not recommended, in the article above). I just don't know. The method in the article seemed to work for me as i did get this message after following the (changed) instructions.

Code: Select all

sudo pip install opencv-contrib-python==4.1.0.25
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting opencv-contrib-python==4.1.0.25
  Downloading https://www.piwheels.org/simple/opencv-contrib-python/opencv_contrib_python-4.1.0.25-cp37-cp37m-linux_armv7l.whl (15.7 MB)
     |████████████████████████████████| 15.7 MB 11 kB/s 
Requirement already satisfied: numpy>=1.16.2 in /usr/lib/python3/dist-packages (from opencv-contrib-python==4.1.0.25) (1.16.2)
Installing collected packages: opencv-contrib-python
Successfully installed opencv-contrib-python-4.1.0.25
Perhaps Chris or ElEscalador can comment on the need, or lack thereof, setting up and using a virtual environment. I'd like to know myself. :)

And now I can finally get around to seeing if my RPi3B with a Picam can detect the cue ball on a pool table.

Re: How to install OpenCV on Raspberry Pi?

Posted: Thu Feb 13, 2020 4:53 am
by MacAttack
Still need help.

It seems that the above (my post) did not install openCV as I'm still getting the above error;

Code: Select all

[email protected]:~ $ workon cv
(cv) [email protected]:~ $ python
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>> 
I tried to find a file named 'cv2.so' (info from other searches on this problem) but could only find this instead ;

/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so

Since I doubt the above was on my path I altered my .bashrc file to add it and my path now looks like;

Code: Select all

[email protected]:~ $ echo $PATH
/home/pi/.virtualenvs/cv/bin:/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/local/lib/python3.7/dist-packages/cv2
It didn't help. So I'm at a loss to continue. Either I don't have openCV installed or I do and can't get to it. It's been a couple of decades since I worked with Unix so I'm a bit lost on how to find and cure even basic problems. Any/all help is appreciated.

Re: How to install OpenCV on Raspberry Pi?

Posted: Thu Feb 13, 2020 10:22 pm
by il_diavolo
Have you tried renaming "cv2.cpython-37m-arm-linux-gnueabihf.so" to "cv2.so"? I'm sure that's what I did.

Re: How to install OpenCV on Raspberry Pi?

Posted: Sat Feb 15, 2020 12:09 am
by MacAttack
Have you tried renaming "cv2.cpython-37m-arm-linux-gnueabihf.so" to "cv2.so"? I'm sure that's what I did.
Thanks but that didn't work either. I got the same old error message. The author of the original article I linked to seemed to poo-poo the usage of NOOBS to install the OS and the latest version of Raspian (Buster). I may have to format another SD card and try again with the prior version of the OS. Or try the install process that Chris used (no virtual environment manager). The thing is that I'm sure openCV is actually installed, I just can't get it to run. Python can't seem to find the needed file, which makes me believe (in my simplistic understanding) that the name and/or path needs work. And yet it would seem that I've (now) "fixed" those.

FWIW - this was all done on a clean SD card; NOOBS, python, pip, OpenCV.