This article has been transferred to a new blog, please click here (https://linzichun.com/posts/use_picam_with_opencv/) to view the updated article.
Thank you !!!
————————————————————————-
Normally OpenCV only supports USB camera for raspberry pi, if you want to use OpenCV with Pi camera, I have a nice guide for you.
Before we set the Pi camera, you have to install OpenCV library firstly, you can follow my previous guide.
I use the RaspiCam library from Rafael Muñoz Salinas (very good job, easy installation and fast speed), it provides C++ API for us, frame per second is 30.
1. Plug Pi camera into Raspberry Pi.
2. Download RaspiCam library into your Pi.
3. Install the library.
tar xvzf raspicam-0.1.3.tgz (uncompress the file) cd raspicam-0.1.3 (go to the library folder) mkdir build cd build cmake .. make sudo make install
4. Testing
The library provides cmake to compile the program, but it’s not convenient for me, so I wrote makefile to compile the program.Here is the makefile:
LIBS = -I/usr/local/include/ CFLAGS = -lraspicam -lraspicam_cv -lopencv_core -lopencv_highgui objects= main.o camera.o picam: $(objects) g++ $(objects) -o picam $(LIBS)$(CFLAGS) main.o: main.cpp camera.h g++ main.cpp -c camera.o: camera.cpp camera.h g++ camera.cpp -c .PHONY: clean clean: rm picam $(objects)
The library provides some examples to us, I rewrote the sample code, actually we also can use OpenCV C API(I transfer cv::Mat to IplImage in sample code).
Here is sample code(GitHub), download to your raspberry pi, make and run the program, FPS is 30(CPU usage is 22%), amazing!
Hello,
thanks for yours great tutorial, but at the last step, I am not sure where I have to put yours files from GitHub. So, I have got some errors with “make” command. Probably, they are caused by wrong final directory.
g++ main.cpp -c
main.cpp:4:34: fatal error: raspicam/raspicam_cv.h: No such file or directory
#include
^
compilation terminated.
Makefile:9: recipe for target ‘main.o’ failed
make: *** [main.o] Error 1
Please, can you help me with this problem?
Dominik
Best regards.
LikeLiked by 1 person
your raspberry pi version? your OS version? You also need to make sure step 3 is done correctly, testing program is in raspicam-0.1.3 folder, you can try.
LikeLike
After step 3, you can try to type command “sudo ldconfig”
LikeLike
Thank you for your reply, but finally, I solved this problem few days ago (it was my mistake of course). I copied folder contained “camera.cpp”, “camera.h”, “main.cpp”, and “Makefile” in main directory instead build one. Then I collected all necessary libraries in one folder and link them all in each .cpp and .h file, because I had problem with adressing correct folders of libraries. But unfortunately, now, after compile command I have some errors.
http://s23.postimg.org/vsei243m3/error.png – error list in Geany, which I am using
http://s11.postimg.org/f86majhqr/build_commands.png – setting of build commands (I am not sure what I need to edit here)
I would appreciate it, if you help me with this problem. I now that, I have got something wrong, but now, I am confused.
LikeLike
sorry, image resolution is too low, can not see detail. Try not to compile in Geany, follow the step and do everything in Terminal
LikeLike
Ok, so I tried steps again. I got one of this notice in terminal although it finished without error.
CMake Warning at CMakeLists.txt:51 (FIND_PACKAGE):
By not providing “FindOpenCV.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “OpenCV”, but
CMake did not find one.
Could not find a package configuration file provided by “OpenCV” with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of “OpenCV” to CMAKE_PREFIX_PATH or set
“OpenCV_DIR” to a directory containing one of the above files. If “OpenCV”
provides a separate development package or SDK, be sure it has been
installed.
I tried to copy all cmake files from Opencv/cmake folder to raspicam or raspicam/build folder but it is still showing same notice.
LikeLike
https://neverbenever.wordpress.com/2014/09/19/install-opencv-on-raspberry-pi/ This is a guide that how do I install OpenCV, is yours same as this one? Finish it without error. but have warning, so it can compile now?
LikeLike
Finally, I resolved it by re-installing OpenCV and everything is working. Thank you very much and I wish you happy and successful new year. 🙂
Dominik
LikeLike
Hello, I’m trying to run make in step 3 and I get the result below:
pi@raspberrypi:~/raspicam-0.1.6/build $ make
[ 5%] Building CXX object src/CMakeFiles/raspicam_cv.dir/raspicam_cv.cpp.o
[ 11%] Building CXX object src/CMakeFiles/raspicam_cv.dir/raspicam_still_cv.cpp.o
make[2]: *** No rule to make target ‘/home/pi/opencv/release/lib/libopencv_core.so.3.2.0’, needed by ‘src/libraspicam_cv.so’. Stop.
CMakeFiles/Makefile2:119: recipe for target ‘src/CMakeFiles/raspicam_cv.dir/all’ failed
make[1]: *** [src/CMakeFiles/raspicam_cv.dir/all] Error 2
Makefile:127: recipe for target ‘all’ failed
make: *** [all] Error 2
pi@raspberrypi:~/raspicam-0.1.6/build $
Can you help me with this problem?
br
zeb
LikeLike
After cmake .. you see something like this?
— CREATE OPENCV MODULE=1
— CMAKE_INSTALL_PREFIX=/usr/local
— REQUIRED_LIBRARIES=/opt/vc/lib/libmmal_core.so;/opt/vc/lib/libmmal_util.so;/opt/vc/lib/libmmal.so
— Change a value with: cmake -D=
—
— Configuring done
— Generating done
— Build files have been written to: /home/pi/raspicam/trunk/build
if your result is exactly like above, it should be ok.
Make sure your OpenCV is installed correctly. By the way, I am not sure OpenCV 3.2.0 is compatible with raspicam, but I guess it’s ok.
LikeLike
Thanks for the reply.
I get this after cmake ..
pi@raspberrypi:~/raspicam-0.1.6/build $ cmake ..
— ——————————————————————————-
— GNU COMPILER
— ——————————————————————————-
— Adding cv library
—
— ——————————————————————————-
— General configuration for raspicam 0.1.6
— ——————————————————————————-
—
Built as dynamic libs?:ON
Compiler:/usr/bin/c++
— C++ flags (Release): -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections -fomit-frame-pointer -O2 -ffast-math -DNDEBUG -lpthread
— C++ flags (Debug): -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections -g3 -O0 -DDEBUG -D_DEBUG -W -Wextra -Wno-return-type -lpthread
— CMAKE_CXX_FLAGS: -std=c++0x -Wl,–no-as-needed -Wall -ffunction-sections
— CMAKE_BINARY_DIR: /home/pi/raspicam-0.1.6/build
—
— CMAKE_SYSTEM_PROCESSOR = armv7l
— BUILD_SHARED_LIBS = ON
— BUILD_UTILS = ON
— CMAKE_INSTALL_PREFIX = /usr/local
— CMAKE_BUILD_TYPE = Release
— CMAKE_MODULE_PATH = /usr/local/lib/cmake/;/usr/lib/cmake
—
— CREATE OPENCV MODULE=1
— CMAKE_INSTALL_PREFIX=/usr/local
— REQUIRED_LIBRARIES=/opt/vc/lib/libmmal_core.so;/opt/vc/lib/libmmal_util.so;/opt/vc/lib/libmmal.so
—
—
— Change a value with: cmake -D=
—
— Configuring done
— Generating done
— Build files have been written to: /home/pi/raspicam-0.1.6/build
It seems to be ok. Strange that I fail to run make.
Can you recommend a version of opencv and raspicam that works together?
br
Zeb
LikeLike
the log looks ok.
I tried 2.4.9 and 3.0, both are ok. You should know reinstall opencv need some time. use “sudo make” instead “make”, try again.
LikeLike
Sir i am getting 13 fps with 30 fps camera raspberry pi .i am using raspberry pi zero w .is that all the fps i will get ?
what are the fps of your camera ? you are getting almost 30 fps
LikeLike
I used official Pi camera(https://www.raspberrypi-spy.co.uk/2013/05/the-official-raspberry-pi-camera-module/), and I used Pi 2, I haven’t tried pi zero W, I notice that pi zero w and pi 2 have different cpu and RAM specification, it may cause low fps
LikeLike
i am using your example code
https://github.com/zcsd/RaspiCam
LikeLike
Thanks Sir for answering and also for great tutorial
LikeLike