How to read a video under OpenCV 3.0.0 - c++

I am using win 8.1 and visual studio 2013 community. I download openCV3.0.0 from here. What I want to do is read a video. Following is my code
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cv::VideoCapture cap("bb.mp4");
if (!cap.isOpened()){
std::cout << "Cannot open video!\n";
return -1;
}
cv::Mat frame;
while (cap.read(frame)){
cv::imshow("frame", frame);
char key = cv::waitKey(1) & 0xFF;
}
return 0;
}
The problem is that cap.isOpened() always return false even I give an absolute path cv::VideoCapture cap("D:\\bb.mp4"). Following is another toy program that operates normally.
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat img = cv::imread("aa.jpg");
cv::imshow("img", img);
char key = cv::waitKey(1) & 0xFF;
return 0;
}
The second program shows that I can compile a program included openCV library correctly and also the path to the file should be correct since the aa.jpg and bb.mp4 are placed in the same folder. Anyone knows how to solve the problem?

Related

Trying to Locate Aruco Fiducials with USB camera Ubuntu 18.04

I have some code I think should be working using open CV to detect a set of fiducials. For some reason, I cant get my code to run. It gives the error "Unable to stop the stream: Invalid argument"
#include "opencv2/opencv.hpp"
using namespace cv;
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/aruco.hpp>
int main(int argc, char** argv)
{
Mat markerImage;
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.open(1))
return 0;
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
std::vector<int> markerIds;
std::vector<std::vector<cv::Point2f>> markerCorners, rejectedCandidates;
cv::Ptr<cv::aruco::DetectorParameters> parameters = cv::aruco::DetectorParameters::create();
cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_50);
cv::aruco::detectMarkers(frame, dictionary, markerCorners, markerIds, parameters, rejectedCandidates);
cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
imshow("Camera)", frame);
if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}

I get the Assertion failure "file_name != nullptr" but only in release mode

When I try to run the code in debug mode everything works fine, but when I try to run it in release mode I get the error "file_name != nullptr"(the error comes from fopen). I know what the error means, but I don't know why I get the error and how to fix it
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
String imageName("C:/Users/x/Desktop/pic.png"); // by default
if (argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread(imageName, IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
imshow("Display window", image); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Error Message:
>-----------Microsoft Visual C++ Runtime Library----------------
>
>
>Debug Assertion Assertion Faild!
>
>Programm: C:\Users\x\...\myProgramm.exe
>File: minkernel\crts\ucrt\src\appcrt\stdio\fopen.cpp
>
>Line: 30
>
>Expression: file_name != nullptr
You have to make sure that you use the right dependency (.lib file) on debug and release mode.

how to use image file name itself as a tag in file storage opencv

I am trying to detect keypoints from list of images in a folder and store them in a file using opencv filestorage. How can I give image file name itself as tag for each respective image using filestorage. I am able to print filenames to console but not in file. It gives me opencv error: Bad argument in icvXMLWriteTag.
my image file names are like shot000_0001_0.jpg etc,.
Any help is deeply appreciated. Thank you
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\nonfree\features2d.hpp>
#include <tchar.h>
#include <io.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
struct _finddata_t c_file;
long hFile;
char imageDirectory[] = "C:\\Users\\RadhaRani\\Documents\\visual studio 2013\\Projects\\Example_Match_SURF\\Example_Match_SURF\\test1";
char imageFileType[] = "jpg";
char fullImagePath[500];
char buffer[500];
char imageFileName[500];
vector<KeyPoint> keypoints;
FileStorage fs("Concept_multipleImages_png.xml", FileStorage::WRITE);
sprintf(buffer, "%s\\*.%s", imageDirectory, imageFileType);
hFile = _findfirst(buffer, &c_file);
// Check to make sure that there are files in directory
if (hFile == -1L)
{
printf("No %s files in current directory!\n", imageFileType);
}
else
{
// List all files in directory
printf("Listing of files:\n");
// Loop through all images of imageFileType
do
{
// Show file name
printf("Opening File: %s \n", c_file.name);
//cout << c_file.name << endl;
sprintf(fullImagePath, "%s\\%s", imageDirectory, c_file.name);
Mat image = imread(fullImagePath);
SurfFeatureDetector surf;
surf.detect(image, keypoints);
sprintf(imageFileName,"%s", c_file.name);
fs << imageFileName << keypoints;
// keep finding files until no more found, i.e. until there is an error code
} while (_findnext(hFile, &c_file) == 0);
// Close file finder object
_findclose(hFile);
}
fs.release();
return 0;
}

Error with OpenCV Includes

See the ticked answer below :)
Error 1 error C2065: 'capture' : undeclared identifier
Using VS2013 Express with OpenCV
Older code examples have worked, but I cant get this one to:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace cv;
int main()
{
Mat frame = cvQueryFrame(capture);
imshow("Video", frame);
}
I had to change "opencv2/core/core.hpp"
To #include <opencv2\core\core.hpp>, and It got that bit.
but I've tried including highgui but I cant get "capture" to work?
Any ideas?
x64 on Debug, and using x64 libs...
that capture part is a leftover from the old c-api.
try this instead:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
while( cap.isOpened() )
{
Mat frame;
if ( ! cap.read(frame) )
break;
imshow("lalala",frame);
int k = waitKey(10);
if ( k==27 )
break;
}
return 0;
}
Of course how will it work when you haven't declared the variable capture? Probably you want to do something like this:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace cv;
int main()
{
CvCapture* capture = cvCreateFileCapture("path to video file");
Mat frame = cvQueryFrame(capture);
imshow("Video", frame);
waitKey();
cvReleaseCapture(&capture);
}

Change resolution of extracted frame in Opencv

I have an H.264 video stream and I need to extract frames from it. However when I extract the frames,the quality is really poor since I need to perform color segmentation!I want to know how can i extract the frame and convert it to B G R so as to have a better quality picture.
Here is the code I have so far:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(){
VideoCapture capture("1.dv4");
if(!capture.isOpened())
return 1;
double rate=capture.get(CV_CAP_PROP_FPS);
bool stop(false);
Mat frame;
namedWindow("Extracted Frame",CV_WINDOW_NORMAL);
cout <<"Rate is="<<rate;
int delay=1000/rate;
while(!stop){
if(!capture.read(frame))
break;
imshow("Extracted Frame",frame);
imwrite("C:/Users/DELL/Documents/Visual Studio 2010/Projects/VideoFrameCapture/VideoFrameCapture/frame.jpg",frame);
if(waitKey(delay)>=0)
stop=true;
}
capture.release();
waitKey(0);
return 1;
}
Once you have opened the VideoCapture, add this:
capture.set(CV_CAP_PROP_CONVERT_RGB, 1);