OpenCV Video Editing? - c++

I am using OpenCV and trying to apply a Gaussian Blur to an incoming video stream. I basically use cvQueryFrame to remove a frame, blur it and display the frame onto the screen. The thing is though, my video gets stuck on the first frame after I apply the blur....anyone know why? its basically showing one frame instead of a video. The second I remove the blur it starts outputting video again.
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include <iostream>
using namespace std;
int main()
{
//declare initial data
IplImage *grabCapture= 0; //used for inital video frame capture
IplImage *process =0; //used for processing
IplImage *output=0; //displays final output
CvCapture* vidStream= cvCaptureFromCAM(0);
cvNamedWindow ("Output", CV_WINDOW_AUTOSIZE);
int createimage=1;
while (1)
{
grabCapture= cvQueryFrame (vidStream);
if (createimage==1)
{
process= cvCreateImage (cvGetSize(grabCapture), IPL_DEPTH_16U, 3);
createimage=0;
}
*process=*grabCapture;
cvSmooth (process,process,CV_GAUSSIAN,7,7); //line that makes it display frame instead of video
cvShowImage("Output",process);
}
//clean up data
cvReleaseImage (&grabCapture);
cvReleaseImage (&process);
cvReleaseImage (&output);
cvReleaseCapture (&vidStream);
return 0;
}

You are missing a call to cvWaitKey. This is the only way to tell OpenCV to process events and thus prevent the GUI from freezing.
Try adding this line:
cvWaitKey(10);
after cvShowImage("Output",process);.
Edit: here is the documentation for cvWaitKey

Related

How can I get the most current frame in OpenCV?

I'm trying to take and display the most current frame on any key event. It is important for me that the photo will be taken only once (without using infinity loop). I have a problem because displayed photo is the previous one not the most current. It looks like some buffor with captured frames in VideoCapture object.
I'm using OpenCV 4.6.0 and laptop camera.
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
cv::VideoCapture cap0(0);
cv::Mat frame0;
for (;;)
{
cap0.read(frame0);
if (!frame0.empty())
{
imshow("cap0", frame0);
}
cv::waitKey(0);
}
return 0;
}
Is it possible to get and display current photo without doing infinity loop whenever I click any key?

problem on displaying live video in qt c++ application

I want to show a live stream of the camera connected to raspberry in qt application (OS Linux). After googling it, I found out I must display the video inside QLabel. When displaying an image there's no problem and everything works fine, but when I want to display the live stream inside QLabel, the live stream window opens separately (not inside QLabel). would you tell me how to solve this problem? here's my code :
void Dialog::on_Preview_clicked()
{
command = "raspistill";
args<<"-o"<<"/home/pi/Pictures/Preview/"+Date1.currentDateTime().toString()+".jpg"<<"-t"<<QString::number(20000);
Pic.start(command,args,QIODevice::ReadOnly);
QPixmap pix("//home//pi//Pictures//Preview//test.jpg");
ui->label_2->setPixmap(pix);
ui->label_2->setScaledContents(true);
}
This code opens video capturing screen and captures an image after 20 seconds. the only problem is that the capture screen (which could be used as a live stream). isn't being displayed inside the "Lable_2". Is there anyway to do this without using OpenCV library? If not, tell me how to do it using OpenCV.
Thanks
It is pretty simple in opencv
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("edges", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
Stream the camera using OpenCV, and show it in QLabel is possible.
When QCamera not working, and also use OpenCV in the project, could use VideoCapture to stream the video instead of QCamera.
The problem can be decomposed into several steps. Basically, We need:
Create a QThread for streaming(Don't let the GUI thread blocked).
In the sub-thread, using cv::VideoCapture to capture the frame into a cv::Mat.
Convert the cv::Mat to QImage(how to convert an opencv cv::Mat to qimage).
Pass QImage frame from sub-thread to the main GUI thread.
Paint the QImage on the QLabel.
I put the complete demo code in Github. it could paint the frame on the QLabel and QML VideoOutput.

OpenCV C++ Not able to save captured image

First of all, sorry for my bad English.
I am new to OpenCV and I wish to save image captured from my webcam.
I got the code from this link http://www.samundra.com.np/opencv-code-to-save-image/403
#include <iostream>
#include <opencv/cv.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc,char** argv)
{
char path[255];
// For Linux, path to save the captured image
//strcpy(path,"/home/samundra/sample.jpg");
// For Windows, path to save the captured image
strcpy(path,"c:\\home.jpg");
// Pointer to current frame
IplImage *frame;
//Resize image to small size
IplImage *small;
// Create capture device ready
// here 0 indicates that we want to use camera at 0th index
CvCapture *capture = cvCaptureFromCAM(0);
// highgui.h is required
cvNamedWindow("capture",CV_WINDOW_AUTOSIZE);
frame = cvQueryFrame(capture);
// Must be initialized before actually cvResize is used
// we are creating image of size that is half of the original frame captured
// 8 = Bits 3 = Channel
small = cvCreateImage(cvSize(frame->width/2,frame->height/2), 8, 3);
while(1)
{
// Query for Frame From Camera
frame = cvQueryFrame(capture);
// Resize the grabbed frame
cvResize(frame, small); //cvResize(source, destination)
// Display the captured image
cvShowImage("capture", small);
char ch = cvWaitKey(25); // Wait for 25 ms for user to hit any key
if(ch==27) break; // If Escape Key was hit just exit the loop
// Save image if s was keyboard
if(ch=='s')
{
cvSaveImage(path,small);
}
}
// Release All Images and Windows
cvReleaseImage(&frame);
cvReleaseImage(&small);
cvDestroyWindow("capture");
return 0;
}
I am using OpenCV 2.3.1 and Eclipse and after I compiled this code, the webcam is successfully opened but my image is not saved. Thanks for the help.

OpenCv error when initializing camera with VisualStudio C++ 2010

I'm new to opencv programming, so maybe my question will be very stupid. But i have such problem, i took one sample code, which should enable laptop webcamera to show image in desktop.
#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main()
{
Mat image; //create Matrix to store image
VideoCapture cap;
cap.open(0); // initialize capture
namedWindow("window", CV_WINDOW_AUTOSIZE); // create window to show image
while(1)
{
cap>>image; // copy webcam stream to image
imshow("window", image); // print image to screen
waitKey(33); // delay 33ms
}
return 0;
}
But when i'm trying to debug it i get an error message.
Unhandled exception at 0x5a16ebe6 in myNewOpenCV.exe: 0xC0000005: Access violation reading location 0x00000018.
But if i put breakpints on
cap>>image;
imshow("window", image); // print image to screen`
and after debuging im taking it off everything work correctly. Maybe someone can help to find a problem. Thanks.

C/C++ OpenCV video processing

Good day everyone! So currently I'm working on a project with video processing, so I decided to give a try to OpenCV. As I'm new to it, I decided to find few sample codes and test them out. First one, is C OpenCV and looks like this:
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
int main( void ) {
CvCapture* capture = 0;
IplImage *frame = 0;
if (!(capture = cvCaptureFromCAM(0)))
printf("Cannot initialize camera\n");
cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
while (1) {
frame = cvQueryFrame(capture);
if (!frame)
break;
IplImage *temp = cvCreateImage(cvSize(frame->width/2, frame->height/2), frame->depth, frame->nChannels); // A new Image half size
cvResize(frame, temp, CV_INTER_CUBIC); // Resize
cvSaveImage("test.jpg", temp, 0); // Save this image
cvShowImage("Capture", frame); // Display the frame
cvReleaseImage(&temp);
if (cvWaitKey(5000) == 27) // Escape key and wait, 5 sec per capture
break;
}
cvReleaseImage(&frame);
cvReleaseCapture(&capture);
return 0;
}
So, this one works perfectly well and stores image to hard drive nicely. But problems begin with next sample, which uses C++ OpenCV:
#include "opencv2/opencv.hpp"
#include <string>
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
//namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_RGB2XYZ);
imshow("edges", edges);
//imshow("edges2", frame);
//imwrite("test1.jpg", frame);
if(waitKey(1000) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
So, yeah, generally, in terms of showing video (image frames) there is practically no changes, but when it comes to using im** functions, some problems arise.
Using cvSaveImage() works out nicely, but the moment I try to use imwrite(), unhandled exception arises in regards of 'access violation reading location'. Same goes for imread(), when I'm trying to load image.
So, the thing I wanted to ask, is it possible to use most of the functionality with C OpenCV? Or is it necessary to use C++ OpenCV. If yes, is there any solution for the problem I described earlier.
Also as stated here, images initially are in BGR-format, so conversion needed. But doing BGR2XYZ conversion seems to invert colors, while RGB2XYZ preserve them. Examples:
images
Or is it necessary to use C++ OpenCV?
No, there is no necessity whatsoever. You can use any interface you like and you think you are good with it (OpenCV offers C, C++, Python interfaces).
For your problem about imwrite() and imread() :
For color images the order channel is normally Blue, Green, Red , this
is what imshow() , imread() and imwrite() expect
Quoted from there