Optical Flow Sample.
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#ifndef DEFAULT_WAITKEY_DELAY
#define DEFAULT_WAITKEY_DELAY \
1
#endif
#define ERROR_CHECK_STATUS(status) \
{ \
vx_status status_ = (status); \
if (status_ != VX_SUCCESS) \
{ \
printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \
exit(1); \
} \
}
#define ERROR_CHECK_OBJECT(obj) \
{ \
vx_status status_ = vxGetStatus((vx_reference)(obj)); \
if (status_ != VX_SUCCESS) \
{ \
printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \
exit(1); \
} \
}
{
(void)context;
(void)ref;
printf("LOG: [ status = %d ] %s\n", status, string);
fflush(stdout);
}
void drawPoint(cv::Mat m_imgBGR,
int x,
int y)
{
cv::Point center(x, y);
cv::circle(m_imgBGR, center, 1, cv::Scalar(0, 0, 255), 2);
}
void drawArrow(cv::Mat m_imgBGR,
int x0,
int y0,
int x1,
int y1)
{
float dx = (float)(x1 - x0), dy = (float)(y1 - y0), arrow_len = sqrtf(dx * dx + dy * dy);
if ((arrow_len >= 3.0f) && (arrow_len <= 50.0f))
{
cv::Scalar color = cv::Scalar(0, 255, 255);
float tip_len = 5.0f + arrow_len * 0.1f, angle = atan2f(dy, dx);
cv::line(m_imgBGR, cv::Point(x0, y0), cv::Point(x1, y1), color, 1);
cv::line(m_imgBGR, cv::Point(x1, y1),
cv::Point(x1 - (int)(tip_len * cosf(angle + (float)CV_PI / 6)),
y1 - (int)(tip_len * sinf(angle + (float)CV_PI / 6))),
color, 1);
cv::line(m_imgBGR, cv::Point(x1, y1),
cv::Point(x1 - (int)(tip_len * cosf(angle - (float)CV_PI / 6)),
y1 - (int)(tip_len * sinf(angle - (float)CV_PI / 6))),
color, 1);
}
}
void drawText(cv::Mat m_imgBGR,
int x,
int y,
const char *text)
{
cv::putText(m_imgBGR, text, cv::Point(x, y), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8,
cv::Scalar(128, 0, 0), 1, cv::LineTypes::LINE_AA);
printf("text: %s\n", text);
}
{
if (key == ' ')
{
key = cv::waitKey(0);
}
if ((key == 'q') || (key == 'Q') || (key == 27) )
{
return true;
}
return false;
}
int main(
int argc,
char *argv[])
{
if (argc < 3)
{
printf(
"Usage:\n"
"\t./opticalFlow --video <Video File>\n"
"\t./opticalFlow --live <Capture Device ID>\n");
return 0;
}
harrisGradientSize, harrisBlockSize, currentKeypoints, NULL)};
for (
vx_size i = 0; i <
sizeof(nodesHarris) /
sizeof(nodesHarris[0]); i++)
{
}
previousKeypoints, currentKeypoints, lkTermination, epsilon,
numIterations, useInitialEstimate, lkWindowDimension)};
for (
vx_size i = 0; i <
sizeof(nodesTrack) /
sizeof(nodesTrack[0]); i++)
{
}
std::string option = argv[1];
cv::Mat input;
if (option == "--video")
{
cv::VideoCapture cap(argv[2]);
int totalFrames = cap.get(cv::CAP_PROP_FRAME_COUNT);
if (!cap.isOpened())
{
}
{
if (frameIndex >= totalFrames)
{
break;
}
cap >> input;
resize(input, input, cv::Size(width, height));
cv::imshow("inputWindow", input);
cv::Mat output = input;
if (cv::waitKey(30) >= 0) break;
cvRgbImageRegion.
end_x = width;
cvRgbImageRegion.
end_y = height;
vx_uint8 *cvRgbImageBuffer = input.data;
vx_size numCorners = 0, numTracking = 0;
sizeof(numCorners)));
if (numCorners > 0)
{
for (
vx_size i = 0; i < numCorners; i++)
{
{
numTracking++;
}
}
}
char text[128];
sprintf(text, "Keyboard: [ESC/Q] -- Quit [SPACE] -- Pause [FRAME %d]", frameIndex);
sprintf(text, "Number of Corners: %d [tracking %d]", (int)numCorners, (int)numTracking);
cv::imshow("opticalFlow", output);
}
}
else if (option == "--live")
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
}
{
cap >> input;
resize(input, input, cv::Size(width, height));
cv::imshow("inputWindow", input);
cv::Mat output = input;
if (cv::waitKey(30) >= 0) break;
cvRgbImageRegion.
end_x = width;
cvRgbImageRegion.
end_y = height;
vx_uint8 *cvRgbImageBuffer = input.data;
vx_size numCorners = 0, numTracking = 0;
sizeof(numCorners)));
if (numCorners > 0)
{
for (
vx_size i = 0; i < numCorners; i++)
{
{
numTracking++;
}
}
}
char text[128];
sprintf(text, "Keyboard: [ESC/Q] -- Quit [SPACE] -- Pause [FRAME %d]", frameIndex);
sprintf(text, "Number of Corners: %d [tracking %d]", (int)numCorners, (int)numTracking);
cv::imshow("opticalFlow", output);
}
}
else
{
printf(
"ERROR: Usage --\n"
"\t./opticalFlow --video <Video File>\n"
"\t./opticalFlow --live <Capture Device ID>\n");
return 0;
}
printf(
"GraphName NumFrames Avg(ms) Min(ms)\n"
"Harris %9d %7.3f %7.3f\n"
"Track %9d %7.3f %7.3f\n",
(
int)perfHarris.
num, (
float)perfHarris.
avg * 1e-6f, (
float)perfHarris.
min * 1e-6f,
(int)perfTrack.num, (float)perfTrack.avg * 1e-6f, (float)perfTrack.min * 1e-6f);
return 0;
}
int main()
Definition blur_pipeline.cpp:15
#define ERROR_CHECK_OBJECT(obj)
Definition bubble_pop.cpp:28
#define ERROR_CHECK_STATUS(status)
Definition bubble_pop.cpp:20
VX_API_ENTRY vx_status VX_API_CALL vxMapArrayRange(vx_array array, vx_size range_start, vx_size range_end, vx_map_id *map_id, vx_size *stride, void **ptr, vx_enum usage, vx_enum mem_type, vx_uint32 flags)
Allows the application to get direct access to a range of an array object.
VX_API_ENTRY vx_status VX_API_CALL vxQueryArray(vx_array arr, vx_enum attribute, void *ptr, vx_size size)
Queries the Array for some specific information.
VX_API_ENTRY vx_status VX_API_CALL vxReleaseArray(vx_array *arr)
Releases a reference of an Array object. The object may not be garbage collected until its total refe...
struct Array * vx_array
The Array Object. Array is a strongly-typed container for other data structures.
Definition vx_types.h:367
VX_API_ENTRY vx_status VX_API_CALL vxUnmapArrayRange(vx_array array, vx_map_id map_id)
Unmap and commit potential changes to an array object range that was previously mapped....
VX_API_ENTRY vx_array VX_API_CALL vxCreateArray(vx_context context, vx_enum item_type, vx_size capacity)
Creates a reference to an Array object.
@ VX_ARRAY_NUMITEMS
The number of items in the Array. Read-only. Use a vx_size parameter.
Definition vx_types.h:1259
uint32_t vx_uint32
A 32-bit unsigned value.
Definition vx_types.h:85
struct _vx_keypoint_t vx_keypoint_t
The keypoint data structure.
size_t vx_size
A wrapper of size_t to keep the naming convention uniform.
Definition vx_types.h:186
vx_enum vx_status
A formal status type with known fixed size.
Definition vx_types.h:550
int32_t vx_enum
Sets the standard enumeration type size to be a fixed quantity.
Definition vx_types.h:181
int32_t vx_int32
A 32-bit signed value.
Definition vx_types.h:105
struct _vx_rectangle_t vx_rectangle_t
The rectangle data structure that is shared with the users. The area of the rectangle can be computed...
char vx_char
An 8 bit ASCII character.
Definition vx_types.h:70
vx_enum vx_bool
A formal boolean type with known fixed size.
Definition vx_types.h:413
uint8_t vx_uint8
An 8-bit unsigned value.
Definition vx_types.h:75
float vx_float32
A 32-bit float value.
Definition vx_types.h:131
#define VX_CALLBACK
Defines calling convention for user callbacks.
Definition vx_types.h:63
@ VX_DF_IMAGE_IYUV
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes. This uses the BT709 full range by default.
Definition vx_types.h:841
@ VX_DF_IMAGE_RGB
A single plane of 24-bit pixel as 3 interleaved 8-bit units of R then G then B data....
Definition vx_types.h:816
@ VX_DF_IMAGE_U8
A single plane of unsigned 8-bit data. The range of data is not specified, as it may be extracted fro...
Definition vx_types.h:855
@ vx_false_e
The "false" value.
Definition vx_types.h:404
@ VX_TYPE_BOOL
A vx_bool.
Definition vx_types.h:450
@ VX_TYPE_FLOAT32
A vx_float32.
Definition vx_types.h:444
@ VX_TYPE_UINT32
A vx_uint32.
Definition vx_types.h:441
@ VX_TYPE_KEYPOINT
A vx_keypoint_t.
Definition vx_types.h:453
@ VX_CHANNEL_Y
Use to extract the LUMA channel, no matter the byte or packing order.
Definition vx_types.h:1323
@ VX_SUCCESS
No error.
Definition vx_types.h:543
@ VX_FAILURE
Indicates a generic error code, used when no other describes the error.
Definition vx_types.h:542
struct Context * vx_context
An opaque reference to the implementation context.
Definition vx_types.h:274
VX_API_ENTRY vx_context VX_API_CALL vxCreateContext(void)
Creates a vx_context.
VX_API_ENTRY vx_status VX_API_CALL vxReleaseContext(vx_context *context)
Releases the OpenVX object context.
@ VX_TERM_CRITERIA_BOTH
Indicates that both an iterations and eplison method are employed. Whichever one matches first causes...
Definition vx_types.h:1481
@ VX_READ_ONLY
The memory shall be treated by the system as if it were read-only. If the User writes to this memory,...
Definition vx_types.h:1515
@ VX_WRITE_ONLY
The memory shall be treated by the system as if it were write-only. If the User reads from this memor...
Definition vx_types.h:1519
@ VX_MEMORY_TYPE_HOST
The default memory type to import from the Host.
Definition vx_types.h:1338
VX_API_ENTRY vx_status VX_API_CALL vxAgeDelay(vx_delay delay)
Shifts the internal delay ring by one.
VX_API_ENTRY vx_reference VX_API_CALL vxGetReferenceFromDelay(vx_delay delay, vx_int32 index)
Retrieves a reference to a delay slot object.
VX_API_ENTRY vx_delay VX_API_CALL vxCreateDelay(vx_context context, vx_reference exemplar, vx_size num_slots)
Creates a Delay object.
VX_API_ENTRY vx_status VX_API_CALL vxReleaseDelay(vx_delay *delay)
Releases a reference to a delay object. The object may not be garbage collected until its total refer...
struct Delay * vx_delay
The delay object. This is like a ring buffer of objects that is maintained by the OpenVX implementati...
Definition vx_types.h:286
VX_API_ENTRY vx_status VX_API_CALL vxVerifyGraph(vx_graph graph)
Verifies the state of the graph before it is executed. This is useful to catch programmer errors and ...
VX_API_ENTRY vx_status VX_API_CALL vxQueryGraph(vx_graph graph, vx_enum attribute, void *ptr, vx_size size)
Allows the user to query attributes of the Graph.
VX_API_ENTRY vx_status VX_API_CALL vxReleaseGraph(vx_graph *graph)
Releases a reference to a graph. The object may not be garbage collected until its total reference co...
VX_API_ENTRY vx_graph VX_API_CALL vxCreateGraph(vx_context context)
Creates an empty graph.
struct Graph * vx_graph
An opaque reference to a graph.
Definition vx_types.h:263
VX_API_ENTRY vx_status VX_API_CALL vxProcessGraph(vx_graph graph)
This function causes the synchronous processing of a graph. If the graph has not been verified,...
@ VX_GRAPH_PERFORMANCE
Returns the overall performance of the graph. Read-only. Use a vx_perf_t parameter....
Definition vx_types.h:787
VX_API_ENTRY vx_image VX_API_CALL vxCreateVirtualImage(vx_graph graph, vx_uint32 width, vx_uint32 height, vx_df_image color)
Creates an opaque reference to an image buffer with no direct user access. This function allows setti...
struct _vx_imagepatch_addressing_t vx_imagepatch_addressing_t
The addressing image patch structure is used by the Host only to address pixels in an image patch....
struct Image * vx_image
An opaque reference to an image.
Definition vx_types.h:219
VX_API_ENTRY vx_status VX_API_CALL vxReleaseImage(vx_image *image)
Releases a reference to an image object. The object may not be garbage collected until its total refe...
uintptr_t vx_map_id
Holds the address of a variable where the map/unmap functions return a map identifier.
Definition vx_types.h:196
VX_API_ENTRY vx_status VX_API_CALL vxCopyImagePatch(vx_image image, const vx_rectangle_t *image_rect, vx_uint32 image_plane_index, const vx_imagepatch_addressing_t *user_addr, void *user_ptr, vx_enum usage, vx_enum user_mem_type)
Allows the application to copy a rectangular patch from/into an image object plane.
VX_API_ENTRY vx_image VX_API_CALL vxCreateImage(vx_context context, vx_uint32 width, vx_uint32 height, vx_df_image color)
Creates an opaque reference to an image buffer.
VX_API_ENTRY void VX_API_CALL vxAddLogEntry(vx_reference ref, vx_status status, const char *message,...)
Adds a line to the log.
VX_API_ENTRY void VX_API_CALL vxRegisterLogCallback(vx_context context, vx_log_callback_f callback, vx_bool reentrant)
Registers a callback facility to the OpenVX implementation to receive error logs.
struct Node * vx_node
An opaque reference to a kernel node.
Definition vx_types.h:253
VX_API_ENTRY vx_status VX_API_CALL vxReleaseNode(vx_node *node)
Releases a reference to a Node object. The object may not be garbage collected until its total refere...
#define VX_SCALE_PYRAMID_HALF
Use to indicate a half-scale pyramid.
Definition vx_types.h:1747
VX_API_ENTRY vx_status VX_API_CALL vxReleasePyramid(vx_pyramid *pyr)
Releases a reference to a pyramid object. The object may not be garbage collected until its total ref...
VX_API_ENTRY vx_pyramid VX_API_CALL vxCreatePyramid(vx_context context, vx_size levels, vx_float32 scale, vx_uint32 width, vx_uint32 height, vx_df_image format)
Creates a reference to a pyramid object of the supplied number of levels.
struct Pyramid * vx_pyramid
The Image Pyramid object. A set of scaled images.
Definition vx_types.h:327
VX_API_ENTRY vx_scalar VX_API_CALL vxCreateScalar(vx_context context, vx_enum data_type, const void *ptr)
Creates a reference to a scalar object. Also see ref sub_node_parameters.
VX_API_ENTRY vx_status VX_API_CALL vxReleaseScalar(vx_scalar *scalar)
Releases a reference to a scalar object. The object may not be garbage collected until its total refe...
VX_API_ENTRY vx_node VX_API_CALL vxColorConvertNode(vx_graph graph, vx_image input, vx_image output)
[Graph] Creates a color conversion node.
VX_API_ENTRY vx_node VX_API_CALL vxGaussianPyramidNode(vx_graph graph, vx_image input, vx_pyramid gaussian)
[Graph] Creates a node for a Gaussian Image Pyramid.
VX_API_ENTRY vx_node VX_API_CALL vxHarrisCornersNode(vx_graph graph, vx_image input, vx_scalar strength_thresh, vx_scalar min_distance, vx_scalar sensitivity, vx_int32 gradient_size, vx_int32 block_size, vx_array corners, vx_scalar num_corners)
[Graph] Creates a Harris Corners Node.
VX_API_ENTRY vx_node VX_API_CALL vxOpticalFlowPyrLKNode(vx_graph graph, vx_pyramid old_images, vx_pyramid new_images, vx_array old_points, vx_array new_points_estimates, vx_array new_points, vx_enum termination, vx_scalar epsilon, vx_scalar num_iterations, vx_scalar use_initial_estimate, vx_size window_dimension)
[Graph] Creates a Lucas Kanade Tracking Node.
void drawPoint(cv::Mat m_imgBGR, int x, int y)
Definition optical_flow.cpp:77
bool abortRequested()
Definition optical_flow.cpp:111
void drawArrow(cv::Mat m_imgBGR, int x0, int y0, int x1, int y1)
Definition optical_flow.cpp:83
void drawText(cv::Mat m_imgBGR, int x, int y, const char *text)
Definition optical_flow.cpp:103
#define DEFAULT_WAITKEY_DELAY
Definition optical_flow.cpp:39
vx_int32 stride_y
Stride in Y dimension in bytes.
Definition vx_types.h:1644
vx_int32 stride_x
Stride in X dimension in bytes.
Definition vx_types.h:1643
vx_int32 x
The x coordinate.
Definition vx_types.h:1758
vx_int32 tracking_status
A zero indicates a lost point. Initialized to 1 by corner detectors.
Definition vx_types.h:1763
vx_int32 y
The y coordinate.
Definition vx_types.h:1759
vx_uint64 min
Holds the minimum of the durations.
Definition vx_types.h:1666
vx_uint64 avg
Holds the average of the durations.
Definition vx_types.h:1665
vx_uint64 num
Holds the number of measurements.
Definition vx_types.h:1667
vx_uint32 start_x
The Start X coordinate.
Definition vx_types.h:1771
vx_uint32 start_y
The Start Y coordinate.
Definition vx_types.h:1772
vx_uint32 end_y
The End Y coordinate.
Definition vx_types.h:1774
vx_uint32 end_x
The End X coordinate.
Definition vx_types.h:1773
The top level OpenVX Header.
struct Reference * vx_reference
Definition vx_types.h:173
struct Scalar * vx_scalar
Definition vx_types.h:208