TIOVX User Guide
vx_tutorial_graph_image_gradients.c
Go to the documentation of this file.
1 /*
2 *
3 * Copyright (c) 2017 Texas Instruments Incorporated
4 *
5 * All rights reserved not granted herein.
6 *
7 * Limited License.
8 *
9 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10 * license under copyrights and patents it now or hereafter owns or controls to make,
11 * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12 * terms herein. With respect to the foregoing patent license, such license is granted
13 * solely to the extent that any such patent is necessary to Utilize the software alone.
14 * The patent license shall not apply to any combinations which include this software,
15 * other than combinations with devices manufactured by or for TI ("TI Devices").
16 * No hardware patent is licensed hereunder.
17 *
18 * Redistributions must preserve existing copyright notices and reproduce this license
19 * (including the above copyright notice and the disclaimer and (if applicable) source
20 * code license limitations below) in the documentation and/or other materials provided
21 * with the distribution
22 *
23 * Redistribution and use in binary form, without modification, are permitted provided
24 * that the following conditions are met:
25 *
26 * * No reverse engineering, decompilation, or disassembly of this software is
27 * permitted with respect to any software provided in binary form.
28 *
29 * * any redistribution and use are licensed by TI for use only with TI Devices.
30 *
31 * * Nothing shall obligate TI to provide you with source code for the software
32 * licensed and provided to you in object code.
33 *
34 * If software source code is provided to you, modification and redistribution of the
35 * source code are permitted provided that the following conditions are met:
36 *
37 * * any redistribution and use of the source code, including any resulting derivative
38 * works, are licensed by TI for use only with TI Devices.
39 *
40 * * any redistribution and use of any object code compiled from the source code
41 * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42 *
43 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44 *
45 * may be used to endorse or promote products derived from this software without
46 * specific prior written permission.
47 *
48 * DISCLAIMER.
49 *
50 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
60 *
61 */
62 
63 
64 
93 #include <stdio.h>
94 #include <VX/vx.h>
95 #include <TI/tivx.h>
96 #include <utility.h>
97 
99 #define IN_FILE_NAME "${VX_TEST_DATA_PATH}/colors.bmp"
100 
102 #define PHASE_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_graph_image_gradients_phase_out.bmp"
103 
105 #define MAGNITUDE_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_graph_image_gradients_magnitude_out.bmp"
106 
108 #define GRAD_X_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_graph_image_gradients_grad_x_out.bmp"
109 
111 #define GRAD_Y_FILE_NAME "${VX_TEST_DATA_PATH}/vx_tutorial_graph_image_gradients_grad_y_out.bmp"
112 
113 #define NUM_NODES (6u)
114 
119 {
127  vx_context context;
128  vx_image in_image = NULL;
129  vx_image grad_x = NULL;
130  vx_image grad_y = NULL;
131  vx_image magnitude = NULL;
132  vx_image phase = NULL;
133  vx_image magnitude_image = NULL;
134  vx_image grad_x_image = NULL;
135  vx_image grad_y_image = NULL;
136  vx_graph graph = NULL;
137  vx_scalar shift = NULL;
138  vx_node node[NUM_NODES] = {NULL};
140  int32_t shift_value = 0;
141  vx_uint32 width, height;
142  vx_status status;
143  uint32_t i;
144 
145  printf(" vx_tutorial_graph_image_gradients: Tutorial Started !!! \n");
146 
154  context = vxCreateContext();
157  printf(" Loading file %s ...\n", IN_FILE_NAME);
158 
169  vxSetReferenceName((vx_reference)in_image, "INPUT");
170 
178  show_image_attributes(in_image);
181  vxQueryImage(in_image, (vx_enum)VX_IMAGE_WIDTH, &width, sizeof(vx_uint32));
182  vxQueryImage(in_image, (vx_enum)VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32));
183 
191  grad_x = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_S16);
194  vxSetReferenceName((vx_reference)grad_x, "GRAD_X");
195 
203  show_image_attributes(grad_x);
213  grad_y = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_S16);
216  vxSetReferenceName((vx_reference)grad_y, "GRAD_Y");
217 
225  show_image_attributes(grad_y);
235  magnitude = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_S16);
238  vxSetReferenceName((vx_reference)magnitude, "MAGNITUDE");
239 
247  show_image_attributes(magnitude);
257  magnitude_image = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
260  vxSetReferenceName((vx_reference)magnitude_image, "MAGNITUDE_IMAGE");
261 
269  show_image_attributes(magnitude_image);
279  grad_x_image = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
282  vxSetReferenceName((vx_reference)grad_x_image, "GRAD_X_IMAGE");
283 
291  show_image_attributes(grad_x_image);
301  grad_y_image = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
304  vxSetReferenceName((vx_reference)grad_y_image, "GRAD_Y_IMAGE");
305 
313  show_image_attributes(grad_y_image);
316  shift = vxCreateScalar(context, (vx_enum)VX_TYPE_INT32, &shift_value);
317 
325  phase = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
327  vxSetReferenceName((vx_reference)phase, "PHASE");
328 
336  show_image_attributes(phase);
344  graph = vxCreateGraph(context);
346  i = 0;
347 
356  node[i] = vxSobel3x3Node(graph, in_image, grad_x, grad_y);
359  vxSetReferenceName((vx_reference)node[i], "SOBEL3x3");
360 
369  i++;
370 
379  node[i] = vxMagnitudeNode(graph, grad_x, grad_y, magnitude);
382  vxSetReferenceName((vx_reference)node[i], "MAGNITUDE");
383 
392  i++;
393 
402  node[i] = vxPhaseNode(graph, grad_x, grad_y, phase);
405  vxSetReferenceName((vx_reference)node[i], "PHASE");
413  {
415  }
416  else /* DSP2 is not present on some platforms, so changing target to DSP1 */
417  {
419  }
421  i++;
422 
432  node[i] = vxConvertDepthNode(graph,
433  magnitude, magnitude_image,
435  shift);
438  vxSetReferenceName((vx_reference)node[i], "MAGNITUDE_IMAGE");
439 
449  i++;
450 
460  node[i] = vxConvertDepthNode(graph,
461  grad_x, grad_x_image,
463  shift);
466  vxSetReferenceName((vx_reference)node[i], "GRAD_X_IMAGE");
467 
477  i++;
478 
488  node[i] = vxConvertDepthNode(graph,
489  grad_y, grad_y_image,
491  shift);
492  vxSetReferenceName((vx_reference)node[i], "GRAD_Y_IMAGE");
493 
501  {
503  }
504  else /* DSP2 is not present on some platforms, so changing target to DSP1 */
505  {
507  }
510  i++;
511 
519  status = vxVerifyGraph(graph);
525  tivxExportGraphToDot(graph, ".", "vx_tutorial_graph_image_gradients");
535  show_graph_attributes(graph);
538  for(i=0; i<sizeof(node)/sizeof(node[0]); i++)
539  {
540  if(node[i])
541  {
549  show_node_attributes(node[i]);
551  }
552  }
553 
554  if(status==(vx_status)VX_SUCCESS)
555  {
556  printf(" Executing graph ...\n");
557 
566  vxScheduleGraph(graph);
575  vxWaitGraph(graph);
578  printf(" Executing graph ... Done !!!\n");
579 
587  show_graph_attributes(graph);
589  for(i=0; i<sizeof(node)/sizeof(node[0]); i++)
590  {
591  if(node[i])
592  {
600  show_node_attributes(node[i]);
602  }
603  }
604 
605  printf(" Saving to file %s ...\n", PHASE_FILE_NAME);
617  printf(" Saving to file %s ...\n", MAGNITUDE_FILE_NAME);
629  printf(" Saving to file %s ...\n", GRAD_X_FILE_NAME);
641  printf(" Saving to file %s ...\n", GRAD_Y_FILE_NAME);
652  }
653 
660  vxReleaseImage(&in_image);
661  vxReleaseImage(&grad_x);
662  vxReleaseImage(&grad_y);
663  vxReleaseImage(&grad_x_image);
664  vxReleaseImage(&grad_y_image);
665  vxReleaseImage(&phase);
666  vxReleaseImage(&magnitude);
667  vxReleaseImage(&magnitude_image);
676  vxReleaseScalar(&shift);
679  for(i=0; i<sizeof(node)/sizeof(node[0]); i++)
680  {
681  if(node[i])
682  {
689  vxReleaseNode(&node[i]);
691  }
692  }
693 
700  vxReleaseGraph(&graph);
711  vxReleaseContext(&context);
714  printf(" vx_tutorial_graph_image_gradients: Tutorial Done !!! \n");
715  printf(" \n");
716 }
struct _vx_image * vx_image
vx_bool
vx_status VX_API_CALL vxQueryImage(vx_image image, vx_enum attribute, void *ptr, vx_size size)
vx_scalar VX_API_CALL vxCreateScalar(vx_context context, vx_enum data_type, const void *ptr)
Interface to TI extension APIs.
void show_node_attributes(vx_node node)
Show attributes of previously created node.
#define GRAD_X_FILE_NAME
Gradient X file name.
int32_t vx_enum
VX_SUCCESS
vx_status VX_API_CALL vxReleaseScalar(vx_scalar *scalar)
void show_graph_attributes(vx_graph graph)
Show attributes of previously created graph.
vx_true_e
VX_DF_IMAGE_U8
vx_bool tivxIsTargetEnabled(const char target_name[])
Utility function to know if target is enabled or not.
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
vx_enum vx_status
struct _vx_context * vx_context
vx_status VX_API_CALL vxSetNodeTarget(vx_node node, vx_enum target_enum, const char *target_string)
vx_node VX_API_CALL vxSobel3x3Node(vx_graph graph, vx_image input, vx_image output_x, vx_image output_y)
struct _vx_reference * vx_reference
#define TIVX_TARGET_DSP2
Name for DSP target class, instance 2.
VX_TARGET_STRING
uint32_t vx_df_image
VX_IMAGE_WIDTH
vx_image VX_API_CALL vxCreateImage(vx_context context, vx_uint32 width, vx_uint32 height, vx_df_image color)
#define IN_FILE_NAME
Input file name.
vx_status VX_API_CALL vxWaitGraph(vx_graph graph)
vx_node VX_API_CALL vxConvertDepthNode(vx_graph graph, vx_image input, vx_image output, vx_enum policy, vx_scalar shift)
#define TIVX_TARGET_DSP1
Name for DSP target class, instance 1.
vx_status VX_API_CALL vxReleaseGraph(vx_graph *graph)
vx_status VX_API_CALL vxSetReferenceName(vx_reference ref, const vx_char *name)
vx_node VX_API_CALL vxMagnitudeNode(vx_graph graph, vx_image grad_x, vx_image grad_y, vx_image mag)
#define GRAD_Y_FILE_NAME
Gradient Y file name.
void show_image_attributes(vx_image image)
Show attributes of previously created image.
vx_image tivx_utils_create_vximage_from_bmpfile(vx_context context, const char *filename, vx_bool convert_to_gray_scale)
Create a image data object given BMP filename as input.
vx_node VX_API_CALL vxPhaseNode(vx_graph graph, vx_image grad_x, vx_image grad_y, vx_image orientation)
VX_DF_IMAGE_S16
uint32_t vx_uint32
vx_graph VX_API_CALL vxCreateGraph(vx_context context)
vx_status VX_API_CALL vxVerifyGraph(vx_graph graph)
vx_status VX_API_CALL vxScheduleGraph(vx_graph graph)
VX_CONVERT_POLICY_SATURATE
vx_context VX_API_CALL vxCreateContext()
#define MAGNITUDE_FILE_NAME
Magnitude file name.
struct _vx_graph * vx_graph
vx_status VX_API_CALL tivxExportGraphToDot(vx_graph graph, const char *output_file_path, const char *output_file_prefix)
Export graph representation as DOT graph file.
vx_status VX_API_CALL vxReleaseNode(vx_node *node)
vx_status tivx_utils_save_vximage_to_bmpfile(const char *filename, vx_image image)
Save data from image object to PNG file.
struct _vx_node * vx_node
VX_TYPE_INT32
vx_status VX_API_CALL vxReleaseImage(vx_image *image)
struct _vx_scalar * vx_scalar
#define PHASE_FILE_NAME
Phase file name.
void vx_tutorial_graph_image_gradients()
Tutorial Entry Point.
VX_IMAGE_HEIGHT