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
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!