OpenCV C API not allowing camera release and reopening - c++

I just installed Ubuntu 14.04 and openCV 2.4.9 in a new computer.
I had a working code that, at some point, closes the last cvVideoCapture and opens another, that may or may not have a different index:
CvCapture* capture;
capture = cvCaptureFromCAM(1);
...
cvReleaseCapture(&capture);
capture = cvCaptureFromCAM(0);
This code used to work flawlessly with Ubuntu 12.0.4 and OpenCV 2.4.? (I'm not sure which version but it was an older one from one year ago). Now it outputs the error
HIGHGUI ERROR: V4L: device /dev/video0: Unable to open for READ ONLY
On the other hand, the equivalent with the C++ API works:
VideoCapture cap;
cap.open(1);
...
cap.release();
cap.open(0);

You can use different names for each camera device open.Like below code.Try this.
VideoCapture cap,cap1;
cap.open(1);
...
cap.release();
cap1.open(0);

After some experiments I'm sure, that cvRelease is definitely not for freeing a CvCapture instance. I'm getting a compiler warning about different pointer types. Maybe you should compile with some additional flags. You definitely have to use cvReleaseCapture(&capture), like stated in the docs. I'm not sure if that's the solution for your problem, maybe you need to give some more information.
CvCapture* capture;
capture = cvCaptureFromCAM(1);
cvReleaseCapture(&capture);
capture = cvCaptureFromCAM(0);

Related

Capturing from webcam under WinPE?

I am writing a suite of hardware tests that will run in a windows PE environment. I need to test the webcam but so far I am stumped. I compiled a small webcam capture program using the opencv library, but it is unable to detect the webcam. I have tried loading additional drivers, but it hasn't worked. Unfortunately I have not found any help online since apparently no one else is crazy/stupid enough to attempt this. Is this idea fundamentally flawed in some way, or is there a way to do this? Any help is appreciated.
Here is the code I am using that compiles and runs on my windows 10 machine:
#include "opencv2/opencv.hpp"
#include <cstdio>
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.isOpened()) // check if we succeeded
{
printf("Unable to open webcam");
return -1;
}
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
imshow("this is you, smile! :)", frame);
if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
Edit: I realized that the drivers were not loading on startup so I loaded them manually with drvload.exe. Now the webcam shows as functional when I do a WQL query(ConfigManagerErrorCode = 0) but my opencv program will still not detect it.
You won't get your cam to work, without adding some DLLs to your PE environment.
PE is a minimalistic WIndows that misses lots of functions due to minimize memory and diskspace consumption. It is only designed to offline edit Windows images, not to do hardware testing. Ran into similar issues some months ago.
I'm not sure if linking to this project is allowed here, but they sure have helpful information for you on that: TheOven

How to convert videostream data to cv::gpumat?

I need to convert videostream data to cv::gpumat. Initially I tried copying to cv::Mat and then use upload to load it to gpumat. This process is very slow(20ms for a 640*480 frame).
I need a method to convert from openni videostream to gpumat directly. I tried the following code but it gives run time error
I am using opencv3.1, cuda-8.0, gtx titanx on ubuntu 16.04
#include "opencv2/opencv_modules.hpp"
#include <opencv2/core.hpp>
#include <opencv2/cudacodec.hpp>
#include <opencv2/highgui.hpp>
int main(int argc, const char* argv[])
{
const std::string fname = argv[1];
cv::cuda::GpuMat d_frame;
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
for (;;)
{
if (!d_reader->nextFrame(d_frame))
break;
cv::Mat frame;
d_frame.download(frame);
cv::imshow("GPU", frame);
if (cv::waitKey(3) > 0)
break;
}
return 0;
}
OpenCV Error: The function/feature is not implemented (The called functionality is disabled for current build or platform) in throw_no_cuda, file /home/krr/softwares/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp, line 101
terminate called after throwing an instance of 'cv::Exception'
what(): /home/krr/softwares/opencv-3.1.0/modules/core/include/opencv2/core/private.cuda.hpp:101: error: (-213) The called functionality is disabled for current build or platform in function throw_no_cuda
Take a look into the source code. The framework called "throw_no_cuda()" (lines are different, version?). Also the error seems to be a duplicate of this one on github.
alalek:
https://developer.nvidia.com/nvidia-video-codec-sdk:
Note: For Video Codec SDK 7.0 and later, NVCUVID has been renamed to NVDECODE API.
OpenCV has no support for new API and there are no plans to add this.
The latest CUDA version with NVCUVID is ~ CUDA 6.5.
Consider using ffmpeg with enabled CUDA features (via normal cv::VideoCapture - but it can't work with CUDA's cv ::GpuMat).
And further:
dapicard:
I found a way to define the codec used by the FFMpeg backend :
export OPENCV_FFMPEG_CAPTURE_OPTIONS="video_codec|h264_cuvid"
More generally, it is possible to define theses parameters, using the syntax parameter_name|value;parameter_name2|value2
That is, to use the hardware capabilities to decode videos (which you tried). Sidenote: ffmpeg also offers options to transcode videos directly on the gpu (i.e. without moving fromes away from gpu memory).
Frankly, using the proposed method will not result in the matrix being delivered to your gpu memory directly, but only solve the error. I don't think it is possible to grab the memory directly from ffmpeg directly, so you are stuck with moving it.

Unable to get Video feed from D-Link DCS 932L using openCv

I am trying to display video feed from IP- Camera(D-Link DCS 932L). I Have gone through topics for the same and tried the code from different posts, but am unable to get the video feed from the camera.
Here's the code which i tried.
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv;
int main(int argc, char *argv[])
{
Mat frame;
namedWindow("video", 1);
String url = "http://admin:admin#172.32.20.55:80/image/jpeg.cgi";
VideoCapture cap(url);
/* VideoCapture cap(0);*/
while ( cap.isOpened() )
{
cap >> frame;
if(frame.empty()) break;
imshow("video", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
I tried many different kind of url's but i was unable to display any video feed. I thought it might be code problem so I even tried displaying the USB Webcam and it worked. So now i come to conclusion that the problem seems to be with URL which am passing. Heres the list of urls which I tried. I got this Url options from iSpy.Here are those URL's
(JPEG)http://admin:admin#172.32.20.55:80?IMAGE.JPG
(JPEG)http://admin:admin#172.32.20.55:80/image/jpeg.cgi
(MPEG)http://admin:admin172.32.20.55:80/video.cgi?resolution=VGA
(MPEG)http://admin:admin172.32.20.55:80/video/mjpg.cgi
(MPEG)http://admin:admin172.32.20.55:80/mjpeg.cgi? user=admin&password=admin&channel=0
(MPEG)http://admin:pnqadmin172.32.20.55:80/VIDEO.CGI
Please let me know what can be probable problem for displaying the video feed.
Is their something to do with the setting of the OpenCv or something else.Please note that am using VS2010 and C++ Need help of all the Expert out their.
Thanks in advance.
I solved my problem. The problem was with URL. I changed the URL and it worked smooth..!
The URL i used was as follows.
"http://USER:PWD#IPADDRESS:8088/mjpeg.cgi?user=USERNAME&password=PWD&channel=0&.mjpg";
I keep getting the same error:
warning: Error opening file <../../modules/highgui/src/cap_ffmpeg_impl.hpp:529>
I was trying to stream MJPG video from a Foscam IP camera. The URL opened just fine but I couldn't read any frames. May be there was some problem with the video codec.
Here's a hack written in Python that worked for me: https://stackoverflow.com/a/18411168/3183051
Perhaps my answer is too late. Check if opencv_ffmpegXXX.dll or opencv_ffmpegXXX_64.dll (if you are building 64bit executable) is in the same folder where your executable is. Replace XXX with the number of opencv version you use.

MJPEG stream fails to open in OpenCV 2.4

I am having an issue getting OpenCV 2.4 to capture from an MJPEG stream from a Raspberry Pi, I have checked the stream URL in a browser and it seems to be working fine, however when I try to open it in OpenCV it seems to hang when I attempt to open it (I get neither the error or success messages on the terminal).
cv::VideoCapture vcap;
cv::Mat raw_image;
const string videoStreamAddress = "http://192.168.0.28:8080/?action=stream";
if(!vcap.open(videoStreamAddress))
{
cout<<"Error opening video stream"<<endl;
return -1;
}
cout<<"Stream opened"<<endl;
I am using MJPEG-Streamer to provide the stream.
The same code works fine when capturing form a RTSP video stream.
EDIT: I tried changing the JPEG quality and resolution of the images captured by raspistill on the Pi and I now get the message saying the stream failed to open.
Try adding a dummy param, that hints at the mjpeg content:
const string videoStreamAddress = "http://192.168.0.28:8080/?action=stream&type=mjpg";
I found the answer, it is similar to what Hitesh suggested, I just needed to have a . before mjpg to make OpenCV think it was a URI to a file.
I found this in this answer to a similar question.

OpenCV displaying avi file encoded in I420

I am trying to write a video processing application using OpenCV 2.4.2 (in Visual C++ 2010 Express on Windows 7) but I am having trouble displaying certain AVI files. Most display correctly, but when I use an AVI file encoded in I420 format all I get is a striped pink image for every frame (it displays correctly in regular media players).
Output displayed: http://i.imgur.com/BOu6c.png?1
Currently, I am using the C++ API, but the same thing happens when I use the C API (code from this page: http://nashruddin.com/how_to_play_avi_files_with_opencv). I find this strange, because in most answers on this site and resources on the web, they explicitly recommend to use the I420 encoding. Does anyone know what could be causing this or how to fix it?
Here is a trimmed down version of the code I am using:
int main(int argc, char** argv){
string fname = "test.avi";
VideoCapture capture(fname);
if(!capture.isOpened()){
cerr << "error opening " << fname << endl;
return -1;
}
Mat frame;
namedWindow("output");
double rate = capture.get(CV_CAP_PROP_FPS);
int delay = 1000/rate;
while(true){
if(!capture.read(frame)) break;
cv::imshow("output", frame);
if(waitKey(delay) >= 0) break;
}
capture.release();
return 0;
}
I am using is the pre-compiled version of OpenCV if that makes a difference (http://sourceforge.net/projects/opencvlibrary/).
Ok, so I managed to test on a few more computers. One just crashed and, on another, the video played fine. It turns out that it was a problem with FFMPEG being enabled in the default OpenCV compilation having problems with the uncompressed AVI. Recompile OpenCV with FFMPEG disabled or just use a different codec to compress the video.