2014-11-27 20:39:05 +08:00
|
|
|
Video Input with OpenCV and similarity measurement {#tutorial_video_input_psnr_ssim}
|
|
|
|
==================================================
|
|
|
|
|
2020-12-08 00:13:54 +08:00
|
|
|
@tableofcontents
|
|
|
|
|
2020-12-05 06:46:00 +08:00
|
|
|
@prev_tutorial{tutorial_raster_io_gdal}
|
2020-05-20 06:59:28 +08:00
|
|
|
@next_tutorial{tutorial_video_write}
|
|
|
|
|
2020-12-05 06:46:00 +08:00
|
|
|
| | |
|
|
|
|
| -: | :- |
|
|
|
|
| Original author | Bernát Gábor |
|
|
|
|
| Compatibility | OpenCV >= 3.0 |
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Goal
|
|
|
|
----
|
|
|
|
|
|
|
|
Today it is common to have a digital video recording system at your disposal. Therefore, you will
|
|
|
|
eventually come to the situation that you no longer process a batch of images, but video streams.
|
|
|
|
These may be of two kinds: real-time image feed (in the case of a webcam) or prerecorded and hard
|
2018-01-07 08:15:43 +08:00
|
|
|
disk drive stored files. Luckily OpenCV treats these two in the same manner, with the same C++
|
2014-11-27 20:39:05 +08:00
|
|
|
class. So here's what you'll learn in this tutorial:
|
|
|
|
|
|
|
|
- How to open and read video streams
|
|
|
|
- Two ways for checking image similarity: PSNR and SSIM
|
|
|
|
|
|
|
|
The source code
|
|
|
|
---------------
|
|
|
|
|
|
|
|
As a test case where to show off these using OpenCV I've created a small program that reads in two
|
|
|
|
video files and performs a similarity check between them. This is something you could use to check
|
|
|
|
just how well a new video compressing algorithms works. Let there be a reference (original) video
|
|
|
|
like [this small Megamind clip
|
2016-06-14 21:01:36 +08:00
|
|
|
](https://github.com/opencv/opencv/tree/master/samples/data/Megamind.avi) and [a compressed
|
|
|
|
version of it ](https://github.com/opencv/opencv/tree/master/samples/data/Megamind_bugy.avi).
|
2014-11-27 20:39:05 +08:00
|
|
|
You may also find the source code and these video file in the
|
2016-02-12 18:42:44 +08:00
|
|
|
`samples/data` folder of the OpenCV source library.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2020-02-22 19:42:26 +08:00
|
|
|
@add_toggle_cpp
|
2015-12-17 12:46:10 +08:00
|
|
|
@include cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp
|
2020-02-22 19:42:26 +08:00
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
@include samples/python/tutorial_code/videoio/video-input-psnr-ssim.py
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
How to read a video stream (online-camera or offline-file)?
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
2014-11-28 00:54:13 +08:00
|
|
|
Essentially, all the functionalities required for video manipulation is integrated in the @ref cv::VideoCapture
|
|
|
|
C++ class. This on itself builds on the FFmpeg open source library. This is a basic
|
2014-11-27 20:39:05 +08:00
|
|
|
dependency of OpenCV so you shouldn't need to worry about this. A video is composed of a succession
|
|
|
|
of images, we refer to these in the literature as frames. In case of a video file there is a *frame
|
|
|
|
rate* specifying just how long is between two frames. While for the video cameras usually there is a
|
2018-02-09 02:04:25 +08:00
|
|
|
limit of just how many frames they can digitize per second, this property is less important as at
|
2014-11-27 20:39:05 +08:00
|
|
|
any time the camera sees the current snapshot of the world.
|
|
|
|
|
|
|
|
The first task you need to do is to assign to a @ref cv::VideoCapture class its source. You can do
|
2014-11-28 00:54:13 +08:00
|
|
|
this either via the @ref cv::VideoCapture::VideoCapture or its @ref cv::VideoCapture::open function. If this argument is an
|
2014-11-27 20:39:05 +08:00
|
|
|
integer then you will bind the class to a camera, a device. The number passed here is the ID of the
|
|
|
|
device, assigned by the operating system. If you have a single camera attached to your system its ID
|
|
|
|
will probably be zero and further ones increasing from there. If the parameter passed to these is a
|
|
|
|
string it will refer to a video file, and the string points to the location and name of the file.
|
|
|
|
For example, to the upper source code a valid command line is:
|
|
|
|
@code{.bash}
|
|
|
|
video/Megamind.avi video/Megamind_bug.avi 35 10
|
|
|
|
@endcode
|
|
|
|
We do a similarity check. This requires a reference and a test case video file. The first two
|
|
|
|
arguments refer to this. Here we use a relative address. This means that the application will look
|
|
|
|
into its current working directory and open the video folder and try to find inside this the
|
|
|
|
*Megamind.avi* and the *Megamind_bug.avi*.
|
|
|
|
@code{.cpp}
|
|
|
|
const string sourceReference = argv[1],sourceCompareWith = argv[2];
|
|
|
|
|
|
|
|
VideoCapture captRefrnc(sourceReference);
|
|
|
|
// or
|
|
|
|
VideoCapture captUndTst;
|
|
|
|
captUndTst.open(sourceCompareWith);
|
|
|
|
@endcode
|
2014-11-28 00:54:13 +08:00
|
|
|
To check if the binding of the class to a video source was successful or not use the @ref cv::VideoCapture::isOpened
|
|
|
|
function:
|
2014-11-27 20:39:05 +08:00
|
|
|
@code{.cpp}
|
|
|
|
if ( !captRefrnc.isOpened())
|
|
|
|
{
|
|
|
|
cout << "Could not open reference " << sourceReference << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
Closing the video is automatic when the objects destructor is called. However, if you want to close
|
2014-11-28 00:54:13 +08:00
|
|
|
it before this you need to call its @ref cv::VideoCapture::release function. The frames of the video are just
|
2014-11-27 20:39:05 +08:00
|
|
|
simple images. Therefore, we just need to extract them from the @ref cv::VideoCapture object and put
|
|
|
|
them inside a *Mat* one. The video streams are sequential. You may get the frames one after another
|
2014-11-28 00:54:13 +08:00
|
|
|
by the @ref cv::VideoCapture::read or the overloaded \>\> operator:
|
2014-11-27 20:39:05 +08:00
|
|
|
@code{.cpp}
|
|
|
|
Mat frameReference, frameUnderTest;
|
|
|
|
captRefrnc >> frameReference;
|
2019-02-11 16:45:16 +08:00
|
|
|
captUndTst.read(frameUnderTest);
|
2014-11-27 20:39:05 +08:00
|
|
|
@endcode
|
|
|
|
The upper read operations will leave empty the *Mat* objects if no frame could be acquired (either
|
|
|
|
cause the video stream was closed or you got to the end of the video file). We can check this with a
|
|
|
|
simple if:
|
|
|
|
@code{.cpp}
|
|
|
|
if( frameReference.empty() || frameUnderTest.empty())
|
|
|
|
{
|
|
|
|
// exit the program
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
A read method is made of a frame grab and a decoding applied on that. You may call explicitly these
|
2014-11-28 00:54:13 +08:00
|
|
|
two by using the @ref cv::VideoCapture::grab and then the @ref cv::VideoCapture::retrieve functions.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Videos have many-many information attached to them besides the content of the frames. These are
|
|
|
|
usually numbers, however in some case it may be short character sequences (4 bytes or less). Due to
|
2014-11-28 00:54:13 +08:00
|
|
|
this to acquire these information there is a general function named @ref cv::VideoCapture::get that returns double
|
2014-11-27 20:39:05 +08:00
|
|
|
values containing these properties. Use bitwise operations to decode the characters from a double
|
|
|
|
type and conversions where valid values are only integers. Its single argument is the ID of the
|
|
|
|
queried property. For example, here we get the size of the frames in the reference and test case
|
|
|
|
video file; plus the number of frames inside the reference.
|
|
|
|
@code{.cpp}
|
|
|
|
Size refS = Size((int) captRefrnc.get(CAP_PROP_FRAME_WIDTH),
|
|
|
|
(int) captRefrnc.get(CAP_PROP_FRAME_HEIGHT)),
|
|
|
|
|
|
|
|
cout << "Reference frame resolution: Width=" << refS.width << " Height=" << refS.height
|
|
|
|
<< " of nr#: " << captRefrnc.get(CAP_PROP_FRAME_COUNT) << endl;
|
|
|
|
@endcode
|
|
|
|
When you are working with videos you may often want to control these values yourself. To do this
|
2014-11-28 00:54:13 +08:00
|
|
|
there is a @ref cv::VideoCapture::set function. Its first argument remains the name of the property you want to
|
2014-11-27 20:39:05 +08:00
|
|
|
change and there is a second of double type containing the value to be set. It will return true if
|
|
|
|
it succeeds and false otherwise. Good examples for this is seeking in a video file to a given time
|
|
|
|
or frame:
|
|
|
|
@code{.cpp}
|
|
|
|
captRefrnc.set(CAP_PROP_POS_MSEC, 1.2); // go to the 1.2 second in the video
|
|
|
|
captRefrnc.set(CAP_PROP_POS_FRAMES, 10); // go to the 10th frame of the video
|
|
|
|
// now a read operation would read the frame at the set position
|
|
|
|
@endcode
|
2014-11-28 00:54:13 +08:00
|
|
|
For properties you can read and change look into the documentation of the @ref cv::VideoCapture::get and
|
|
|
|
@ref cv::VideoCapture::set functions.
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2020-11-10 20:36:13 +08:00
|
|
|
### Image similarity - PSNR and SSIM
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
We want to check just how imperceptible our video converting operation went, therefore we need a
|
|
|
|
system to check frame by frame the similarity or differences. The most common algorithm used for
|
|
|
|
this is the PSNR (aka **Peak signal-to-noise ratio**). The simplest definition of this starts out
|
2020-11-14 10:47:44 +08:00
|
|
|
from the *mean squared error*. Let there be two images: I1 and I2; with a two dimensional size i and
|
2014-11-27 20:39:05 +08:00
|
|
|
j, composed of c number of channels.
|
|
|
|
|
|
|
|
\f[MSE = \frac{1}{c*i*j} \sum{(I_1-I_2)^2}\f]
|
|
|
|
|
|
|
|
Then the PSNR is expressed as:
|
|
|
|
|
|
|
|
\f[PSNR = 10 \cdot \log_{10} \left( \frac{MAX_I^2}{MSE} \right)\f]
|
|
|
|
|
2017-08-14 22:16:04 +08:00
|
|
|
Here the \f$MAX_I\f$ is the maximum valid value for a pixel. In case of the simple single byte image
|
2014-11-27 20:39:05 +08:00
|
|
|
per pixel per channel this is 255. When two images are the same the MSE will give zero, resulting in
|
|
|
|
an invalid divide by zero operation in the PSNR formula. In this case the PSNR is undefined and as
|
|
|
|
we'll need to handle this case separately. The transition to a logarithmic scale is made because the
|
2020-11-10 20:36:13 +08:00
|
|
|
pixel values have a very wide dynamic range. All this translated to OpenCV and a function looks
|
2014-11-27 20:39:05 +08:00
|
|
|
like:
|
2020-02-22 19:42:26 +08:00
|
|
|
|
|
|
|
@add_toggle_cpp
|
2020-11-10 20:36:13 +08:00
|
|
|
@snippet cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp get-psnr
|
2020-02-22 19:42:26 +08:00
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
2020-11-10 20:36:13 +08:00
|
|
|
@snippet samples/python/tutorial_code/videoio/video-input-psnr-ssim.py get-psnr
|
2020-02-22 19:42:26 +08:00
|
|
|
@end_toggle
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Typically result values are anywhere between 30 and 50 for video compression, where higher is
|
|
|
|
better. If the images significantly differ you'll get much lower ones like 15 and so. This
|
|
|
|
similarity check is easy and fast to calculate, however in practice it may turn out somewhat
|
|
|
|
inconsistent with human eye perception. The **structural similarity** algorithm aims to correct
|
|
|
|
this.
|
|
|
|
|
|
|
|
Describing the methods goes well beyond the purpose of this tutorial. For that I invite you to read
|
|
|
|
the article introducing it. Nevertheless, you can get a good image of it by looking at the OpenCV
|
|
|
|
implementation below.
|
|
|
|
|
2020-03-21 08:25:49 +08:00
|
|
|
@note
|
2014-11-28 00:54:13 +08:00
|
|
|
SSIM is described more in-depth in the: "Z. Wang, A. C. Bovik, H. R. Sheikh and E. P.
|
2014-11-27 20:39:05 +08:00
|
|
|
Simoncelli, "Image quality assessment: From error visibility to structural similarity," IEEE
|
|
|
|
Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004." article.
|
2014-11-28 00:54:13 +08:00
|
|
|
|
2020-02-22 19:42:26 +08:00
|
|
|
@add_toggle_cpp
|
2020-11-10 20:36:13 +08:00
|
|
|
@snippet samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp get-mssim
|
2020-02-22 19:42:26 +08:00
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2020-02-22 19:42:26 +08:00
|
|
|
@add_toggle_python
|
2020-11-10 20:36:13 +08:00
|
|
|
@snippet samples/python/tutorial_code/videoio/video-input-psnr-ssim.py get-mssim
|
2020-02-22 19:42:26 +08:00
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
This will return a similarity index for each channel of the image. This value is between zero and
|
|
|
|
one, where one corresponds to perfect fit. Unfortunately, the many Gaussian blurring is quite
|
2021-09-28 20:29:47 +08:00
|
|
|
costly, so while the PSNR may work in a real time like environment (24 frames per second) this will
|
2014-11-27 20:39:05 +08:00
|
|
|
take significantly more than to accomplish similar performance results.
|
|
|
|
|
|
|
|
Therefore, the source code presented at the start of the tutorial will perform the PSNR measurement
|
|
|
|
for each frame, and the SSIM only for the frames where the PSNR falls below an input value. For
|
|
|
|
visualization purpose we show both images in an OpenCV window and print the PSNR and MSSIM values to
|
|
|
|
the console. Expect to see something like:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/outputVideoInput.png)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
You may observe a runtime instance of this on the [YouTube here](https://www.youtube.com/watch?v=iOcNljutOgg).
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2017-10-17 18:40:16 +08:00
|
|
|
@youtube{iOcNljutOgg}
|