This seems fairly straightforward, but for the life of me, I cannot figure out why this will not work. I have similar code that writes an image everytime it reads an image, and that works fine saving the last image seen. I am seriously puzzled as to why this is saving the same image to both img0 and img1. If you guys could shed some light, that would be amazing! Thanks so much for taking the time to read this.
#include "highgui.hpp"
#include "imgproc.hpp"
#include "opencv.hpp"
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
using namespace cv;
int main(){
VideoCapture stream(0);
if(!stream.isOpened()){
cout << "No camera :(\n";
}
stream.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int img_num = 0;
int num_pics;
cout << "How many images do you want to take?\n";
cin >> num_pics;
Mat image;
while(img_num < num_pics){
cout << "Picture in...\n";
cout << "3...\n";
sleep(1);
cout << "2...\n";
sleep(1);
cout << "1...\n";
sleep(1);
stream.read(image);
imshow("pic",image);
imwrite(format("img_%d.jpg",img_num),image);
waitKey(3000);
img_num += 1;
}
return 0;
}
Edit to add simple code for saving every frame captured (into the same file, so should ultimately be the last image seen):
#include "/home/sarah/opencv-2.4.9/modules/highgui/include/opencv2/highgui/highgui.hpp"
#include "/home/sarah/opencv-2.4.9/modules/imgproc/include/opencv2/imgproc/imgproc.hpp"
#include "/home/sarah/opencv-2.4.9/include/opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
VideoCapture stream(0);
//stream.set(CV_CAP_PROP_FPS,1);
if(!stream.isOpened()){
cout << "No camera :(\n";
}
cout << "After\n";
stream.set(CV_CAP_PROP_FRAME_WIDTH, 640);
stream.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
Mat cameraFrame;
while(1){
stream.read(cameraFrame);
imshow("camera",cameraFrame);
imwrite("img.jpg",cameraFrame);
if(waitKey(30) == 13){
break;
}
}
return 0;
}
here's the culprit:
imwrite(filename,image);
atm, it will save any image to the same filename(thus overwriting any previos ones). what you want is probably more similar to:
imwrite( format("img_%d.jpg",img_num) ,image );
Related
I am reading in an image sequence from a file using an OpenCV VidoeCapture - I believe I am doing this part correctly - and then putting them in a c++ vector, for processing at a later point.
To test this, I wrote the following that would read in images, put them in a vector, and then display those images from the vector one by one. However, when I run this, no images appear.
What's wrong?
I am using a raspberry pi, I don't know if that makes any difference.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
vector<Mat> imageQueue;
int main(int argc, char** argv)
{
string arg = ("/home/pi/pictures/ceilingSequence/%02d.jpg");
VidoeCapture sequence(arg);
if(!sequence.isOpened()) {
cout << "Failed to open image sequence" << endl;
return -1;
}
Mat image;
for(;;)
{
sequence >> image;
if(image.empty()) {
cout << "End of sequence" << endl;
break;
}
imageQueue.push_back(image);
}
for(int i = 0; i < 10; i++)
{
//display 10 images
Mat readImage;
readImage = imageQueue[i];
namedWindow("Current Image", CV_WINDOW_AUTOSIZE);
imshow("Current Image", readImage);
sleep(2);
}
return 0;
}
please replace the sleep(2) with a waitKey(2000). // assuming you want to wait for 2 seconds
even if you're not interested in keypresses in general, it is needed to update the opencv / highgui graphics loop correctly.
I need some some help with the usage of opencv VideoCapture in another thread.
When I use the VideoCapture in the main thread, it is perfectly fine and it shows the video smoothly. But once I put the code in another thread and expect it do the same thing, it seems the VideoCapture does not work at all.
I made some attempts: if I initialize the VideoCapture with 0 (the default one) as parameter, it gets blocked. But if I don't initialize it
VideoCapture cap;
or use another number
VideoCapture cap(1);
it prints out error message and exits but does not get blocked.
Here is the code:
#include <iostream>
#include <thread>
#include <functional>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
class MyClass {
public:
// display the video
static void display(int i) {
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "cannot access webcame" << endl;
exit(-1);
}
Mat imgOriginal;
namedWindow("Original", WINDOW_AUTOSIZE);
while (true) {
bool success = cap.read(imgOriginal);
if (!success) {
cout << "fail to read video into mat" << endl;
break;
}
imshow("Original", imgOriginal);
if (waitKey(30) == 27) {
break;
}
}
}
};
int main()
{
//cout << "Hello World!" << endl;
thread myThread(bind(MyClass::display, 0));
myThread.join();
return 0;
}
Very appreciated if anyone can point out where I got it wrong. Thank you.
I am working on the following code
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture *camera = new VideoCapture();
camera->open(0);
if(!camera->isOpened())
{
cout << "No Camera" << endl;
return -1;
}
Mat image,blur,image2;
namedWindow("Video");
while(true)
{
*camera>>image;
*camera>>image2;
//Show default image
imshow("Video",image);
if(waitKey(30)>=0)
{
break;
}
}
return 0;
}
I got to know I can reduce the video of real time web camera output by 70% if I manage to get the average output of two consecutive frames. I got the two consecutive frames using
*camera>>image;
*camera>>image2;
Now, how can I get the average of this and display it?
For cv::Mat, you can do it like this:
Mat img_mean=0.5*image+0.5*image2;
imshow("Average",img_mean);
I wrote some simple programs using imshow function. It worked fine few times. I've tried to view two pictures, before and after processing. For the first time it worked fine, but the second time it crashed my application.
Now imshow crash application every time.
How to fix that?
#include <iostream>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat image, gray_image;
string file_path;
cout << "Input file path: ";
cin >> file_path;
image = imread(file_path, CV_LOAD_IMAGE_UNCHANGED);
if (image.data==NULL)
{
cout << "No image found!";
return 1;
}
cvtColor(image, gray_image, CV_BGR2GRAY);
namedWindow("Orig", CV_WINDOW_AUTOSIZE);
namedWindow("Gray", CV_WINDOW_AUTOSIZE);
imshow("Orig", image);
imshow("Gray", gray_image);
cout << "Output file path: ";
cin >> file_path;
imwrite(file_path, gray_image);
return 0;
}
You need waitKey
See what does waitKey (30) mean in OpenCV?
I'm trying to display a video file at 25fps smoothly without any lag. The code below does this, but only achieves about 10fps, taking about 0.1ms to execute. With cvWaitKey(1) I get around 0.03 to 0.04ms, which would be perfect, but the named window just stays grey and doesn't show the video!
Is this because cvShowImage() is too slow? Is there any other way to speed up the code and output the video smoothly?
See my code below.
Thanks a lot in advance,
Adrian
#include <cv.h>
#include <iostream>
#include <highgui.h>
#include <cxcore.h>
#include <cvaux.h>
#include <sstream>
#include <time.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
CvCapture* vid = 0;
IplImage* input; //Input image
int fps;
int i=0;
clock_t start, end;
/*Creates the GUI to output the processed images*/
cvNamedWindow("Video input", 0);
/*Open input video*/
if (argc!=2)
{
cout << "Please specify an input video." << endl;
return -1;
}
else
{
vid=cvCreateFileCapture(argv[1]);
if (!vid)
{
cout << "Could not extract frame." << endl;
return -1;
}
}
input = cvQueryFrame(vid);
fps = (int)cvGetCaptureProperty(vid, CV_CAP_PROP_FPS);
cout << fps << endl;
cout << "Video found." << endl;
/*Extraction loop */
while (input)
{
start = clock();
cout << flush;
cout << i << "\r";
i++;
/*Show image*/
cvShowImage("Video input", input);
cvWaitKey(2); //Wait is needed or else we see a grey box
input = cvQueryFrame(vid); //Read next frame from video
end = clock();
cout << (double)(end-start)/CLOCKS_PER_SEC << " s" << endl;
};
/*Release the allocated memory for the frames */
cvReleaseImage(&input);
cvDestroyWindow("Video input");
return 1;
}
Have you tried this without all the cout stuff?
The debug build of the Microsoft STL has cout handling which is almost unbelievably slow.
Try calling cvWaitKey with 1000 / fps wanted, in your case :
cvWaitKey(1000/25)
You could try something like:
char key = cvWaitKey(10); //waits 10 milliseconds
if (key == 27) //and if ESC is pressed, get out of the loop
break;