OpenCV VideoWriter does not open file - c++

The following code fails to open a VideoWriter object:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
VideoWriter oVideo;
oVideo.open ("381.avi", -1, 30, Size(640,480), true);
if (!oVideo.isOpened()) {
cout << "Could not open the output video for write" << endl;
return -1;
}
return 0;
}
I'm running OpenCV 2.4.9 pre-built with Code::Blocks on Ubuntu 12.04. I've written a number of images using imwrite() on the same location without issue, so I doubt it has to do with permissions. Also I tried CV_FOURCC('X','V','I','D') which did not work.
What am I missing here?
Any help is greatly appreciated.

I reinstalled OpenCV using this amazing script: https://help.ubuntu.com/community/OpenCV
Solved.

Make sure specific codec is installed in your machine.

Related

Done building project "Project.vcxproj" -- FAILED

I wrote some lines of code to simply read and write an image and it was working a few days ago, but now Visual Studio gives these errors: "1>Done building project "Lab1_PI.vcxproj" -- FAILED." and it says it was "unable to start the program" and that the system cannot find the file specified.
I also tried to add add _CRT_SECURE_NO_DEPRECATE and _CRT_SECURE_NO_WARNINGS via right-click project->C/C+±>Preprocessor->Preprocessor Definitions, but nothing changed.
Also, here's my code, but it is correct since it was working a few days ago:
#include <iostream>
#include <string>
#include "opencv2\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace cv;
using namespace std;
int main(int arc, const char** argv) {
string imgPath = argv[1];
string img2Path = argv[2];
cv::Mat img = imread(imgPath);
if (!img.data) {
cout << "error:no image" << endl;
return -1;
}
imshow("flower image (color)", img);
Mat img2;
cvtColor(img, img2, COLOR_RGB2GRAY);
imwrite(img2Path, img2);
imshow("flower image (grey)", img2);
waitKey(0);
}
I would be really grateful if someone could help me fix this problem because it stops me from using Visual Studio and I really need it for a project!

OpenCV 3 : Program not running in Eclipse

I am trying to run a simple image show program in Eclipse CDT in MinGW built using cmake.
OpenCV Include Path : "E:\cv\opencv\eclipse\install\include"
OpenCV Library Path : "E:\cv\opencv\eclipse\lib" (has all libraries eg.libopencv_highgui310)
My Code is,
#include <iostream>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Mat img(500, 500, CV_8UC3, Scalar(100, 0, 0));
cout << "LOL!!!" << endl;
if (img.empty()) {
cout << "Error: Image cannot be loaded." << endl;
system("pause");
return -1;
}
namedWindow("Image Window", CV_WINDOW_AUTOSIZE);
imshow("Image Window", img);
if (waitKey(10) == 27) {
return -1;
}
destroyWindow("Image Window");
return 1;
}
When I build the code my console shows,
07:19:50 **** Incremental Build of configuration Release for project opencv_cpp ****
Info: Internal Builder is used for build
g++ "-IE:\\cv\\opencv\\eclipseBuild\\install\\include" -O3 -Wall -c -fmessage-length=0 -o "src\\faceDetect.o" "..\\src\\faceDetect.cpp"
g++ "-LE:\\cv\\opencv\\eclipseBuild\\lib" -o opencv_cpp.exe "src\\faceDetect.o" -llibopencv_highgui310 -llibopencv_core310 -llibopencv_imgproc310 -llibopencv_imgcodecs310 -llibopencv_objdetect310
07:19:56 Build Finished (took 5s.647ms)
When I run the program it just terminates and nothing happens. Even the print statement is not executed.
Here is the youtube link for the video of the problem,
https://youtu.be/kCrz_WPi_AI
Can someone help me with this?
Even I too faced this problem for longtime.
I researched through all the forum's , websites and all over google but i'm not able to find the answer.
But suddenly I thought of switching off the Windows Firewall and Windows Defender, And it magically works for me.. You too try it and let me know the result !
Happy coding :)

opencv and visual studio 2010 cannot load image from a subdirectory

im new to open cv. im using Open CV 2.4.9 with visual Studio 2010. Im using a basic program to open an image. the problem is that when i put the image file on the main drive (eg: D:\image.png), it works correctly.
but when i move the image to a subdirectory (eg:D:\OpenCV\opencv\build\doc\opencv-logo.png), the output says "image cannot be loaded"
where am i going wrong. please help me out
here is the code that im using:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat im = imread("D:\OpenCV\opencv\build\doc\opencv-logo.png");
if (im.empty())
{
cout << "Cannot load image!" << endl;
return -1;
}
imshow("Image", im);
waitKey(0);
}
You need to understand about escape characters
try this
Mat im = imread("D:\\OpenCV\\opencv\\build\\doc\\opencv-logo.png");
You will have to escape the \ character. You have to do this :
Mat im = imread("D:\\OpenCV\\opencv\\build\\doc\\opencv-logo.png")

warning: Could not find codec parameters (../../modules/highgui/src/cap_ffmpeg_impl.hpp:540)

I am trying to display the video feed from IP camera getting the following error
warning: Could not find codec parameters
(../../modules/highgui/src/cap_ffmpeg_impl.hpp:540)
Here's the code for the same.
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture vcap;
Mat image;
// This works on a D-Link CDS-932L
const string videoStreamAddress = "http://admin:admin123#172.41.20.55:80/? action=stream?dummy=param.mjpg";//From mjpeg streamer
//const string videoStreamAddress = "http://192.168.1.13:8080/videofeed? dummy=param.mjpg"; // Streaming from android using ip-cam
//open the video stream and make sure it's opened
if(!vcap.open(videoStreamAddress)) {
cout << "Error opening video stream or file" << std::endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
cout << "No frame" << std::endl;
waitKey();
}
cv::imshow("Output Window", image);
if(cv::waitKey(1) >= 0) break;
}
}
First i got different error so I installed K-Lite codec. Now I am getting this error.
Can some one please tell me what is the error related to.
I have gone through many post from stackoverflow and opencv also but could manage to get a satisfactory answer.
Please help me.
Thanks in advance.
I was able to Solve the problem with the following code.
#include <stdio.h>
#include <opencv2/opencv.hpp>
int main(){
CvCapture *camera=cvCaptureFromFile("http://username:password#ipOfCamera/axis-cgi/mjpg/video.cgi?resolution=640x480&req_fps=30&.mjpg");
if (camera==NULL)
printf("camera is null\n");
else
printf("camera is not null");
cvNamedWindow("img");
while (cvWaitKey(10)!=atoi("q")){
double t1=(double)cvGetTickCount();
IplImage *img=cvQueryFrame(camera);
/*if(img){
cvSaveImage("C:/opencv.jpg",img);
}*/
double t2=(double)cvGetTickCount();
printf("time: %gms fps: %.2g\n",(t2-t1)/(cvGetTickFrequency()*1000.), 1000./((t2-t1)/(cvGetTickFrequency()*1000.)));
cvShowImage("img",img);
}
cvReleaseCapture(&camera);
}
Would be good if it helps someone like me.
Also Thanks #karlphillip for giving your time.
Warnings are not errors! Relax.
In this case FFmpeg is complaining and not OpenCV. The reason is probably because the mjpg format that is specified on the URL doesn't really require an actual codec.

How to set 'nmixures' and 'bShadowDetection' values in 'BackgroundSubtractorMOG2'?

Please have a look at the following code
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace cv;
using namespace std;
Mat frame,back,fore;
int main()
{
VideoCapture cam;
BackgroundSubtractorMOG2 bgs(0,0,false);
vector<vector<Point>>contours;
bgs.setInt("nmixtures",3);
cam.open(0);
if(!cam.isOpened())
{
cout << "Cam not Found";
return -1;
}
namedWindow("Frame");
while(true)
{
cam>>frame;
imshow("Frame",frame);
if(waitKey(30)>=0)
{
break;
}
}
}
I am trying to set the value of nmixures of BackgroundSubtractorMOG2 into 3 and bShadowDetection of BackgroundSubtractorMOG2 into false.
However, as with OpenCV 2.4.5, these values are set to private, so I can't acess them directly. I managed to set the value of bShadowDetection via the constructor (Eventhough I dnt know what other 2 params are), and I couldn't find a way to set the nmixers. I don't know whether The way I set the nmixures is correct or not,because in the article I read, the writer says "Set them via constructors in case of opencv 2.4"
Can you please tell me how to set those 2 values?
In OpenCV 2.4.8.:
bgs.set("nmixtures", 3);
bgs.set("detectShadows", false);
In your case, you have to write:
BackgroundSubtractorMOG2 bgs;
bgs.setInt("nmixtures", 3);
bgs.setBool("detectShadows", false);