Running PiCamera on Ubuntu in C++ - c++

I'm trying to get my PiCamera Module v2.1 running on my RaspberryPi4. Unfortunately I must install the Ubuntu 19.10 64bit distribution. So far so good.
I've installed Opencv4. There was some big trouble because it seems like Ubuntu does not come with VideoCore, raspi-config etc. So I downloaded and updated my firmware with sudo rpi-update and installed userland.
First I tried to open the camera with Videocapture cap(0), but this throws a bunch of errors (see here Ubuntu 19.10: Enabling and using Raspberry Pi Camera Module v2.1) and I've read that this is only for usb cameras (actually i can't believe this, because under Raspbian, I can use the module like this)
So I googled and found this repo https://github.com/cedricve/raspicam. I've installed it, but even with this I cannot get it running.
Again here is what I've down:
install opencv4
update firmware
install userland
writing start_x=1 and "gpu_mem=128" to /boot/firmware/config.txt
doing modprobe bcm2835-v4l2
sudo vcgencmd get_camera results in supported and detected = 1
When I use sudo raspistill -o test.jpg a window opens and the image is saved. But there are some errors:
mmal: mmal_vc_shm_init: could not initialize vc shared memory service
mmal: mmal_vc_component_create: failed to initialise shm for 'vc.camera_info' (7:EIO)
mmal: mmal_component_create_core: could not create component 'vc.camera_info' (7)
mmal: Failed to create camera_info component
Also I need to start it with sudo, although I've run sudo usermod -a -G video ubuntu several times (also rebooted). Strange, isn't?
My example script for accessing the camera is:
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std;
int main ( int argc,char **argv ) {
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
//set camera params
Camera.set( cv::CAP_PROP_FORMAT, CV_8UC1 );
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
//Start capture
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
for ( int i=0; i<nCount; i++ ) {
Camera.grab();
Camera.retrieve ( image);
if ( i%5==0 ) cout<<"\r captured "<<i<<" images"<<std::flush;
}
cout<<"Stop camera..."<<endl;
Camera.release();
}
Compilation is successful:
sudo g++ stream.cpp -I/usr/local/include/opencv4 -I/usr/local/include -L/usr/local/lib -L/opt/vc/lib -lraspicam_cv -lopencv_core -lraspicam -lmmal -lmmal_core -lmmal_util -lopencv_highgui -lmmal_vc_client -lvcos -lbcm_host -o stream
Executing stream (even with sudo) results in:
Opening Camera...
mmal: mmal_component_create_core: could not find component 'vc.ril.camera'
Failed to create camera componentopen Failed to create camera component/home/raspicam/src/private/private_impl.cpp 103
Error opening the camera
Does anyone have an idea what I can try?
Thanks !

I had this error while compiling a ROS node for the raspicam.
I fixed it by adding the following to my CMakeLists.txt:
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-as-needed")
The issue was that the reference to the library containing the 'vc.ril.camera' was optimized out by the linker and count not be found at run time.
Hopefully it will work for you.

Related

Opencv raspberry pi 3 video play c++

Im currently working on video processing project on raspberry pi 3 using OpenCV libraries. As a guide im reading opencv2 computer vision application programming cookbook. If you are familiar with this book, it explains everything on windows visual studio. But im able to compile things using cmake. And everything works fine.
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
int main()
{
// Open the video file
cv::VideoCapture capture("../bike.avi");
// check if video successfully opened
if (!capture.isOpened()){
std::cout<<"Error loading video!.."<<std::endl;
return 1;
}
// Get the frame rate
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
In the book writer uses this code. And i know this code works on linux windows etc but not on raspberry pi. I changed bike.avi with a video that i recorded with raspicam. raspivid -o bike.h264 -h 620 -w 480 -fps 15. But i still get Error loading video!...
Ps: i can play bike.avi video that i downloaded from books website via vlc player using ssh -X.
My CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project(salt)
FIND_PACKAGE(OpenCV REQUIRED)
add_executable(a.out main.cpp)
TARGET_LINK_LIBRARIES(a.out ${OpenCV_LIBS})
I figured out the problem. OpenCV with usb webcams work fine on raspberry pi. But when it comes using raspverry pi camera, its not supported. That is why some developers created RaspiCam libraries which works together OpenCV. They even provide cmake configurations. I installed it and capturing video around 25fps working great. This solution is for C++ users. If u are coding with python, just search python raspberry pi camera OpenCV.

How Disable Auto-focus of a webcam using opencv

I am trying to calibrate two cameras but my problem is the auto-focus. Im using the webcam logitech c920. Anyone knows a way to disable the auto-focus feature?? Im using C++ and opencv 2.4.9 in osx system.
You can try this.
cap = cv2.VideoCapture(1) // Generate camera object
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0) // turn the autofocus off
You can find more information on how to set some properties at these links.
The VideoCapture class
http://docs.opencv.org/3.2.0/d8/dfe/classcv_1_1VideoCapture.html
The VideoCapture properties
http://docs.opencv.org/3.2.0/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d
Try v4l-utils:
Install: sudo apt-get install v4l-utils
Find your device v4l2-ctl --list-devices
Replace video0 with output from the previous command and disable autofocus with:
v4l2-ctl -d /dev/video0 --set-ctrl=focus_auto=0

Image Capture with OpenCV - Select Timeout Error

I would like to save a captured image from my webcam attached BeagleBone Black, however when I run the following code, I get the error: select timeout.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
using namespace std;
using namespace cv;
int main(){
cout<< "Hello, OpenCV version: "<< CV_VERSION << endl;
Mat frame;
VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH , 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT , 240);
cap >> frame;
imwrite("camCap.jpg", frame);
return 0;
}
When I compile and run the code:
root#beaglebone:/home/iroh/test# g++ test.cpp -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -o test
root#beaglebone:/home/iroh/test# ./test
Hello, OpenCV version: 3.1.0
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != NULL) in Mat, file /home/iroh/Downloads/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp, line 410
terminate called after throwing an instance of 'cv::Exception'
what(): /home/iroh/Downloads/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp:410: error: (-215) total() == 0 || data != NULL in function Mat
Aborted
What can be the cause of that problem?
According to the error output, my frame is empty. It is first time I attached this webcam to BeagleBone Black. It works properly on my PC, but I do not know whether it is working successfully with BeagleBone Black.
Here is the dmesg output:
[ 4276.642456] usb 1-1: new high-speed USB device number 2 using musb-hdrc
[ 4277.411212] usb 1-1: New USB device found, idVendor=1e4e, idProduct=0110
[ 4277.411257] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 4277.411284] usb 1-1: Product: USB2.0 Camera
[ 4277.411310] usb 1-1: Manufacturer: Etron Technology, Inc.
[ 4277.532768] uvcvideo: Found UVC 1.00 device USB2.0 Camera (1e4e:0110)
[ 4277.542871] input: USB2.0 Camera as /devices/platform/ocp/47400000.usb/47401c00.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.0/input/input1
[ 4277.547293] usbcore: registered new interface driver uvcvideo
[ 4277.547322] USB Video Class driver (1.1.1)
What shall I do?
Thanks!
I was running into this same issue and managed to fix it.
Check my answer here
I met the same issue, I guess your working environment was Ubuntu system.
I changed my virtual machine setting "USB2.0 -> USB3.0".
Hope it helps you!

Can't run an executable file in matlab with system(), but it works in command line

I need to run a C++ executable file which uses the OPencv library using the system command in Matlab. I already compile all the code with make and it was successful. When I run the program in command line it works as shown below.
lib/sift/bin/siftfeat -x -o tampered1.txt tampered1.jpg
Finding SIFT features...
Found 2596 features.
The problem is when I run it with matlab. The following error happens:
>>system('lib/sift/bin/siftfeat -x -o tampered1.txt tampered1.jpg')
Finding SIFT features...
Error: unable to load image from tampered1.jpg
lib/sift/bin/siftfeat -x -o tampered1.txt tampered1.jpg: Aborted
The error happens in lib/sift/src/siftfeat.c in the following piece of code:
fprintf( stderr, "Finding SIFT features...\n" );
img = cvLoadImage( img_file_name, 1 );
if( ! img )
fatal_error( "unable to load image from %s", img_file_name );
Why does this happen? I mean, if the code runs in command line, why it doesn't run with system Matlab command? doesn't matlab recognize the cvLoadImage function? I am using Matlab R2014a and OPencv-2.4.9. This code was copied from another PC and I just recompiled it. In the original PC in which the code was copied the system command works.
You are running the system()-cmd in the wrong folder.
While you call your binary with an absolute path, the path to your images are relative.
Therefore, the images need to be in the folder, where matlab executes your system()-cmd.
You can simply check this by executing:
>> system('pwd')
"Folder/where/matlab/exec/your/cmd"
If the output corresponds to your image location, everything should be fine, otherwise you have to give your program the absolute path like this:
>> system('lib/sift/bin/siftfeat -x -o Folder/to/your/txt/tampered1.txt Folder/to/your/image/tampered1.jpg')
I tried it, and found that using sudo will work.

Unable to capture form a web camera using OpenCV 2.3.1

I try to run the example shown here. Unfortunately it dosen't work. The camera won't be open. The program fails on line
VideoCapture cap(0);
if(!cap.isOpened())
return -1; // on this line the program fails
I'm sure that the camera works becouse when I run a program to capture images (cheese) everything works fine.
I using 64bit ubuntu 11.04
I compile the program with the following comand
g++ -Wall -o my_program my_program.cpp -lopencv_highgu
It seems like there were no camera. But in other programs the camera works.
Any ideas why it dosen't work?
To solve the problem I upgraded my ubuntu from 11.04 to 11.10.
After that the camera works fine.