Opencv raspberry pi 3 video play c++ - 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.

Related

VideoCapture::read always returns false

I'm running OS X El Capitan and trying to use OpenCV VideoCapture to read an .avi file. I've tried it on both opencv 2.4.1 and 3, both of which have the same outcome.
cv::VideoCapture capture("filename.avi");
cv::Mat currentFrame, prevFrame;
bool capPrevSuccess = capture.read(prevFrame);
bool capCurrSuccess = capture.read(currentFrame);
I've verified that the filename.avi is in the current working directory and I don't see any errors in the console.
I'm beginning to wonder if it's something with my machine. I have a similar problem running VideoReader in MATLAB on the same machine. I believe MATLAB uses OpenCV VideoReader as well, perhaps they're connected.
Error using VideoReader/init (line 619) Failed to initialize internal
resources.
Error in VideoReader (line 172)
obj.init(fileName);
EDIT: Looks like this has something to do with the video files I'm using. I downloaded an mp4 video, and had no trouble with it. Unfortunately I need to use the video files I'm using (all .avi), yet they all seem to not work with VideoCapture.

Access an IP camera with Raspberry Pi2, OpenCV and GStreamer: Gstremaer unable to start pipeline

I'm trying to get frames from an IP camera. I can get frames with FFMPEG or GStreamer from my Ubuntu 14.04 PC and omxplayer or gst-launch-1.0 from Raspberry Pi. But when I want to access camera with c++ OpenCV from Raspberry Pi, it is giving me an error about GStreamer pipeline;
(ocvip:2870): GStreamer-WARNING **: Failed to load plugin '/usr/local/lib/gstreamer-1.0/libgstrtsp.so': /usr/local/lib/gstreamer-1.0/libgstrtsp.so: undefined symbol: gst_rtsp_connection_set_tls_validation_flags
GStreamer Plugin: Embedded video playback halted; module u-uridecodebin, reported: URI was not accepted by any element
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in icvStartPipeline, file /opt/opencv-2.4.10/modules/highgui/src/cap_gstreamer.cpp, line 383
what(): /opt/opencv-2.4.10/modules/highgui/src/cap_gstreamer.cpp:383: error: (-2) GStreamer unable to start pipeline in function icvStartPipeline
Aborted
code:
const string videoAddress = "rtsp://user:password#xxx.xxx.x.xxx:xxx/cam/realmonitor?channel=1&subtype=1";
int main() {
cv::VideoCapture video;
cv::Mat im;
cv::namedWindow("IP", 1);
video.open(videoAddress);
if (video.isOpened()) {
video >> im;
if (!im.data) break;
cv::imshow("IP", im);
if (cv::waitKey(20) >= 0) break;
}
}
I can access camera with same rtsp address, omxplayer is showing normal gst-launch is showing frames rotated by 180 degrees. I've built GStreamer-1.0 using this page because prebuilt libraries hasn't found by cmake. I found this issue. There are two fixes int this bug but I'm not sure are these the correct solutions and solve my problem because I'm not accessing camera with video.open(0) so I couldn't do like video.open(IPAddress + CV_CAP_PVAPI)?
Thanks.

XIMEA: openCV 2.4.9 can not find Ximea camera although it works fine with other programs

Although XIMEA camera(MQ013CG-E2) is ok and it is working with sample simple demo programs(for Win x64) from ximea.com, the next code always print "ximea camera not found":
VideoCapture cap;
if (!cap.open(CV_CAP_XIAPI)) cout << "ximea camera not found";
the same for :
CvCapture* capture = cvCreateCameraCapture(CV_CAP_XIAPI);
if (!capture) cout << "ximea camera not found";
If simple Logitech web-camera is connected then code above shows it exists (with CV_CAP_ANY flag). But ximea-camera is unreachable from openCV. OpenCV is build using cmake with option "WITH_XIMEA" by minGW from official source.
Have someone ever read something from ximea camera via openCV? I know this kind of cameras is not very popular, I could not even find tag "ximea" here (creating new tag requires 1500 reputation so no "ximea" tag here too).
UPD: I can transfer info from camera to openCV via xiApi.h-functions. So camera definitely works.
UPD2: I have been provided today with new MSVS-project example from ximea, which includes new openCV 2.4.9 libs. It contains code:
capture = cvCaptureFromCAM(0); //0=default, -1=any camera, 1..99=your camera
if (!capture) cout << "no camera detected" << endl;
I compiled it in MSVS it and exe-file started to work with ximea cam as expected using opencv-libs provided by ximea. But when I am trying to compile the same code in Eclipse and use opencv-libs created by MinGW with "WITH_XIMEA" option, it always shows "no camera detected". I checked all settings in CMake, Eclipse project "compiler includes" and "MinGW linker libs" twice but still can not understand where is my mistake.
I spent some time checking here and there all settings and decided to compile openCV by MSVS, not by minGW. Then I got error with "xiExt.h" (I missed it while minGW compilation process), I googled it and found this bug report. Then I commented one line in source code, compiled it by minGW and.. it started to work!
It is only openCV 2.4.9 proplem since 2.4.10 does not contain this buggy #include "xiExt.h" anymore.

videocapture opencv (ubuntu 13.10, opencv 2.4.8)

when i tried to work with a video file i can't seem to open the file
when i test on isOpen() it gives me thats it did not open
what did i already check:
videofile working and in correct path
rebooting
reinstalling ffmpeg (with diferent configurations)
my code:
VideoCapture readVideo;
readVideo.open(argv[1]);
Mat frame;
if(!readVideo.isOpened()){
fprintf(stderr,"video niet geladen 0 \n");
return 2;
}
Example of the file i give in argv[1]:
out.avi (mpeg4 codex)
it works on someone elses setup so i know its not the code
The problem seemed to be with the installation of the ffmpeg that wasn't compleetly linked to the instalation of opencv, installed bouth again and worked like a charm

Possible bug in OpenCV2.4 capturing frames from video

Could it be that there is a bug in OpenCV2.4 highgui for capturing frames from video in windows?
I installed both the precompiled libraries, the ones compiled by me, I can compile everything perfectly and I can run my programs if
they are image based. The problem is only for videos. OpenCV crashes in this function always:
virtual IplImage* retrieveFrame(int)
{
unsigned char* data = 0;
int step=0, width=0, height=0, cn=0;
if(!ffmpegCapture ||
!icvRetrieveFrame_FFMPEG_p(ffmpegCapture,&data,&step,&width,&height,&cn)) <-------CRASHES HERE
return 0;
cvInitImageHeader(&frame, cvSize(width, height), 8, cn);
cvSetData(&frame, data, step);
return &frame;
}
This is inside the class cap_ffmpeg.cpp and is called by VideoCapture.
I tried versions 2.4.2 and 2.4.9. My programes were working finde with opencv2
More information
Windows 7
Build the projects with cmake (important as it could be that cmake is not building/finding the right codecs)
VisualStudio 9 2008
OpenCV 2.4.2
EDIT
It looks like it is actually a bug, so, how can I solve this problem and change my code to be able to read avi files?
As a temporary solution I decided to re-encode the videos so OpenCV doesn't use the ffmpeg. I used VirtualDub with the microsoft video 1 compression, which uses "msvidc32.dll" driver.
It works with all my videos so it is enough by now as I can keep working with OpenCV in windows.
I had similar problem. I downloaded VirtualDub, but it did not open one of my avi video because of its FMP4 encode. So in the end, the solution that solved the problem was to install ffdshow, a decoder for windows. See this link: http://www.moviecodec.com/video-codecs/fmp4-codec-with-virtualdub-45814/
Then I do not need to use VirtualDub anymore to re-encode my video anymore!
if your using x64. Please use opencv_ffmpeg245_64.dll this solves may solve the problem. Or rename opencv_ffmpeg245_64.dll to opencv_ffmpeg245.dll may solve the problem.