OpenCV: Reading the frames of a video sequence - c++

Anyone help me ,I am trying to run code to read frames from video in folder its success in building but when debugging there isn't any output
* I am using Visual studio 2012 ,opencv 2.4.11 version
the code is :
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
// Open the video file
cv::VideoCapture capture("C:/Users/asus/Desktop/A.mp4");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
int 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();
}

Your main() function is never executed. The only thing, that gets executed is _tmain(), which does nothing and returns immediately.
I haven't done much Windows programming in a while, but if I remember correctly this is how it works:
When Unicode is enabled for your compiler
int _tmain(int argc, _TCHAR* argv[])
gets compiled as
int wmain(int argc, wchar * argv[])
which is then used as the program entry point.
Since you seem not to be using any Windows-APIs in your code I would ignore the Microsoft specific way of doing multibyte character strings, which is non-portable, and simply use plain ASCII strings as you did in the main() function, that you intended to use.
So to solve your problem simply throw out the _tmain() function. Maybe you also need to disable Unicode in your project settings if you get linker errors.

Related

How do I make a portable program with OpenCV2 on C++?

I have installed OpenCV on Windows 10 and using C++ in Visual Studio.
Now I'm trying to make a program that makes several photos then saves them.
Problem:
I made a copy of project on USB flash (go to another PC) and when I try to start it from .exe file, I get this error:
How can start it without installing 1Gb of FULL OpenCV libraries?
I tried to start from release version.
My C++ source:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h> // For Sleep
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap(0);
// Get the frame
Mat save_img; cap >> save_img;
if (save_img.empty())
{
std::cerr << "Something is wrong with the webcam, could not get frame." << std::endl;
}
// Save the frame into a file
imwrite("test.jpg", save_img); // A JPG FILE IS BEING SAVED
return 0;
}
My settings:
P.S. I want to make light program for making photo on Windows. And how to make real portable programs.

Capture the output of a QR scanner from a raspberry in C++

I have a QR scanner attached to my raspberry pi 3 B+. I need to capture the output of the scanner from C++ on raspbian. The scanner writes the read information to the window with the focus, like a keyboard input.
I have tried to capture the keyboard input with the ncurses library and other similar choices but sometimes I get an incomplete code, because I don't know when the read info ends, and I don't know when I have a complete input.
I tried something like this:
#include <stdio.h>
#include <iostream>
#include <ncurses.h>
#include <future>
using namespace std;
int main(int argc, char *argv[])
{
initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
char buffer[1000];
while (true) {
getstr(buffer);
cout<< buffer << endl;
napms(500);
}
return 0;
}
The problem is that I may get the code in a fragmented way if it's too long or if the output slows down for whatever reason.

SDL not printing to console

I'm trying to use SDL with Visual Studio 2019 but my programs are only showing an empty console. At the moment I just want to be able to compile my program with the SDL libraries.
#include <iostream>
#include <SDL.h>
int main(int argc, char** argv)
{
std::cout << "yee haw!" << std::endl;
return 0;
}
This code is just giving me a console with the text:
(process 32) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
Where I would want 'yee haw!' preceding it.
It works fine when I take out the #include <SDL.h> (but I want the SDL.h)
I've heard that SDL now redirects to a stdout.txt file but I couldn't find that anywhere. I've also tried displaying a window with code from a tutorial I found, but that also gives me the empty console.
I'm using Visual Studio 2019 on Windows and SDL 2.0.9
Thanks!
By default, SDL uses a macro hack to replace the main function. The user defined main function must be in the following format:
int main(int argc, char** argv)
{
// whatever
return 0;
}
Alternatively, if you don't want this behaviour you can use SDL_SetMainReady.
#define SDL_MAIN_HANDLED
#include <SDL.h>
int main()
{
SDL_SetMainReady();
// whatever
return 0;
}

OpenCV memory leak using cvCreateFileCapture and cvQueryFrame

I am new to OpenCV (OpenCV 3.2 / opencv_ffmpeg320_64.dll / Windows 10 / Visual Studio 2017) and wrote a program dealing with a video stream of a webcam. Unfortunately the program has a memory leak. After hours of searching and googling I managed to break down the problem to the following minimal example:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <thread>
CvCapture *capture;
IplImage *frame;
int main(int argc, char **argv)
{
capture = cvCreateFileCapture("http://192.168.1.123:8080/CamStream");
while (true)
{
cvWaitKey(1);
frame = cvQueryFrame(capture);
if (frame)
{
std::cout << "New image" << std::endl;
}
}
return 0;
}
As you can see I am capturing of a simple HTTP stream. After the capture is created new frames are received. Unfortunately the task manager shows a never stopping increase of memory:
What could cause this problem and what are possible approaches to solve it?

OpenCV capture loops video/Does not detect last frame

I am capturing an avi file and processing it. My code has worked for sometime without problem but now it does not seem to stop after the last frame of the video is captured. Instead it keeps looping back to the beginning of the video. I do not understand why this is happening and I can not think of anything changing with regards to Eclipse or OpenCV. I have tried the same code on my Ubuntu pc with the same video and it works without problems. I have even tried as much as reinstalling the OS and apps without success.
Sample code:
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame;
VideoCapture capture;
const string inputVideo = argv[1];
char buff[PATH_MAX];
getcwd( buff, PATH_MAX );
std::string fileName( buff );
fileName.append("/");
fileName.append(inputVideo);
capture.open(inputVideo);
while(true)
{
capture >> frame;
if(!frame.empty())
{
imshow("frame", frame);
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
int key = waitKey(10);
if((char)key == 'c')
{
break;
}
}
return 0;
}
I am running this on a Mac OS X (10.8.2), Eclipse Juno, and OpenCV 2.4.3.
Any advice or comments are appreciated. Thanks in advance
The solution that I used was posted as a comment by #G B. I am creating a solution so that it may be marked as one.
I used capture.get(CV_CAP_PROP_POS_FRAMES) before and after frame grabbing, if the value "after" is less than the value "before", then I've reached the end of the video.
Get the frame count like below,
int frameCnt = capture.get(CV_CAP_PROP_FRAME_COUNT);
And check to exit the loop when the frame count exceeds..