Artefact in read/write (copying) video file using OpenCV - c++

I am trying to read a video frame by frame and process it and write it back to another video file.
Part of the process is to read the video and in another part write it back into another video. I noted that when I am doing this, I am getting some artifacts on the generated video.
The simplest code that I can show the problem is as follow:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <conio.h>
using namespace cv;
using namespace std;
int main()
{
std::string inputFile = "D:\\MyData\\sample2.mp4";
std::string outputFileName = "D:\\MyData\\sample2out.mp4";
VideoCapture inputVideo;
inputVideo.open(inputFile);
VideoWriter videoWritter;
videoWritter.open(outputFileName, inputVideo.get(CAP_PROP_FOURCC), inputVideo.get(CAP_PROP_FPS),Size(inputVideo.get(CAP_PROP_FRAME_WIDTH), inputVideo.get(CAP_PROP_FRAME_HEIGHT)));
while (true) // loop until all frames read. this
{
cv::Mat frame;
// Capture frame-by-frame
inputVideo >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
videoWritter << frame;
}
inputVideo.release();
videoWritter.release();
}
when I run this code, the output video has some artifacts that can be seen in the following image:
I also noted that the size of the output file became a lot larger than the original one ( input file ~ 98 Mbyte and output is around 143 Mbyte).
How can I fix this artifact? why the output size is bigger than the input file when the compression and other parameters are set to be the same?
I am using OpenCV 4.6 and Visual studio 2022 on windows 10.
Edit 1
I run the application on two videos, both of which are downloaded from the internet, one from a site (I can not remember the name of the site but it offers free videos) and the other downloaded from Youtube using a youtube downloader website.
I can share the videos, but I do not know how to upload them to this site. How can I share it?
I can see the same issue in both videos.
If I extract images and save them into a jpeg, the images are correct. but when I encode them into a video I can see the issue.
Edit 2
Sample files are uploaded here: https://www.filemail.com/d/kobthfbkbjanwqm

Related

OpenCV, can't release CAP_OPENCV_MJPEG VideoWriter for long records

Hi stackoverflow community,
I recently had a problem with the performance of the opencv VideoWriter (see here), and I am now using cv::CAP_OPENCV_MJPEG as a backend instead of cv::CAP_FFMPEG.
Now I am running in another issue, so my code looks like this:
cv::VideoWriter videoWriter(path, cv::CAP_OPENCV_MJPEG, fourcc, fps, *size);
videoWriter.set(cv::VIDEOWRITER_PROP_QUALITY, 100);
int id = metaDataWriter.insertNow(path);
while (this->isRunning) {
while (!this->stackFrames.empty()) {
cv:Mat m = this->stackFrames.pop();
videoWriter << m;
}
}
videoWriter.release();
This loop runs in a separate thread and "isRunning" will be switched from outside. There is a stack with cv::Mat (stackFrames) which is filled by another thread that captures the images by a video capture card. This works fine, but if the file size is too big, several GB, I get the following error when the videoWriter.release() is called:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.4.0) /home/michael-gary/opencv/opencv/modules/videoio/src/container_avi.cpp:27: error: (-211:One of the arguments' values is out of range) Failed to write AVI file: chunk size is out of bounds in function 'safe_int_cast'
I tried to change the video container from .avi to .mkv, .mp4, .mpg but none of them is working, it does not even create the file. Only .avi is working, but fails by the release.
For now I will try to write multiple files, so I don't run in this problem, but I would like to face the problem itself.
Open for any suggestions
BR Michael
AVI files are practically limited in size.
when you force it to use OPENCV_MJPEG, it will only use OpenCV's own writing methods, which can only do AVI and MJPEG.
if you need a different container, you have to use a different backend, such as FFMPEG. you can still have MJPG for a codec (fourcc).

Capturing from webcam under WinPE?

I am writing a suite of hardware tests that will run in a windows PE environment. I need to test the webcam but so far I am stumped. I compiled a small webcam capture program using the opencv library, but it is unable to detect the webcam. I have tried loading additional drivers, but it hasn't worked. Unfortunately I have not found any help online since apparently no one else is crazy/stupid enough to attempt this. Is this idea fundamentally flawed in some way, or is there a way to do this? Any help is appreciated.
Here is the code I am using that compiles and runs on my windows 10 machine:
#include "opencv2/opencv.hpp"
#include <cstdio>
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.isOpened()) // check if we succeeded
{
printf("Unable to open webcam");
return -1;
}
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
imshow("this is you, smile! :)", frame);
if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
Edit: I realized that the drivers were not loading on startup so I loaded them manually with drvload.exe. Now the webcam shows as functional when I do a WQL query(ConfigManagerErrorCode = 0) but my opencv program will still not detect it.
You won't get your cam to work, without adding some DLLs to your PE environment.
PE is a minimalistic WIndows that misses lots of functions due to minimize memory and diskspace consumption. It is only designed to offline edit Windows images, not to do hardware testing. Ran into similar issues some months ago.
I'm not sure if linking to this project is allowed here, but they sure have helpful information for you on that: TheOven

Opencv VideoCapture, not opening some video files

I'm trying to use the opencv librarie to display video files to the screen using sdl-2. I got it to work with smaller files(20.5Mb), but when i tryed using bigger files (100 and 136Mb) it didnt work and i go the message "ERROR: Coudnt load the video < video.avi>".
cv::VideoCapture video;
video.open(videoName);
if (!video.isOpened())
{
SDL_Log("ERROR: Coudnt load the video <%s>", videoName.c_str());
return;
}
I don't know if size was the problem, but that would make the most sense to me. I've not found any documentation on size restrictions thought. Any ideas on how to fix it?
PS: All were .avi files, and the file's name was not the problem. Was able to open them with ffplay if that matters at all. OpenCV version 2.4.8

Unable to get Video feed from D-Link DCS 932L using openCv

I am trying to display video feed from IP- Camera(D-Link DCS 932L). I Have gone through topics for the same and tried the code from different posts, but am unable to get the video feed from the camera.
Here's the code which i tried.
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv;
int main(int argc, char *argv[])
{
Mat frame;
namedWindow("video", 1);
String url = "http://admin:admin#172.32.20.55:80/image/jpeg.cgi";
VideoCapture cap(url);
/* VideoCapture cap(0);*/
while ( cap.isOpened() )
{
cap >> frame;
if(frame.empty()) break;
imshow("video", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
I tried many different kind of url's but i was unable to display any video feed. I thought it might be code problem so I even tried displaying the USB Webcam and it worked. So now i come to conclusion that the problem seems to be with URL which am passing. Heres the list of urls which I tried. I got this Url options from iSpy.Here are those URL's
(JPEG)http://admin:admin#172.32.20.55:80?IMAGE.JPG
(JPEG)http://admin:admin#172.32.20.55:80/image/jpeg.cgi
(MPEG)http://admin:admin172.32.20.55:80/video.cgi?resolution=VGA
(MPEG)http://admin:admin172.32.20.55:80/video/mjpg.cgi
(MPEG)http://admin:admin172.32.20.55:80/mjpeg.cgi? user=admin&password=admin&channel=0
(MPEG)http://admin:pnqadmin172.32.20.55:80/VIDEO.CGI
Please let me know what can be probable problem for displaying the video feed.
Is their something to do with the setting of the OpenCv or something else.Please note that am using VS2010 and C++ Need help of all the Expert out their.
Thanks in advance.
I solved my problem. The problem was with URL. I changed the URL and it worked smooth..!
The URL i used was as follows.
"http://USER:PWD#IPADDRESS:8088/mjpeg.cgi?user=USERNAME&password=PWD&channel=0&.mjpg";
I keep getting the same error:
warning: Error opening file <../../modules/highgui/src/cap_ffmpeg_impl.hpp:529>
I was trying to stream MJPG video from a Foscam IP camera. The URL opened just fine but I couldn't read any frames. May be there was some problem with the video codec.
Here's a hack written in Python that worked for me: https://stackoverflow.com/a/18411168/3183051
Perhaps my answer is too late. Check if opencv_ffmpegXXX.dll or opencv_ffmpegXXX_64.dll (if you are building 64bit executable) is in the same folder where your executable is. Replace XXX with the number of opencv version you use.

OpenCV displaying avi file encoded in I420

I am trying to write a video processing application using OpenCV 2.4.2 (in Visual C++ 2010 Express on Windows 7) but I am having trouble displaying certain AVI files. Most display correctly, but when I use an AVI file encoded in I420 format all I get is a striped pink image for every frame (it displays correctly in regular media players).
Output displayed: http://i.imgur.com/BOu6c.png?1
Currently, I am using the C++ API, but the same thing happens when I use the C API (code from this page: http://nashruddin.com/how_to_play_avi_files_with_opencv). I find this strange, because in most answers on this site and resources on the web, they explicitly recommend to use the I420 encoding. Does anyone know what could be causing this or how to fix it?
Here is a trimmed down version of the code I am using:
int main(int argc, char** argv){
string fname = "test.avi";
VideoCapture capture(fname);
if(!capture.isOpened()){
cerr << "error opening " << fname << endl;
return -1;
}
Mat frame;
namedWindow("output");
double rate = capture.get(CV_CAP_PROP_FPS);
int delay = 1000/rate;
while(true){
if(!capture.read(frame)) break;
cv::imshow("output", frame);
if(waitKey(delay) >= 0) break;
}
capture.release();
return 0;
}
I am using is the pre-compiled version of OpenCV if that makes a difference (http://sourceforge.net/projects/opencvlibrary/).
Ok, so I managed to test on a few more computers. One just crashed and, on another, the video played fine. It turns out that it was a problem with FFMPEG being enabled in the default OpenCV compilation having problems with the uncompressed AVI. Recompile OpenCV with FFMPEG disabled or just use a different codec to compress the video.