OpenCV 5.0.0
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/cpp/snippets/dis_opticalflow.cpp

An example using the dense optical flow and DIS optical flow algorithms

using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
CommandLineParser parser(argc, argv, "{ @video | vtest.avi | use video as input }");
string filename = samples::findFileOrKeep(parser.get<string>("@video"));
cap.open(filename);
if(!cap.isOpened())
{
printf("ERROR: Cannot open file %s\n", filename.c_str());
parser.printMessage();
return -1;
}
Mat prevgray, gray, rgb, frame;
Mat flow, flow_uv[2];
Mat mag, ang;
Mat hsv_split[3], hsv;
char ret;
while(true)
{
cap >> frame;
if (frame.empty())
break;
cvtColor(frame, gray, COLOR_BGR2GRAY);
if (!prevgray.empty())
{
algorithm->calc(prevgray, gray, flow);
split(flow, flow_uv);
multiply(flow_uv[1], -1, flow_uv[1]);
cartToPolar(flow_uv[0], flow_uv[1], mag, ang, true);
normalize(mag, mag, 0, 1, NORM_MINMAX);
hsv_split[0] = ang;
hsv_split[1] = mag;
hsv_split[2] = Mat::ones(ang.size(), ang.type());
merge(hsv_split, 3, hsv);
cvtColor(hsv, rgb, COLOR_HSV2BGR);
imshow("flow", rgb);
imshow("orig", frame);
}
if ((ret = (char)waitKey(20)) > 0)
break;
std::swap(prevgray, gray);
}
return 0;
}
Designed for command line parsing.
Definition utility.hpp:915
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition utility.hpp:981
void printMessage() const
Print help message.
@ PRESET_MEDIUM
Definition tracking.hpp:775
static Ptr< DISOpticalFlow > create(int preset=DISOpticalFlow::PRESET_FAST)
Creates an instance of DISOpticalFlow.
Comma-separated Matrix Initializer.
Definition mat.hpp:964
MatSize size
Definition mat.hpp:2511
static CV_NODISCARD_STD MatExpr ones(int rows, int cols, int type)
Returns an array of all 1's of the specified size and type.
bool empty() const
Returns true if the array has no elements.
int type() const
Returns the type of a matrix element.
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:790
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
void split(const Mat &src, Mat *mvbegin)
Divides a multi-channel array into several single-channel arrays.
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
Normalizes the norm or value range of an array.
void multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
Calculates the per-element scaled product of two arrays.
void cartToPolar(InputArray x, InputArray y, OutputArray magnitude, OutputArray angle, bool angleInDegrees=false)
Calculates the magnitude and angle of 2D vectors.
@ NORM_MINMAX
flag
Definition base.hpp:141
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
cv::String findFileOrKeep(const cv::String &relative_path, bool silentMode=false)
Definition utility.hpp:1289
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
@ COLOR_HSV2BGR
[8U/32F] backward conversions HSV to RGB/BGR with H range 0..180 if 8 bit image
Definition imgproc.hpp:601
@ COLOR_BGR2GRAY
[8U/16U/32F] convert between RGB/BGR and grayscale, color conversions
Definition imgproc.hpp:547
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition core.hpp:107
STL namespace.