VideoCapture opencv + rstp + Qt c++ fails - c++

I am trying to capture some video from my IP-camera, but only got theese artefacts:
How do I fix it? Help me, please! Here is my code:
QString str1 = "rtsp://admin:000000#192.168.1.100:554/H264?ch=1&subtype=0";
cv::VideoCapture vcap = VideoCapture(str1.toStdString());
cv::Mat image;
if(!vcap.isOpened())
{
qDebug() << "Input error\n";
return;
}
namedWindow("window",CV_WINDOW_AUTOSIZE);
for(;;) {
Mat frame;
if(vcap.grab())
vcap.retrieve(image);
if (image.empty())
break;
imshow("window", image);
waitKey(1);
}

Related

VideoCapture will not open the video

I am trying to open a video and write it to a location:
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
string videoName = "KorExp3.avi";
VideoCapture video(videoName);
Mat frame;
video >> frame;
VideoWriter w("D:/w.avi", CV_FOURCC('M', 'P', '4', '2'), 30, frame.size(), true);
while (true) {
video >> frame;
imshow("frame", frame);
w << frame;
}
w.release();
waitKey(0);
return 0;
}
In debug mode, while hovering the mouse on video it says:
Information not available, no symbols loaded for opencv_world340d.dll
I have copied this dll file and the video file to the same location of .exe, but still same thing happens. I also tried the absolute path to the video string videoName = "D:\\KorExp3.avi"; but didn't work.
How can I capture a video and write it to a location using openCV?
Do you generate (compile) OpenCV with debug symbols??
This is a sample (and simple) code to record a video file using OpenCV...
I am using Qt (5.5.1 and upper) on Linux, but this is doesn't matter...
It will work in every OS...
void MainWindow::on_Rec_Click()
{
QString szNome = QString("%1/%2-%3-M.mp4").arg(szPath).arg(szCamIndex).arg(obAgora.toString("yyyyMMddHHmmss"));
qDebug() << szNome;
char szCPath[2048];
strcpy(szCPath, szNome.toStdString().c_str());
qDebug() << "Path: " << szCPath;
MakePath(szCPath, inIndice+1); // If the path does not exist...
SaveEventToDB(szNome, inIndice+1, obAgora, 0); // Register event in DB
qDebug() << m_Capture[inIndice - 1] << " / " << inIndice;
cv::Size S = cv::Size((int) m_Capture[inIndice]->get(CV_CAP_PROP_FRAME_WIDTH), // Acquire input size
(int) m_Capture[inIndice]->get(CV_CAP_PROP_FRAME_HEIGHT));
qDebug() << S.width << " / " << S.height;
int ex = cv::VideoWriter::fourcc('M','P','4','2');
qDebug() << ex;
double dlFrameRate = m_Capture[inIndice]->get(CV_CAP_PROP_FPS);
qDebug() << dlFrameRate;
m_Output = new cv::VideoWriter(szNome.toStdString(), ex, dlFrameRate, S, true);
qDebug() << "Object cv::VideoWriter created.";
m_OutputFile = szNome;
m_inTimerID = startTimer(1000 / dlFrameRate);
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if(m_inActualView != 0) {
cv::Mat image;
*m_Capture[m_inActualView] >> image;
if(m_Output) {
if(m_Output->isOpened()) {
*m_Output << image;
}
}
cv::flip( image,image, 0);
// Show the image
m_Ui->openCVViewer->showImage( image );
}
}
I put the directory of the video instead of moving the video to the project location andthe error was solved:
string videoName = "D:\\KorExp3.avi";
while (true) {
video >> frame;
...

Videowriter function for greyscale image using OpenCV function

I have a GigaE camera and which gives me a greyscale image and I want to record it as a video and process it later.
So as initial step I tried recording video using my webcam it worked and if i convert it into greyscale before writing it into video. I am not getting any video.
My code is below
int main(int argc, char* argv[])
{
VideoCapture cap(0);
VideoWriter writer;
if (!cap.isOpened())
{
cout << "not opened" << endl;
return -1;
}
char* windowName = "Webcam Feed";
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
string filename = "D:\myVideo_greyscale.avi";
int fcc = CV_FOURCC('8', 'B', 'P', 'S');
int fps = 30;
Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH),cap.get(CV_CAP_PROP_FRAME_HEIGHT));
writer = VideoWriter(filename,-1,fps,frameSize);
if(!writer.isOpened())
{
cout<<"Error not opened"<<endl;
getchar();
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "ERROR READING FRAME FROM CAMERA FEED" << endl;
break;
}
cvtColor(frame, frame, CV_BGR2GRAY);
writer.write(frame);
imshow(windowName, frame);
}
return 0;
}`
I used fcc has -1 tried all the possibilities none of them are able to record video.
I also tried creating a grayscale video using opencv for fcc has CV_FOURCC('8','B','P','S') but it did not help me.
I get this error in debug after using the breakpoint
VideoWriter has an optional parameter which tells whether the video is grayscale or color. Default ist color = true. Try
bool isColor = false;
writer = VideoWriter(filename,-1,fps,frameSize, isColor);

Converting Live Video Frames to Grayscale (OpenCV)

First and foremost, I should say that I'm a beginner to OpenCV. I'm trying to convert a live video stream from my webcam from RGB to Grayscale.
I have the following code in my function:
VideoCapture cap(0);
while (true)
{
Mat frame;
Mat grayscale;
cvtColor(frame, grayscale, CV_RGB2GRAY);
imshow("Debug Window", grayscale);
if (waitKey(30) >=0)
{
cout << "End of Stream";
break;
}
}
I know it isn't complete. I'm trying to find a way to take a frame of the video and send it to frame, manipulate it using cvtColor, then output it back to grayscale so I can display it on my screen.
If anyone could help, it would be much appreciated.
Please see this example, here the complete code exists, hope this will work for you:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat grayscale;
cvtColor(frame, grayscale, CV_RGB2GRAY);
imshow("MyVideo", grayscale);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
You just initialized the variable "frame" and forgot to assign an image to it. Since the variable "frame" is empty you won't get output. Grab a image and copy to frame from the video sequence "cap". This piece of code will do the job for you.
Mat frame;
bool bSuccess = cap.read(frame); // read a frame from the video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}

MP3 header missing from IP camera RTSP

I just want to show the IP camera video without sound, but this camera plays video with sound, and I get [mp3 # 0x107808800] Header missing in the console. How do I play without sound? I'm using vstarcam-30s. Here is my code:
int main (int argc, char *argv[])
{
VideoCapture vcap;
Mat image;
string videoStreamAddress =
"rtsp://[IPADDRESS]/profile2/media.smp";
if (!vcap.open(videoStreamAddress)) {
cout << "Error opening video stream or file" << endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
cout << "No frame" << endl;
waitKey();
}
imshow("Output Window", image);
if(waitKey(1) >= 0) break;
}
return 0;
}

Opencv cvCaptureFromCAM not loading any frame on OS X

I'm trying to write my own app with OpenCV but i have problem. cvCaptureFromCAM() not loading any frame on my Mac. I tried this way:
CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL) {
std::cerr << "!!! ERROR: vCaptureFromCAM No camera found\n";
return;
}
cvNamedWindow("webcam", CV_WINDOW_AUTOSIZE);
cvMoveWindow("webcam", 50, 50);
IplImage* src = NULL; for (;;) {
if ((src = cvQueryFrame(capture)) == NULL) {
std::cerr << "!!! ERROR: vQueryFrame\n";
break;
}
cvShowImage("webcam", &src);
}
cvReleaseCapture(&capture);
and this one:
cv::VideoCapture cap;
cap.open(0);
if( !cap.isOpened() )
{
std::cerr << "***Could not initialize capturing...***\n";
std::cerr << "Current parameter's value: \n";
return;
}
cv::Mat frame;
while(1){
cap >> frame;
if(frame.empty()){
std::cerr<<"frame is empty"<<std::endl;
break;
}
cv::imshow("", frame);
cv::waitKey(10);
}
Those two ways work good on linux, but not on my mac. My built-in camera works in skype and Photo Booth. What i do wrong? Have any ideas?