├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── book_samples ├── CMakeLists.txt ├── LICENSE ├── README ├── background │ ├── vx_bg.c │ ├── vx_bg_ex1.c │ └── vx_bg_ex2.c ├── birds-eye │ ├── CMakeLists.txt │ ├── birdsEyeView.c │ └── opencv-birdsEyeView.cpp ├── deploy │ ├── XML-example.c │ ├── export_graph.c │ ├── graphFactory.c │ ├── graphFactoryTest.c │ ├── processGraph.cpp │ └── process_graph.c ├── example1 │ ├── CMakeLists.txt │ └── example1.c ├── example2 │ ├── CMakeLists.txt │ └── example2.c ├── example3 │ ├── CMakeLists.txt │ └── example3.c ├── example4 │ ├── CMakeLists.txt │ ├── changeImage.c │ ├── example4.c │ └── example4a.c ├── filter │ ├── CMakeLists.txt │ ├── filterGaussImage.c │ ├── filterImage.c │ ├── filterImageROI.c │ └── filterImageROIvxu.c ├── hough │ ├── CMakeLists.txt │ ├── houghLines.c │ └── houghLinesEx.c ├── opencl_interop │ ├── CMakeLists.txt │ ├── common.h │ ├── my_vx_tensor_map_impl.cpp │ ├── my_vx_tensor_map_impl.h │ ├── opencl_interop_example.cpp │ └── opencl_kernel_example.cpp ├── ppm-io │ ├── readImage.c │ ├── readImage.h │ ├── writeImage.c │ └── writeImage.h ├── stitch │ ├── CMakeLists.txt │ ├── homography-multiband-opencv.cpp │ ├── homography-opencv.cpp │ ├── stitch-debug.c │ ├── stitch-multiband.c │ └── stitch.c ├── tracking │ ├── CMakeLists.txt │ ├── centroid_tracking.c │ ├── centroid_tracking.h │ └── tracking_example.cpp └── undistort │ ├── CMakeLists.txt │ ├── undistort-remap.c │ └── undistortOpenCV.cpp ├── scripts ├── clean-all.sh ├── download-amdovx.sh ├── download-khronos-sample.sh ├── download-videos.sh └── qt-clean.sh └── tutorial_exercises ├── CMakeLists.txt ├── cmake └── FindOpenCL.cmake ├── exercise1 ├── CMakeLists.txt └── exercise1.cpp ├── exercise2 ├── CMakeLists.txt └── exercise2.cpp ├── exercise3 ├── CMakeLists.txt └── exercise3.cpp ├── exercise4 ├── CMakeLists.txt └── exercise4.cpp ├── include ├── VX │ ├── vx.h │ ├── vx_api.h │ ├── vx_compatibility.h │ ├── vx_import.h │ ├── vx_kernels.h │ ├── vx_khr_class.h │ ├── vx_khr_icd.h │ ├── vx_khr_ix.h │ ├── vx_khr_nn.h │ ├── vx_khr_tiling.h │ ├── vx_khr_xml.h │ ├── vx_nodes.h │ ├── vx_types.h │ ├── vx_vendors.h │ └── vxu.h └── opencv_camera_display.h ├── solution_exercise1 ├── CMakeLists.txt └── solution_exercise1.cpp ├── solution_exercise2 ├── CMakeLists.txt └── solution_exercise2.cpp ├── solution_exercise3 ├── CMakeLists.txt └── solution_exercise3.cpp ├── solution_exercise4 ├── CMakeLists.txt └── solution_exercise4.cpp └── vgg16-nnef-openvx ├── README.md ├── tf2nnef.py └── vgg16_export.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Downloaded and build files 31 | tutorial_videos/ 32 | tutorial_exercises/amdovx-core/ 33 | tutorial_exercises/openvx_sample/ 34 | build-*/ 35 | tutorial_exercises/CMakeLists.txt.user 36 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tutorial_exercises/amdovx-modules"] 2 | path = tutorial_exercises/amdovx-modules 3 | url = https://github.com/rgiduthuri/amdovx-modules.git 4 | branch = last 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Radha Giduthuri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /book_samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # Global setup file for OpenVX CMake 3 | 4 | # 5 | cmake_minimum_required(VERSION 2.8.9) 6 | 7 | project(openvx-book-samples) 8 | find_package(OpenCV REQUIRED) 9 | if(${OpenCV_VERSION} VERSION_GREATER 4 OR ${OpenCV_VERSION} VERSION_EQUAL 4) 10 | message("OpenCV version 4.0 or higher detected") 11 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 12 | message("${OpenCV_LIBS}") 13 | endif() 14 | 15 | find_package(LAPACK) 16 | if(${LAPACK_FOUND}) 17 | message("LAPACK found:") 18 | message(${LAPACK_LIBRARIES}) 19 | else() 20 | message("LAPACK not found") 21 | endif() 22 | 23 | find_library(OPENVX openvx) 24 | message("OpenVX found: ${OPENVX}") 25 | find_library(VXU vxu) 26 | find_package(VXA QUIET) 27 | if(NOT ${VXA_FOUND}) 28 | message(FATAL_ERROR "The required VXA library is not found, please download it from https://github.com/relrotciv/vxa.") 29 | endif() 30 | 31 | 32 | include_directories($ENV{C_INCLUDE_PATH}) 33 | include_directories(ppm-io) 34 | link_directories(/usr/local/lib) 35 | 36 | add_subdirectory(example1) 37 | add_subdirectory(example2) 38 | add_subdirectory(example3) 39 | add_subdirectory(example4) 40 | add_subdirectory(filter) 41 | add_subdirectory(stitch) 42 | add_subdirectory(hough) 43 | add_subdirectory(birds-eye) 44 | add_subdirectory(undistort) 45 | add_subdirectory(tracking) 46 | add_subdirectory(opencl_interop) 47 | -------------------------------------------------------------------------------- /book_samples/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Frank Brill, Victor Erukhimov, Radha Giduthuri, Steve Ramm 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /book_samples/README: -------------------------------------------------------------------------------- 1 | This is a collection of code samples for using the OpenVX API. The samples are 2 | reviewed in detail by the OpenVX Programming Guide [1]. The data for running 3 | the code samples is available from 4 | https://www.dropbox.com/sh/urzzs64a85tqf3d/AADxdIEer_tjHFGzBifZWhsLa. 5 | 6 | Prerequisites 7 | - C and C++ compilers 8 | - CMake 2.8.9 or higher 9 | - OpenCV is required for building some of the samples (*.cpp) 10 | - vxa library is required for the most of the OpenVX samples. It can be 11 | downloaded from https://github.com/relrotciv/vxa. 12 | - OpenVX (the most of the samples have been tested against the OpenVX sample 13 | implementation that can be downloaded from 14 | https://github.com/KhronosGroup/OpenVX-sample-impl) 15 | - LAPACK is required for building the birdsEyeView.c sample 16 | 17 | BUILDING SAMPLES 18 | 19 | In order to build the samples for Linux please follow the following steps: 20 | $ mkdir build 21 | $ cd build 22 | $ cmake -G "Unix Makefiles" ../ 23 | $ make 24 | 25 | [1] Frank Brill, Victor Erukhimov, Radhakrishna Giduthuri, Steve Ramm, OpenVX programming guide, TBD 26 | -------------------------------------------------------------------------------- /book_samples/background/vx_bg_ex1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define PATH_MAX 4096 15 | 16 | char *viddir = "/mnt/c/Users/Frank/Documents/piper-video"; 17 | char *basefname = "piper01"; 18 | char filename[PATH_MAX]; 19 | 20 | int myCaptureImage(vx_context context, vx_image image, int framenum) { 21 | sprintf(filename, "%s/%s/pgm/%s %04d.pgm", viddir, basefname, basefname, framenum); 22 | if (framenum == 1) 23 | printf("Beginning processing %s/%s\n", viddir, basefname); 24 | return vxuFReadImage(context, filename, image); 25 | } 26 | 27 | int myDisplayImage(vx_context context, vx_image image, char *suffix, int framenum) { 28 | sprintf(filename, "%s/%s/out/%s_%s %04d.pgm", viddir, basefname, basefname, suffix, framenum); 29 | vxuFWriteImage(context, image, filename); 30 | } 31 | 32 | int main(int argc, char *argv[]) 33 | { 34 | vx_uint32 w_in = 1080, h_in = 1920; // index and input image size 35 | int scale = 4; // image size scale 36 | int w = w_in/scale; // scaled image width 37 | int h = h_in/scale; // scaled image height 38 | 39 | vx_uint8 threshval = 10; // basic difference threshold value 40 | if (argc > 1) threshval = atoi(argv[1]); 41 | printf("Threshold value is %d\n", threshval); 42 | 43 | vx_context context = vxCreateContext(); 44 | 45 | vxLoadKernels(context, "openvx-debug"); // For reading and writing images 46 | 47 | vx_graph graph = vxCreateGraph(context); 48 | 49 | vx_image input_image = vxCreateImage(context, w_in, h_in, VX_DF_IMAGE_U8); 50 | vx_image curr_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 51 | vx_image diff_image = vxCreateVirtualImage(graph, w, h, VX_DF_IMAGE_U8); 52 | vx_image bg_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 53 | vx_image fg_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 54 | 55 | vx_threshold threshold = vxCreateThresholdForImage(context, VX_THRESHOLD_TYPE_BINARY, 56 | VX_DF_IMAGE_U8, VX_DF_IMAGE_U8); 57 | vxCopyThresholdValue(threshold, (vx_pixel_value_t*)&threshval, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); 58 | 59 | vx_node scale_node = vxScaleImageNode(graph, input_image, curr_image, VX_INTERPOLATION_AREA); 60 | vx_node absdiff_node = vxAbsDiffNode(graph, bg_image, curr_image, diff_image); 61 | vx_node thresh_node = vxThresholdNode(graph, diff_image, threshold, fg_image); 62 | 63 | vxVerifyGraph(graph); 64 | 65 | int framenum = 1; 66 | while (myCaptureImage(context, input_image, framenum) == VX_SUCCESS) { 67 | 68 | // Initialize the background model 69 | if (framenum == 1) { 70 | vxuScaleImage(context, input_image, bg_image, VX_INTERPOLATION_AREA); 71 | } 72 | printf("Frame %d%c[1000D", framenum, 0x1b); 73 | fflush(stdout); 74 | 75 | vxProcessGraph(graph); 76 | 77 | myDisplayImage(context, fg_image, "fg", framenum); 78 | framenum++; 79 | } 80 | printf("Finished after %d frames\n", framenum-1); 81 | 82 | vxReleaseNode(&scale_node); 83 | vxReleaseNode(&absdiff_node); 84 | vxReleaseNode(&thresh_node); 85 | vxReleaseGraph(&graph); 86 | vxUnloadKernels(context, "openvx-debug"); 87 | vxReleaseContext(&context); 88 | 89 | } 90 | -------------------------------------------------------------------------------- /book_samples/background/vx_bg_ex2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #define PATH_MAX 4096 15 | 16 | char *viddir = "/mnt/c/Users/Frank/Documents/piper-video"; 17 | char *basefname = "piper01"; 18 | char filename[PATH_MAX]; 19 | 20 | int myCaptureImage(vx_context context, vx_image image, int framenum) { 21 | sprintf(filename, "%s/%s/pgm/%s %04d.pgm", viddir, basefname, basefname, framenum); 22 | if (framenum == 1) 23 | printf("Beginning processing %s/%s\n", viddir, basefname); 24 | return vxuFReadImage(context, filename, image); 25 | } 26 | 27 | int myDisplayImage(vx_context context, vx_image image, char *suffix, int framenum) { 28 | sprintf(filename, "%s/%s/out/%s_%s %04d.pgm", viddir, basefname, basefname, suffix, framenum); 29 | vxuFWriteImage(context, image, filename); 30 | } 31 | 32 | int main(int argc, char *argv[]) 33 | { 34 | vx_uint32 w_in = 1080, h_in = 1920; // index and input image size 35 | int scale = 4; // image size scale 36 | int w = w_in/scale; // scaled image width 37 | int h = h_in/scale; // scaled image height 38 | 39 | vx_uint8 threshval = 30; // basic difference threshold value 40 | if (argc > 1) threshval = atoi(argv[1]); 41 | printf("Threshold value is %d\n", threshval); 42 | 43 | vx_context context = vxCreateContext(); 44 | 45 | vxLoadKernels(context, "openvx-debug"); // For reading and writing images 46 | 47 | vx_graph graph = vxCreateGraph(context); 48 | 49 | vx_image input_image = vxCreateImage(context, w_in, h_in, VX_DF_IMAGE_U8); 50 | vx_image curr_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 51 | vx_image diff_image = vxCreateVirtualImage(graph, w, h, VX_DF_IMAGE_U8); 52 | vx_image bg_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 53 | vx_image fg_image = vxCreateVirtualImage(graph, w, h, VX_DF_IMAGE_U8); 54 | vx_image dilated_image = vxCreateVirtualImage(graph, w, h, VX_DF_IMAGE_U8); 55 | vx_image eroded_image = vxCreateImage(context, w, h, VX_DF_IMAGE_U8); 56 | 57 | vx_threshold threshold = vxCreateThresholdForImage(context, VX_THRESHOLD_TYPE_BINARY, 58 | VX_DF_IMAGE_U8, VX_DF_IMAGE_U8); 59 | vxCopyThresholdValue(threshold, (vx_pixel_value_t*)&threshval, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); 60 | 61 | vx_node scale_node = vxScaleImageNode(graph, input_image, curr_image, VX_INTERPOLATION_AREA); 62 | vx_node absdiff_node = vxAbsDiffNode(graph, bg_image, curr_image, diff_image); 63 | vx_node thresh_node = vxThresholdNode(graph, diff_image, threshold, fg_image); 64 | vx_node dilate_node = vxDilate3x3Node(graph, fg_image, dilated_image); 65 | vx_node erode_node = vxErode3x3Node(graph, dilated_image, eroded_image); 66 | 67 | vx_float32 alphaval = 0.3f; 68 | if (argc > 2) alphaval = atof(argv[2]); 69 | printf("Alpha blend value is %f\n", alphaval); 70 | vx_scalar alpha = vxCreateScalar(context, VX_TYPE_FLOAT32, &alphaval); 71 | vx_node accum_node = vxAccumulateWeightedImageNode(graph, curr_image, alpha, bg_image); 72 | 73 | vxVerifyGraph(graph); 74 | 75 | int framenum = 1; 76 | while (myCaptureImage(context, input_image, framenum) == VX_SUCCESS) { 77 | 78 | // Initialize the background model 79 | if (framenum == 1) { 80 | vxuScaleImage(context, input_image, bg_image, VX_INTERPOLATION_AREA); 81 | } 82 | printf("Frame %d%c[1000D", framenum, 0x1b); 83 | fflush(stdout); 84 | 85 | vxProcessGraph(graph); 86 | 87 | myDisplayImage(context, fg_image, "fg", framenum); 88 | myDisplayImage(context, dilated_image, "dil", framenum); 89 | myDisplayImage(context, eroded_image, "erod", framenum); 90 | framenum++; 91 | } 92 | printf("Finished after %d frames\n", framenum-1); 93 | 94 | vxReleaseImage(&input_image); 95 | vxReleaseImage(&curr_image); 96 | vxReleaseImage(&diff_image); 97 | vxReleaseImage(&bg_image); 98 | vxReleaseImage(&fg_image); 99 | vxReleaseImage(&dilated_image); 100 | vxReleaseImage(&eroded_image); 101 | vxReleaseScalar(&alpha); 102 | vxReleaseNode(&erode_node); 103 | vxReleaseNode(&dilate_node); 104 | vxReleaseNode(&accum_node); 105 | vxReleaseNode(&scale_node); 106 | vxReleaseNode(&absdiff_node); 107 | vxReleaseNode(&thresh_node); 108 | vxReleaseGraph(&graph); 109 | vxUnloadKernels(context, "openvx-debug"); 110 | vxReleaseContext(&context); 111 | 112 | } 113 | -------------------------------------------------------------------------------- /book_samples/birds-eye/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if(${LAPACK_FOUND}) 2 | add_executable(birdsEyeView birdsEyeView.c) 3 | target_link_libraries(birdsEyeView ${OpenCV_LIBS} vxa ${OPENVX} m ${LAPACK_LIBRARIES}) 4 | else() 5 | message("LAPACK is required for building birdsEyeView, skipping...") 6 | endif() 7 | 8 | add_executable(opencv-birdsEyeView opencv-birdsEyeView.cpp) 9 | target_link_libraries(opencv-birdsEyeView ${OpenCV_LIBS}) 10 | -------------------------------------------------------------------------------- /book_samples/birds-eye/opencv-birdsEyeView.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file opencv-birdsEyeView.cpp 26 | * \example opencv-birdsEyeView 27 | * \brief This sample implements the bird's eye view algorithm from 28 | * the OpenVX sample birdsEyeView.c using OpenCV. 29 | * \author Victor Erukhimov 30 | */ 31 | 32 | 33 | #include 34 | #include 35 | 36 | using namespace cv; 37 | 38 | int main(int argc, char** argv) 39 | { 40 | if(argc != 3) 41 | { 42 | printf("opencv-birdsEyeView \n"); 43 | exit(0); 44 | } 45 | 46 | Mat input = imread(argv[1]); 47 | Mat temp; 48 | 49 | Mat K = (Mat_(3,3) << 8.4026236186715255e+02, 0., 3.7724917600845038e+02, 50 | 0., 8.3752885759166338e+02, 4.6712164335800873e+02, 51 | 0., 0., 1.); 52 | K = K*4.0f; 53 | K.at(2, 2) = 1.0f; 54 | std::cout << "K = " << K << std::endl; 55 | std::cout << "Kinv = " << K.inv() << std::endl; 56 | 57 | Point3f p0(482.0f*4, 332.0f*4, 1.0f); 58 | Point3f pu = Mat(K.inv()*Mat(p0)).at(0); 59 | float phi = acos(-1) + atan(1.0f/pu.y); 60 | 61 | std::cout << "p0 = (" << p0 << "), pu = (" << pu << "), phi = " << phi << std::endl; 62 | 63 | // calculate homography, rotation around x axis to make the camera look down 64 | Mat H1 = Mat::zeros(3, 3, CV_32F); 65 | H1.at(0, 0) = 1.0f; 66 | H1.at(1, 1) = cos(phi); 67 | H1.at(1, 2) = sin(phi); 68 | H1.at(2, 1) = -sin(phi); 69 | H1.at(2, 2) = cos(phi); 70 | 71 | std::cout << "phi = " << phi << std::endl; 72 | std::cout << "H1 = " << H1 << std::endl; 73 | 74 | // now we need to adjust offset and scale to map input image to 75 | // visible coordinates in the output image. 76 | Mat H = K*H1*K.inv(); 77 | const Point3f p1(p0.x, p0.y*1.2, 1); 78 | const Point3f p2(p0.x, input.rows, 1); 79 | Point3f p1h = Mat(H*Mat(p1)).at(0, 0); 80 | p1h *= 1/p1h.z; 81 | Point3f p2h = Mat(H*Mat(p2)).at(0, 0); 82 | p2h *= 1/p2h.z; 83 | Mat scaleY = Mat::eye(3, 3, CV_32F); 84 | 85 | float scale = (p2h.y - p1h.y)/input.rows; 86 | scaleY.at(0, 2) = input.cols*scale/2 - p0.x; 87 | scaleY.at(1, 2) = -p1h.y; 88 | scaleY.at(2, 2) = scale; 89 | 90 | std::cout << "scaleY = " << scaleY << std::endl << std::endl; 91 | std::cout << "H = " << H << std::endl << std::endl; 92 | std::cout << "K*H1 = " << K*H1 << std::endl << std::endl; 93 | 94 | H = scaleY*H; 95 | 96 | std::cout << "scaleY*H = " << H << std::endl << std::endl; 97 | 98 | Point3f corners[]= {Point3f(0, p0.y*1.15, 1), Point3f(input.cols, p0.y*1.15, 1), 99 | Point3f(input.cols, input.rows, 1), Point3f(0, input.rows, 1), 100 | p0, Point3f(p0.x, p0.y*1.1, 1)}; 101 | 102 | for(int i = 0; i < sizeof(corners)/sizeof(Point3f); i++) 103 | { 104 | Point3f ph1 = Mat(K.inv()*Mat(corners[i])).at(0, 0); 105 | Point3f ph2 = Mat(H*Mat(corners[i])).at(0, 0); 106 | std::cout << "point " << i << " maps to: " << std::endl << 107 | " uni: (" << ph1.x/ph1.z << " " << ph1.y/ph1.z << ")" << std::endl << 108 | " output: (" << ph2.x/ph2.z << " " << ph2.y/ph2.z << ")" << std::endl; 109 | } 110 | 111 | Mat output; 112 | warpPerspective(input, output, H, input.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0)); 113 | imwrite(argv[2], output); 114 | 115 | return(0); 116 | } 117 | -------------------------------------------------------------------------------- /book_samples/deploy/XML-example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "readImage.h" 5 | #include "writeImage.h" 6 | #include 7 | 8 | vx_char xmlFilename[] = "ExampleXMLGraph.xml"; 9 | 10 | extern vx_graph makeTestGraph(vx_context context, vx_image image, vx_image output); 11 | 12 | vx_status createXMLGraph(vx_uint32 width, vx_uint32 height, vx_char xmlfile[]) 13 | { 14 | vx_context context = vxCreateContext(); 15 | return vxGetStatus((vx_reference)makeTestGraph(context, 16 | vxCreateImage(context, width, height, VX_DF_IMAGE_RGB), 17 | vxCreateImage(context, width, height, VX_DF_IMAGE_RGB))) || 18 | vxExportToXML(context, xmlfile) || 19 | vxReleaseContext(&context); 20 | } 21 | 22 | void main(int argc, char **argv) 23 | { 24 | if (argc != 3) 25 | printf("Change an image\n%s \n", argv[0]); 26 | /* We create the XML graph here but in practice it will be done by a different application */ 27 | /* Note also our example must specifiy the width and height up front, if the images are a different size 28 | then the graph will fail to verify later */ 29 | else if (createXMLGraph(640, 480, xmlFilename)) 30 | printf("Failed to export the context\n"); 31 | else 32 | { 33 | struct read_image_attributes attr; 34 | vx_context context = vxCreateContext(); 35 | vx_image image = createImageFromFile(context, argv[1], &attr); 36 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 37 | vx_import import = vxImportFromXML(context, xmlFilename); 38 | if (vxGetStatus((vx_reference)import)) 39 | printf("Failed to import the XML\n"); 40 | else 41 | { 42 | vx_graph graph = (vx_graph)vxGetImportReferenceByName(import, "Test Graph"); 43 | if (vxGetStatus((vx_reference)graph)) 44 | printf("Failed to find the test graph\n"); 45 | else if (vxSetGraphParameterByIndex(graph, 0, (vx_reference)image) || 46 | vxSetGraphParameterByIndex(graph, 1, (vx_reference)output)) 47 | printf("Error setting the graph parameters\n"); 48 | else if (vxProcessGraph(graph)) 49 | printf("Error processing the graph\n"); 50 | else if (writeImage(output, argv[2])) 51 | printf("Problem writing the output image\n"); 52 | } 53 | vxReleaseContext(&context); 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /book_samples/deploy/export_graph.c: -------------------------------------------------------------------------------- 1 | /* 2 | export_graph.c 3 | Create a graph and export it using the export and import extension 4 | The memory "blob" is written to a file so it may be later read and imported 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | extern vx_graph makeTestGraph(vx_context context, vx_image image, vx_image output); 12 | 13 | void main(int argc, char **argv) 14 | { 15 | vx_context context = vxCreateContext(); 16 | vx_image input = vxCreateImage(context, 640, 480, VX_DF_IMAGE_RGB); 17 | vx_image output = vxCreateImage(context, 640, 480, VX_DF_IMAGE_RGB); 18 | vx_graph graph = makeTestGraph(context, input, output); 19 | vx_reference refs[3] = { 20 | (vx_reference)graph, 21 | (vx_reference)input, 22 | (vx_reference)output 23 | }; 24 | vx_enum uses[3] = { 25 | VX_IX_USE_EXPORT_VALUES, 26 | VX_IX_USE_APPLICATION_CREATE, 27 | VX_IX_USE_APPLICATION_CREATE 28 | }; 29 | const vx_uint8 *blob = NULL; 30 | vx_size length; 31 | if (vxExportObjectsToMemory(context, 3, refs, uses, &blob, &length)) { 32 | /* There was an error creating the export, report to the user... */ 33 | printf("Got an error when exporting the graph. No file was written.\n"); 34 | } else if (argc != 2) { 35 | printf("Expected a valid filename: %s \n", argv[0]); 36 | } else { 37 | /* We have a valid export of length bytes at address blob. Do something with it like writing it 38 | to a file... */ 39 | FILE *fp = fopen(argv[1], "wb"); 40 | if (fp && (fwrite(blob, length, 1, fp) == 1) && (fclose(fp) == 0)) { 41 | printf("Wrote the exported graph to file '%s', total %zu bytes\n", argv[1], length); 42 | } else { 43 | fclose(fp); 44 | printf("Error opening, writing or closing the file '%s'\n", argv[1]); 45 | } 46 | } 47 | /* now release the export blob memory, now we have copied it somewhere */ 48 | vxReleaseExportedMemory(context, &blob); 49 | /* Release the context and all other resources */ 50 | vxReleaseContext(&context); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /book_samples/deploy/graphFactory.c: -------------------------------------------------------------------------------- 1 | /* 2 | graphFactory.c 3 | Create a test graph in the context 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | void releaseNode(vx_node node) 10 | { 11 | vxReleaseNode(&node); 12 | } 13 | 14 | vx_graph makeTestGraph(vx_context context, vx_image image, vx_image output) 15 | { 16 | /* creates a graph with one input image and one output image. 17 | The input and output images can be provided through the mechanism of graph paramters, 18 | it is assumed that the input and output images are RGB. 19 | Replace the default processing with what you like! 20 | */ 21 | enum { 22 | numvyuv = 2, /* Number of virtual YUV images we need */ 23 | numv16 = 3, /* Number of virtual S16 images we need */ 24 | numv8 = 8 /* Number of virtual U8 images we need */ 25 | }; 26 | vx_graph graph = vxCreateGraph(context); 27 | vx_image virtsyuv[numvyuv], virts8[numv8], virts16[numv16]; 28 | 29 | int i; 30 | 31 | for (i = 0; i < numvyuv; ++i) 32 | virtsyuv[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_NV12); 33 | for (i = 0; i < numv8; ++i) 34 | virts8[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 35 | for (i = 0; i < numv16; ++i) 36 | virts16[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_S16); 37 | 38 | /* Do some arbitrary processing on the imput image */ 39 | /* First, make a true greyscale image. We do this by converting to YUV 40 | and extracting the Y. */ 41 | vx_node node = vxColorConvertNode(graph, image, virtsyuv[0]); 42 | 43 | /* Get the parameter that will be the input and add it to the graph */ 44 | vx_parameter parameter = vxGetParameterByIndex(node, 0); 45 | vxReleaseNode(&node); 46 | vxAddParameterToGraph(graph, parameter); 47 | vxReleaseParameter(¶meter); 48 | 49 | /* Extract the Y */ 50 | releaseNode(vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_Y, virts8[0])); 51 | 52 | /* Use Sobel plus magnitude to find edges on the greyscale image */ 53 | releaseNode(vxSobel3x3Node(graph, virts8[0], virts16[0], virts16[1])); 54 | /* Note that we have to use specifically U8 and S16 images to satisfy the convert depth node */ 55 | releaseNode(vxMagnitudeNode(graph, virts16[0], virts16[1], virts16[2])); 56 | vx_int32 shift = 1; 57 | vx_scalar shift_scalar = vxCreateScalar(context, VX_TYPE_INT32, &shift); 58 | releaseNode(vxConvertDepthNode(graph, virts16[2], virts8[1], VX_CONVERT_POLICY_SATURATE, shift_scalar)); 59 | vxReleaseScalar(&shift_scalar); 60 | 61 | /* Make the edges wider, then black and AND the edges back with the Y value so as to super-impose a black background */ 62 | releaseNode(vxDilate3x3Node(graph, virts8[1], virts8[2])); 63 | releaseNode(vxDilate3x3Node(graph, virts8[2], virts8[3])); 64 | releaseNode(vxNotNode(graph, virts8[3], virts8[4])); 65 | releaseNode(vxAndNode(graph, virts8[0], virts8[4], virts8[5])); 66 | 67 | /* Get the U and V channels as well.. */ 68 | releaseNode(vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_U, virts8[6])); 69 | releaseNode(vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_V, virts8[7])); 70 | 71 | /* Combine the colour channels to give a YUV output image */ 72 | releaseNode(vxChannelCombineNode(graph, virts8[5], virts8[6], virts8[7], NULL, virtsyuv[1])); 73 | 74 | /* Convert the YUV to RGB output */ 75 | node = vxColorConvertNode(graph, virtsyuv[1], output); 76 | 77 | /* Now get the parameter that will be the output and add it to the graph */ 78 | parameter = vxGetParameterByIndex(node, 1); 79 | vxReleaseNode(&node); 80 | vxAddParameterToGraph(graph, parameter); 81 | vxReleaseParameter(¶meter); 82 | 83 | /* Give the graph a name */ 84 | vxSetReferenceName((vx_reference)graph, "Test Graph"); 85 | 86 | for (i =0; i < numv16; ++i) 87 | vxReleaseImage(&virts16[i]); 88 | for (i =0; i < numvyuv; ++i) 89 | vxReleaseImage(&virtsyuv[i]); 90 | for (i =0; i < numv8; ++i) 91 | vxReleaseImage(&virts8[i]); 92 | return graph; 93 | } 94 | -------------------------------------------------------------------------------- /book_samples/deploy/graphFactoryTest.c: -------------------------------------------------------------------------------- 1 | /* 2 | graphFactoryTest.c 3 | Read an image, change it, write it out. 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include "readImage.h" 9 | #include "writeImage.h" 10 | 11 | extern vx_graph makeTestGraph(vx_context context, vx_image image, vx_image output); 12 | 13 | void main(int argc, void **argv) 14 | { 15 | if (argc != 3) { 16 | printf("Change an image\n" 17 | "%s \n", (char *)argv[0]); 18 | } else { 19 | struct read_image_attributes attr; 20 | vx_context context = vxCreateContext(); 21 | vx_image image = createImageFromFile(context, (const char *)argv[1], &attr); 22 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 23 | vx_graph graph = makeTestGraph(context, image, output); 24 | if (vxGetStatus((vx_reference)image)) { 25 | printf("Could not create input image\n"); 26 | } else if (vxProcessGraph(graph)) { 27 | printf("Error processing graph\n"); 28 | } else if (writeImage(output, (const char *)argv[2])) { 29 | printf("Problem writing the output image\n"); 30 | } 31 | vxReleaseContext(&context); 32 | } 33 | } -------------------------------------------------------------------------------- /book_samples/deploy/processGraph.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | processGraph.cpp 3 | Read an image, change it using a saved graph, write it out. 4 | Using a C++ API 5 | */ 6 | #include "openvx_deploy.hpp" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "readImage.h" 17 | #include "writeImage.h" 18 | using namespace openvx; 19 | using namespace deployment; 20 | 21 | VxImport loadObjectsFromFile(VxContext context, VxRefArray & refs, const char * fname) 22 | { 23 | struct stat statbuf; 24 | auto statres { stat(fname, &statbuf) }; 25 | auto fp { fopen(fname, "rb") }; 26 | vx_uint8 blob[statbuf.st_size]; 27 | if (!fp || statres || (fread(blob, statbuf.st_size, 1, fp) != 1) ) 28 | std::cout << "Failed to read the file '" << fname << "'\n"; 29 | fclose(fp); 30 | return context.importObjectsFromMemory(refs, blob, statbuf.st_size); 31 | } 32 | 33 | int main(int argc, char **argv) 34 | { 35 | if (argc != 4) { 36 | std::cout << "Change an image using a saved graph\n" << argv[0] << 37 | " \n"; 38 | } else { 39 | struct read_image_attributes attr; 40 | VxContext context; 41 | VxImage input(createImageFromFile(context, argv[2], &attr)); 42 | auto output { context.createImage(attr.width, attr.height, attr.format) }; 43 | auto final_image { context.createImage(attr.width, attr.height, attr.format) }; 44 | std::cout << "Image Width = " << attr.width << ", height = " << attr.height << "\n"; 45 | VxRefArray refs(3); 46 | refs.put(1, input, VX_IX_USE_APPLICATION_CREATE); 47 | refs.put(2, output, VX_IX_USE_APPLICATION_CREATE); 48 | auto graph { loadObjectsFromFile(context, refs, argv[1]).getReferenceByName("Test Graph") }; 49 | if (input.getStatus() || output.getStatus() || final_image.getStatus()) { 50 | std::cout << "Could not create input or output images\n"; 51 | } else if (graph.getStatus()) { 52 | std::cout << "Problem with status of imported graph\n"; 53 | } else if (graph.processGraph()) { 54 | std::cout << "Error processing graph\n"; 55 | } else { 56 | std::cout << "Graph was processed OK, about to set parameters and process again\n"; 57 | if (VX_SUCCESS == graph.setGraphParameterByIndex(0, output) && 58 | VX_SUCCESS == graph.setGraphParameterByIndex(1, final_image) && 59 | VX_SUCCESS == graph.processGraph() ) { 60 | std::cout << "Once again, successful, writing output image\n"; 61 | if (writeImage(final_image, argv[3])) { 62 | std::cout << "Problem writing the output image\n"; 63 | } 64 | } else { 65 | std::cout << "Error setting parameters or processing graph\n"; 66 | } 67 | } 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /book_samples/deploy/process_graph.c: -------------------------------------------------------------------------------- 1 | /* 2 | processGraph.c 3 | Read an image, change it using a saved graph, write it out. 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "readImage.h" 12 | #include "writeImage.h" 13 | 14 | vx_import loadObjectsFromFile(vx_context context, vx_size num_refs, vx_reference *refs, vx_enum *uses, const char * fname) 15 | { 16 | struct stat statbuf; 17 | int statres = stat(fname, &statbuf); 18 | FILE *fp = fopen(fname, "rb"); 19 | vx_uint8 *blob = (vx_uint8 *)malloc(statbuf.st_size); 20 | vx_import import = NULL; 21 | if (fp && 0 == statres && blob) { 22 | if (fread(blob, statbuf.st_size, 1, fp) == 1) { 23 | printf("Read %zu bytes ok\n", (size_t)statbuf.st_size); 24 | import = vxImportObjectsFromMemory(context, num_refs, refs, uses, blob, statbuf.st_size); 25 | } else { 26 | printf("Failed to read the file '%s'\n", fname); 27 | } 28 | } else { 29 | printf("Problem opening '%s' for reading, or allocating %zu bytes of memory\n", 30 | fname, (size_t)statbuf.st_size); 31 | } 32 | fclose(fp); 33 | if (blob) { 34 | free(blob); 35 | } 36 | return import; 37 | } 38 | 39 | void main(int argc, void **argv) 40 | { 41 | if (argc != 4) { 42 | printf("Change an image using a saved graph\n" 43 | "%s \n", (char *)argv[0]); 44 | } else { 45 | struct read_image_attributes attr; 46 | vx_context context = vxCreateContext(); 47 | vx_image input = createImageFromFile(context, (const char *)argv[2], &attr); 48 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 49 | vx_image final = vxCreateImage(context, attr.width, attr.height, attr.format); 50 | printf("Image Width = %d, height = %d\n", attr.width, attr.height); 51 | enum {num_refs = 3}; 52 | vx_reference refs[num_refs] = { 53 | NULL, 54 | (vx_reference)input, 55 | (vx_reference)output 56 | }; 57 | vx_enum uses[num_refs] = { 58 | VX_IX_USE_EXPORT_VALUES, 59 | VX_IX_USE_APPLICATION_CREATE, 60 | VX_IX_USE_APPLICATION_CREATE 61 | }; 62 | vx_import import = loadObjectsFromFile(context, num_refs, refs, uses, (const char *)argv[1]); 63 | if (vxGetStatus((vx_reference)input) || vxGetStatus((vx_reference)output) || vxGetStatus((vx_reference)final)) { 64 | printf("Could not create input or output images\n"); 65 | } else if (vxGetStatus(refs[0])) { 66 | printf("Problem with status of imported graph\n"); 67 | } else { 68 | vx_graph graph = (vx_graph)refs[0]; 69 | if (VX_SUCCESS != vxProcessGraph(graph)) { 70 | printf("Error processing graph\n"); 71 | } else { 72 | printf("Graph was processed OK, about to set parameters and process again\n"); 73 | if (VX_SUCCESS == vxSetGraphParameterByIndex(graph, 0, (vx_reference)output) && 74 | VX_SUCCESS == vxSetGraphParameterByIndex(graph, 1, (vx_reference)final) && 75 | VX_SUCCESS == vxProcessGraph(graph) ) { 76 | printf("Once again, successful, writing output image\n"); 77 | if (writeImage(final, (const char *)argv[3])) { 78 | printf("Problem writing the output image\n"); 79 | } 80 | } else { 81 | printf("Error setting parameters or processing graph\n"); 82 | } 83 | } 84 | } 85 | vxReleaseImport(&import); 86 | vxReleaseContext(&context); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /book_samples/example1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(example1 example1.c) 2 | target_link_libraries(example1 ${VXU} ${OPENVX}) 3 | -------------------------------------------------------------------------------- /book_samples/example1/example1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Stephen Ramm 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file example1.c 26 | * \example example1 27 | * \brief Use the image creation functions to create a white rectangle on a black background 28 | * and count the corners in the result using the Fast Corners function. 29 | * \author Stephen Ramm 30 | */ 31 | 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | void errorCheck(vx_context *context_p, vx_status status, const char *message) 39 | { 40 | if (status) 41 | { 42 | puts("ERROR! "); 43 | puts(message); 44 | vxReleaseContext(context_p); 45 | exit(1); 46 | } 47 | } 48 | 49 | vx_image makeInputImage(vx_context context) 50 | { 51 | vx_image image = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 52 | vx_rectangle_t rect = { 53 | .start_x = 20, .start_y = 40, .end_x=80, .end_y = 60 54 | }; 55 | 56 | if (VX_SUCCESS == vxGetStatus((vx_reference)image)) 57 | { 58 | vx_image roi = vxCreateImageFromROI(image, &rect); 59 | vx_pixel_value_t pixel_white, pixel_black; 60 | pixel_white.U8 = 255; 61 | pixel_black.U8 = 0; 62 | if (VX_SUCCESS == vxGetStatus((vx_reference)roi) && 63 | VX_SUCCESS == vxSetImagePixelValues(image, &pixel_black) && 64 | VX_SUCCESS == vxSetImagePixelValues(roi, &pixel_white)) 65 | vxReleaseImage(&roi); 66 | else 67 | vxReleaseImage(&image); 68 | } 69 | 70 | return image; 71 | } 72 | 73 | int main(void) 74 | { 75 | vx_context context = vxCreateContext(); 76 | 77 | errorCheck(&context, vxGetStatus((vx_reference)context), "Could not create a vx_context\n"); 78 | 79 | vx_image image1 = makeInputImage(context); 80 | 81 | errorCheck(&context, vxGetStatus((vx_reference)image1), "Could not create image"); 82 | 83 | vx_float32 strength_thresh_value = 128.0; 84 | vx_scalar strength_thresh = vxCreateScalar(context, VX_TYPE_FLOAT32, &strength_thresh_value); 85 | vx_array corners = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 86 | vx_array corners1 = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 87 | vx_size num_corners_value = 0; 88 | vx_scalar num_corners = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 89 | vx_scalar num_corners1 = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 90 | vx_keypoint_t *kp = calloc( 100, sizeof(vx_keypoint_t)); 91 | 92 | errorCheck(&context, 93 | kp == NULL || 94 | vxGetStatus((vx_reference)strength_thresh) || 95 | vxGetStatus((vx_reference)corners) || 96 | vxGetStatus((vx_reference)num_corners) || 97 | vxGetStatus((vx_reference)corners1) || 98 | vxGetStatus((vx_reference)num_corners1), 99 | "Could not create parameters for FastCorners"); 100 | 101 | errorCheck(&context, vxuFastCorners(context, image1, strength_thresh, vx_true_e, corners, num_corners), "Fast Corners function failed"); 102 | errorCheck(&context, vxuFastCorners(context, image1, strength_thresh, vx_false_e, corners1, num_corners1), "Fast Corners function failed"); 103 | 104 | errorCheck(&context, vxCopyScalar(num_corners, &num_corners_value, VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyScalar failed"); 105 | printf("Found %zu corners with non-max suppression\n", num_corners_value); 106 | 107 | errorCheck(&context, vxCopyArrayRange( corners, 0, num_corners_value, sizeof(vx_keypoint_t), kp, 108 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyArrayRange failed"); 109 | for (int i=0; i 32 | */ 33 | 34 | 35 | #include 36 | #include 37 | #include "VX/vx.h" 38 | #include "VX/vxu.h" 39 | 40 | void errorCheck(vx_context *context_p, vx_status status, const char *message) 41 | { 42 | if (status) 43 | { 44 | puts("ERROR! "); 45 | puts(message); 46 | vxReleaseContext(context_p); 47 | exit(1); 48 | } 49 | } 50 | 51 | vx_image makeInputImage(vx_context context) 52 | { 53 | vx_image image = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 54 | vx_rectangle_t rect = { 55 | .start_x = 20, .start_y = 40, .end_x=80, .end_y = 60 56 | }; 57 | 58 | if (VX_SUCCESS == vxGetStatus((vx_reference)image)) 59 | { 60 | vx_image roi = vxCreateImageFromROI(image, &rect); 61 | vx_pixel_value_t pixel_white, pixel_black; 62 | pixel_white.U8 = 255; 63 | pixel_black.U8 = 0; 64 | if (VX_SUCCESS == vxGetStatus((vx_reference)roi) && 65 | VX_SUCCESS == vxSetImagePixelValues(image, &pixel_black) && 66 | VX_SUCCESS == vxSetImagePixelValues(roi, &pixel_white)) 67 | vxReleaseImage(&roi); 68 | else 69 | vxReleaseImage(&image); 70 | } 71 | 72 | return image; 73 | } 74 | 75 | int main(void) 76 | { 77 | vx_context context = vxCreateContext(); 78 | 79 | errorCheck(&context, vxGetStatus((vx_reference)context), "Could not create a vx_context\n"); 80 | 81 | vx_image image1 = makeInputImage(context); 82 | 83 | errorCheck(&context, vxGetStatus((vx_reference)image1), "Could not create first image"); 84 | 85 | vx_image image2 = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 86 | vx_image image3 = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 87 | vx_matrix warp_matrix = vxCreateMatrix(context, VX_TYPE_FLOAT32, 2U, 3U); 88 | vx_float32 matrix_values[3][2] = { /* Rotate through 90 degrees */ 89 | {0.0, 1.0}, /* x coefficients */ 90 | {1.0, 0.0}, /* y coefficients */ 91 | {0.0, 0.0} /* offsets */ 92 | }; 93 | vx_float32 strength_thresh_value = 128.0; 94 | vx_scalar strength_thresh = vxCreateScalar(context, VX_TYPE_FLOAT32, &strength_thresh_value); 95 | vx_array corners = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 96 | vx_size num_corners_value = 0; 97 | vx_scalar num_corners = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 98 | vx_keypoint_t *kp = calloc( 100, sizeof(vx_keypoint_t)); 99 | 100 | errorCheck(&context, 101 | kp == NULL || 102 | vxGetStatus((vx_reference)strength_thresh) || 103 | vxGetStatus((vx_reference)corners) || 104 | vxGetStatus((vx_reference)num_corners) || 105 | vxGetStatus((vx_reference)image2) || 106 | vxGetStatus((vx_reference)image3) || 107 | vxGetStatus((vx_reference)warp_matrix), 108 | "Could not create objects"); 109 | 110 | errorCheck(&context, vxCopyMatrix(warp_matrix, matrix_values, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST), "Could not initialise the matrix"); 111 | 112 | errorCheck(&context, /* Now image2 set to image 1 rotated */ 113 | vxuWarpAffine(context, image1, warp_matrix, VX_INTERPOLATION_NEAREST_NEIGHBOR, image2) || 114 | /* image3 set to logical OR of images 1 and 2 */ 115 | vxuOr(context, image1, image2, image3) || 116 | /*And now count the corners */ 117 | vxuFastCorners(context, image3, strength_thresh, vx_true_e, corners, num_corners), 118 | "Image functions failed"); 119 | 120 | errorCheck(&context, vxCopyScalar(num_corners, &num_corners_value, VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyScalar failed"); 121 | printf("Found %zu corners with non-max suppression\n", num_corners_value); 122 | 123 | errorCheck(&context, vxCopyArrayRange( corners, 0, num_corners_value, sizeof(vx_keypoint_t), kp, 124 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyArrayRange failed"); 125 | for (int i=0; i 35 | */ 36 | 37 | #include 38 | #include 39 | #include "VX/vx.h" 40 | #include "VX/vxu.h" 41 | 42 | void errorCheck(vx_context *context_p, vx_status status, const char *message) 43 | { 44 | if (status) 45 | { 46 | puts("ERROR! "); 47 | puts(message); 48 | vxReleaseContext(context_p); 49 | exit(1); 50 | } 51 | } 52 | 53 | vx_image makeInputImage(vx_context context) 54 | { 55 | vx_image image = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 56 | vx_rectangle_t rect = { 57 | .start_x = 20, .start_y = 40, .end_x=80, .end_y = 60 58 | }; 59 | 60 | if (VX_SUCCESS == vxGetStatus((vx_reference)image)) 61 | { 62 | vx_image roi = vxCreateImageFromROI(image, &rect); 63 | vx_pixel_value_t pixel_white, pixel_black; 64 | pixel_white.U8 = 255; 65 | pixel_black.U8 = 0; 66 | if (VX_SUCCESS == vxGetStatus((vx_reference)roi) && 67 | VX_SUCCESS == vxSetImagePixelValues(image, &pixel_black) && 68 | VX_SUCCESS == vxSetImagePixelValues(roi, &pixel_white)) 69 | vxReleaseImage(&roi); 70 | else 71 | vxReleaseImage(&image); 72 | } 73 | 74 | return image; 75 | } 76 | 77 | int main(void) 78 | { 79 | vx_context context = vxCreateContext(); 80 | 81 | errorCheck(&context, vxGetStatus((vx_reference)context), "Could not create a vx_context\n"); 82 | 83 | vx_image image1 = makeInputImage(context); 84 | 85 | errorCheck(&context, vxGetStatus((vx_reference)image1), "Could not create first image"); 86 | 87 | vx_image image2 = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 88 | vx_image image3 = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 89 | vx_image grad_x = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_S16); 90 | vx_image grad_y = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_S16); 91 | vx_image magnitude = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_S16); 92 | vx_image converted = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 93 | vx_image dilated = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 94 | vx_image image4 = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 95 | vx_matrix warp_matrix = vxCreateMatrix(context, VX_TYPE_FLOAT32, 2U, 3U); 96 | vx_float32 matrix_values[3][2] = { /* Rotate through 90 degrees */ 97 | {0.0, 1.0}, /* x coefficients */ 98 | {1.0, 0.0}, /* y coefficients */ 99 | {0.0, 0.0} /* offsets */ 100 | }; 101 | vx_float32 strength_thresh_value = 128.0; 102 | vx_scalar strength_thresh = vxCreateScalar(context, VX_TYPE_FLOAT32, &strength_thresh_value); 103 | vx_array corners = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 104 | vx_size num_corners_value = 0; 105 | vx_scalar num_corners = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 106 | vx_keypoint_t *kp = calloc( 100, sizeof(vx_keypoint_t)); 107 | 108 | errorCheck(&context, 109 | kp == NULL || 110 | vxGetStatus((vx_reference)strength_thresh) || 111 | vxGetStatus((vx_reference)corners) || 112 | vxGetStatus((vx_reference)num_corners) || 113 | vxGetStatus((vx_reference)image2) || 114 | vxGetStatus((vx_reference)image3) || 115 | vxGetStatus((vx_reference)grad_x) || 116 | vxGetStatus((vx_reference)grad_y) || 117 | vxGetStatus((vx_reference)magnitude) || 118 | vxGetStatus((vx_reference)converted) || 119 | vxGetStatus((vx_reference)dilated) || 120 | vxGetStatus((vx_reference)image4) || 121 | vxGetStatus((vx_reference)warp_matrix), 122 | "Could not create objects"); 123 | 124 | errorCheck(&context, vxCopyMatrix(warp_matrix, matrix_values, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST), "Could not initialise the matrix"); 125 | 126 | /* Now image2 set to image 1 rotated */ 127 | vx_status status = vxuWarpAffine(context, image1, warp_matrix, VX_INTERPOLATION_NEAREST_NEIGHBOR, image2); 128 | /* image3 set to logical OR of images 1 and 2 and then processed as described above */ 129 | errorCheck(&context, vxuOr(context, image1, image2, image3) || 130 | vxuSobel3x3(context, image3, grad_x, grad_y) || 131 | vxuMagnitude(context, grad_x, grad_y, magnitude) || 132 | vxuConvertDepth(context, magnitude, converted, VX_CONVERT_POLICY_SATURATE, 1) || 133 | vxuDilate3x3(context, converted, dilated) || 134 | /* And now count the corners */ 135 | vxuFastCorners(context, dilated, strength_thresh, vx_true_e, corners, num_corners), 136 | "Image functions failed"); 137 | 138 | errorCheck(&context, vxCopyScalar(num_corners, &num_corners_value, VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyScalar failed"); 139 | printf("Found %zu corners with non-max suppression\n", num_corners_value); 140 | 141 | errorCheck(&context, vxCopyArrayRange( corners, 0, num_corners_value, sizeof(vx_keypoint_t), kp, 142 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyArrayRange failed"); 143 | for (int i=0; i 6 | #include 7 | #include 8 | #include "readImage.h" 9 | #include "writeImage.h" 10 | 11 | vx_graph makeTestGraph(vx_context context, vx_image image, vx_image output) 12 | { 13 | /* creates a graph with one input image and one output image. 14 | You supply the input and output images, it is assumed that the input and output images are RGB. 15 | Replace the default processing with what you like! 16 | */ 17 | #define numv8 (6) /* Number of virtual U8 images we need */ 18 | #define numvyuv (2) /* Number of virtual YUV4 images we need */ 19 | vx_graph graph = vxCreateGraph(context); 20 | vx_image virts8[numv8], virtsyuv[numvyuv]; 21 | vx_threshold hyst = vxCreateThresholdForImage(context, VX_THRESHOLD_TYPE_RANGE, VX_DF_IMAGE_U8, VX_DF_IMAGE_U8); 22 | vx_pixel_value_t lower_value = {.U8 = 220}; 23 | vx_pixel_value_t upper_value = {.U8 = 230}; 24 | 25 | vxCopyThresholdRange(hyst, &lower_value, &upper_value, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); 26 | 27 | int i; 28 | 29 | for (i = 0; i < numv8; ++i) 30 | virts8[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 31 | for (i = 0; i < numvyuv; ++i) 32 | virtsyuv[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_IYUV); 33 | 34 | /* Do some arbitrary processing on the imput image */ 35 | /* First, make a true greyscale image. We do this by converting to YUV 36 | and extracting the Y. */ 37 | vxColorConvertNode(graph, image, virtsyuv[0]); 38 | vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_Y, virts8[0]); 39 | 40 | /* Use a Canny detector on the greyscale image to find edges */ 41 | vxCannyEdgeDetectorNode(graph, virts8[0], hyst, 5, VX_NORM_L1, virts8[1]); 42 | 43 | /* Make the edges black and AND the edges back with the Y value so as to super-impose a black background */ 44 | vxNotNode(graph, virts8[1], virts8[2]); 45 | vxAndNode(graph, virts8[0], virts8[2], virts8[3]); 46 | 47 | /* Get the U and V channels as well.. */ 48 | vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_U, virts8[4]); 49 | vxChannelExtractNode(graph, virtsyuv[0], VX_CHANNEL_V, virts8[5]); 50 | 51 | /* Combine the colour channels to give a YUV output image */ 52 | vxChannelCombineNode(graph, virts8[3], virts8[4], virts8[5], NULL, virtsyuv[1]); 53 | 54 | /* Convert the YUV to RGB output */ 55 | vxColorConvertNode(graph, virtsyuv[1], output); 56 | 57 | for (i =0; i < numv8; ++i) 58 | vxReleaseImage(&virts8[i]); 59 | for (i =0; i < numvyuv; ++i) 60 | vxReleaseImage(&virtsyuv[i]); 61 | return graph; 62 | } 63 | 64 | int main(int argc, char **argv) 65 | { 66 | if (argc != 3) 67 | { 68 | printf("Change an image\n" 69 | "%s \n", (char *)argv[0]); 70 | } 71 | else 72 | { 73 | struct read_image_attributes attr; 74 | vx_context context = vxCreateContext(); 75 | vx_image image = createImageFromFile(context, (const char *)argv[1], &attr); 76 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 77 | vx_graph graph = makeTestGraph(context, image, output); 78 | if (vxGetStatus((vx_reference)image)) 79 | printf("Could not create input image\n"); 80 | else if (vxProcessGraph(graph)) 81 | printf("Error processing graph\n"); 82 | else if (writeImage(output, (const char *)argv[2])) 83 | printf("Problem writing the output image\n"); 84 | vxReleaseContext(&context); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /book_samples/example4/example4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Stephen Ramm 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file example4.c 26 | * \example example4 27 | * \brief In this example we re-imnplement example three, using graph mode. 28 | * \author Stephen Ramm 29 | */ 30 | 31 | #include 32 | #include 33 | #include "VX/vx.h" 34 | 35 | void errorCheck(vx_context *context_p, vx_status status, const char *message) 36 | { 37 | if (status) 38 | { 39 | puts("ERROR! "); 40 | puts(message); 41 | vxReleaseContext(context_p); 42 | exit(1); 43 | } 44 | } 45 | 46 | vx_image makeInputImage(vx_context context, vx_uint32 width, vx_uint32 height) 47 | { 48 | vx_image image = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 49 | if (width > 48) 50 | width = 48; 51 | if (height > 48) 52 | height = 48; 53 | vx_rectangle_t rect = { 54 | .start_x = 50 - width, .start_y = 50 - height, .end_x = 50 + width, .end_y = 50 + height 55 | }; 56 | 57 | if (VX_SUCCESS == vxGetStatus((vx_reference)image)) 58 | { 59 | vx_image roi = vxCreateImageFromROI(image, &rect); 60 | vx_pixel_value_t pixel_white, pixel_black; 61 | pixel_white.U8 = 255; 62 | pixel_black.U8 = 0; 63 | if (VX_SUCCESS == vxGetStatus((vx_reference)roi) && 64 | VX_SUCCESS == vxSetImagePixelValues(image, &pixel_black) && 65 | VX_SUCCESS == vxSetImagePixelValues(roi, &pixel_white)) 66 | vxReleaseImage(&roi); 67 | else 68 | vxReleaseImage(&image); 69 | } 70 | return image; 71 | } 72 | 73 | vx_graph makeTestGraph(vx_context context) 74 | { 75 | vx_graph graph = vxCreateGraph(context); 76 | int i; 77 | vx_image imagesU8[5], imagesS16[3]; 78 | vx_image input = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 79 | 80 | for (i = 0; i < 5; ++i) 81 | imagesU8[i] = vxCreateVirtualImage(graph, 100, 100, VX_DF_IMAGE_U8); 82 | for (i = 0; i < 3; ++i) 83 | imagesS16[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_VIRT); 84 | 85 | vx_matrix warp_matrix = vxCreateMatrix(context, VX_TYPE_FLOAT32, 2U, 3U); 86 | vx_float32 matrix_values[6] = {0.0, 1.0, 1.0, 0.0, 0.0, 0.0 }; /* Rotate through 90 degrees */ 87 | vx_float32 strength_thresh_value = 128.0; 88 | vx_scalar strength_thresh = vxCreateScalar(context, VX_TYPE_FLOAT32, &strength_thresh_value); 89 | vx_array corners = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 90 | vx_size num_corners_value = 0; 91 | vx_int32 shift_value = 1; 92 | vx_scalar num_corners = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 93 | vx_scalar shift = vxCreateScalar(context, VX_TYPE_INT32, &shift_value); 94 | 95 | vxCopyMatrix(warp_matrix, matrix_values, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); 96 | 97 | /* Create the nodes to do the processing, order of creation is not important */ 98 | vx_node last_node = vxFastCornersNode(graph, imagesU8[4], strength_thresh, vx_true_e, corners, num_corners); 99 | vxDilate3x3Node(graph, imagesU8[3], imagesU8[4]); 100 | vxConvertDepthNode(graph, imagesS16[2], imagesU8[3], VX_CONVERT_POLICY_SATURATE, shift); 101 | vxMagnitudeNode(graph, imagesS16[0], imagesS16[1], imagesS16[2]); 102 | vxSobel3x3Node(graph, imagesU8[2], imagesS16[0], imagesS16[1]); 103 | vxOrNode(graph, imagesU8[0], imagesU8[1], imagesU8[2]); 104 | vxWarpAffineNode(graph, imagesU8[0], warp_matrix, VX_INTERPOLATION_NEAREST_NEIGHBOR, imagesU8[1]); 105 | 106 | /* Setup input parameter using a Copy node */ 107 | vxAddParameterToGraph(graph, vxGetParameterByIndex(vxCopyNode(graph, (vx_reference)input, (vx_reference)imagesU8[0]), 0)); 108 | 109 | /* Setup the output parameters from the last node */ 110 | vxAddParameterToGraph(graph, vxGetParameterByIndex(last_node, 3)); /* array of corners */ 111 | vxAddParameterToGraph(graph, vxGetParameterByIndex(last_node, 4)); /* number of corners */ 112 | 113 | /* Release resources */ 114 | vxReleaseImage(&input); 115 | for (i = 0; i < 5; ++i) 116 | vxReleaseImage(&imagesU8[i]); 117 | for (i = 0; i < 3; ++i) 118 | vxReleaseImage(&imagesS16[i]); 119 | vxReleaseMatrix(&warp_matrix); 120 | vxReleaseScalar(&strength_thresh); 121 | vxReleaseScalar(&num_corners); 122 | vxReleaseScalar(&shift); 123 | vxReleaseArray(&corners); 124 | 125 | return graph; 126 | } 127 | 128 | vx_reference getGraphParameter(vx_graph graph, vx_uint32 index) 129 | { 130 | vx_parameter p = vxGetGraphParameterByIndex(graph, index); 131 | vx_reference ref = NULL; 132 | vxQueryParameter(p, VX_PARAMETER_REF, &ref, sizeof(ref)); 133 | vxReleaseParameter(&p); 134 | return ref; 135 | } 136 | 137 | void showResults(vx_graph graph, vx_image image, const char * message) 138 | { 139 | vx_context context = vxGetContext((vx_reference)graph); 140 | puts(message); 141 | vxSetGraphParameterByIndex(graph, 0, (vx_reference)image); 142 | if (VX_SUCCESS == vxProcessGraph(graph)) 143 | { 144 | vx_size num_corners_value = 0; 145 | vx_keypoint_t *kp = calloc( 100, sizeof(vx_keypoint_t)); 146 | errorCheck(&context, vxCopyScalar((vx_scalar)getGraphParameter(graph, 2), &num_corners_value, 147 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyScalar failed"); 148 | printf("Found %zu corners with non-max suppression\n", num_corners_value); 149 | 150 | /* Array can only hold 100 values */ 151 | if (num_corners_value > 100) 152 | num_corners_value = 100; 153 | 154 | errorCheck(&context, vxCopyArrayRange((vx_array)getGraphParameter(graph, 1), 0, 155 | num_corners_value, sizeof(vx_keypoint_t), kp, 156 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyArrayRange failed"); 157 | for (int i=0; i 29 | */ 30 | 31 | 32 | #include 33 | #include 34 | #include "VX/vx.h" 35 | #include "writeImage.h" 36 | 37 | void errorCheck(vx_context *context_p, vx_status status, const char *message) 38 | { 39 | if (status) 40 | { 41 | puts("ERROR! "); 42 | puts(message); 43 | vxReleaseContext(context_p); 44 | exit(1); 45 | } 46 | } 47 | 48 | vx_image makeInputImage(vx_context context, vx_uint32 width, vx_uint32 height) 49 | { 50 | vx_image image = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 51 | if (width > 48) 52 | width = 48; 53 | if (height > 48) 54 | height = 48; 55 | vx_rectangle_t rect = { 56 | .start_x = 50 - width, .start_y = 50 - height, .end_x = 50 + width, .end_y = 50 + height 57 | }; 58 | 59 | if (VX_SUCCESS == vxGetStatus((vx_reference)image)) 60 | { 61 | vx_image roi = vxCreateImageFromROI(image, &rect); 62 | vx_pixel_value_t pixel_white, pixel_black; 63 | pixel_white.U8 = 255; 64 | pixel_black.U8 = 0; 65 | if (VX_SUCCESS == vxGetStatus((vx_reference)roi) && 66 | VX_SUCCESS == vxSetImagePixelValues(image, &pixel_black) && 67 | VX_SUCCESS == vxSetImagePixelValues(roi, &pixel_white)) 68 | vxReleaseImage(&roi); 69 | else 70 | vxReleaseImage(&image); 71 | } 72 | return image; 73 | } 74 | 75 | vx_graph makeTestGraph(vx_context context) 76 | { 77 | vx_graph graph = vxCreateGraph(context); 78 | int i; 79 | vx_image imagesU8[5], imagesS16[3]; 80 | vx_image input = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 81 | 82 | for (i = 0; i < 5; ++i) 83 | imagesU8[i] = vxCreateVirtualImage(graph, 100, 100, VX_DF_IMAGE_U8); 84 | for (i = 0; i < 3; ++i) 85 | imagesS16[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_VIRT); 86 | 87 | vx_matrix warp_matrix = vxCreateMatrix(context, VX_TYPE_FLOAT32, 2U, 3U); 88 | vx_float32 matrix_values[6] = {0.0, 1.0, 1.0, 0.0, 0.0, 0.0 }; /* Rotate through 90 degrees */ 89 | vx_float32 strength_thresh_value = 128.0; 90 | vx_scalar strength_thresh = vxCreateScalar(context, VX_TYPE_FLOAT32, &strength_thresh_value); 91 | vx_array corners = vxCreateArray(context, VX_TYPE_KEYPOINT, 100); 92 | vx_size num_corners_value = 0; 93 | vx_int32 shift_value = 1; 94 | vx_scalar num_corners = vxCreateScalar(context, VX_TYPE_SIZE, &num_corners_value); 95 | vx_scalar shift = vxCreateScalar(context, VX_TYPE_INT32, &shift_value); 96 | 97 | vxCopyMatrix(warp_matrix, matrix_values, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST); 98 | 99 | /* Create the nodes to do the processing, order of creation is not important */ 100 | vx_node last_node = vxFastCornersNode(graph, imagesU8[4], strength_thresh, vx_true_e, corners, num_corners); 101 | vxDilate3x3Node(graph, imagesU8[3], imagesU8[4]); 102 | vxConvertDepthNode(graph, imagesS16[2], imagesU8[3], VX_CONVERT_POLICY_SATURATE, shift); 103 | vxMagnitudeNode(graph, imagesS16[0], imagesS16[1], imagesS16[2]); 104 | vxSobel3x3Node(graph, imagesU8[2], imagesS16[0], imagesS16[1]); 105 | vxOrNode(graph, imagesU8[0], imagesU8[1], imagesU8[2]); 106 | vxWarpAffineNode(graph, imagesU8[0], warp_matrix, VX_INTERPOLATION_NEAREST_NEIGHBOR, imagesU8[1]); 107 | 108 | /* Setup input parameter using a Copy node */ 109 | vxAddParameterToGraph(graph, vxGetParameterByIndex(vxCopyNode(graph, (vx_reference)input, (vx_reference)imagesU8[0]), 0)); 110 | 111 | /* Setup the output parameters from the last node */ 112 | vxAddParameterToGraph(graph, vxGetParameterByIndex(last_node, 3)); /* array of corners */ 113 | vxAddParameterToGraph(graph, vxGetParameterByIndex(last_node, 4)); /* number of corners */ 114 | 115 | /* Add another output parameter to the graph */ 116 | vx_image output = vxCreateImage(context, 100U, 100U, VX_DF_IMAGE_U8); 117 | vxAddParameterToGraph(graph, vxGetParameterByIndex(vxCopyNode(graph, (vx_reference)imagesU8[4], (vx_reference)output), 1)); 118 | 119 | /* Release resources */ 120 | vxReleaseImage(&input); 121 | vxReleaseImage(&output); 122 | for (i = 0; i < 5; ++i) 123 | vxReleaseImage(&imagesU8[i]); 124 | for (i = 0; i < 3; ++i) 125 | vxReleaseImage(&imagesS16[i]); 126 | vxReleaseMatrix(&warp_matrix); 127 | vxReleaseScalar(&strength_thresh); 128 | vxReleaseScalar(&num_corners); 129 | vxReleaseScalar(&shift); 130 | vxReleaseArray(&corners); 131 | 132 | return graph; 133 | } 134 | 135 | vx_reference getGraphParameter(vx_graph graph, vx_uint32 index) 136 | { 137 | vx_parameter p = vxGetGraphParameterByIndex(graph, index); 138 | vx_reference ref = NULL; 139 | vxQueryParameter(p, VX_PARAMETER_REF, &ref, sizeof(ref)); 140 | vxReleaseParameter(&p); 141 | return ref; 142 | } 143 | 144 | void showResults(vx_graph graph, vx_image image, const char * message) 145 | { 146 | vx_context context = vxGetContext((vx_reference)graph); 147 | puts(message); 148 | vxSetGraphParameterByIndex(graph, 0, (vx_reference)image); 149 | if (VX_SUCCESS == vxProcessGraph(graph)) 150 | { 151 | vx_size num_corners_value = 0; 152 | vx_keypoint_t *kp = calloc( 100, sizeof(vx_keypoint_t)); 153 | errorCheck(&context, vxCopyScalar((vx_scalar)getGraphParameter(graph, 2), &num_corners_value, 154 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyScalar failed"); 155 | printf("Found %zu corners with non-max suppression\n", num_corners_value); 156 | 157 | /* Array can only hold 100 values */ 158 | if (num_corners_value > 100) 159 | num_corners_value = 100; 160 | 161 | errorCheck(&context, vxCopyArrayRange((vx_array)getGraphParameter(graph, 1), 0, 162 | num_corners_value, sizeof(vx_keypoint_t), kp, 163 | VX_READ_ONLY, VX_MEMORY_TYPE_HOST), "vxCopyArrayRange failed"); 164 | for (int i=0; i 6 | #include 7 | #include 8 | #include "readImage.h" 9 | #include "writeImage.h" 10 | 11 | vx_graph makeFilterGraph(vx_context context, vx_image input, vx_image output) 12 | { 13 | /* creates a graph with one input image and one output image. 14 | You supply the input and output images, it is assumed that the input and output images are RGB. 15 | Replace the default processing with what you like! 16 | */ 17 | #define numv8 (18) /* Number of virtual U8 images we need */ 18 | vx_graph graph = vxCreateGraph(context); 19 | vx_image virtu8[numv8]; 20 | int i; 21 | 22 | for (i = 0; i < numv8; ++i) 23 | virtu8[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 24 | 25 | /* Do Gaussian processing on the input image */ 26 | /* First, extract R, G, and B channels to individual virtual images */ 27 | vxChannelExtractNode(graph, input, VX_CHANNEL_R, virtu8[0]); 28 | vxChannelExtractNode(graph, input, VX_CHANNEL_G, virtu8[1]); 29 | vxChannelExtractNode(graph, input, VX_CHANNEL_B, virtu8[2]); 30 | 31 | /* Now, run Gaussian filter on each of gray scale images */ 32 | for(i = 0; i < numv8 - 3; i++) 33 | vxGaussian3x3Node(graph, virtu8[i], virtu8[i + 3]); 34 | 35 | /* Now combine images together in the ouptut color image */ 36 | vxChannelCombineNode(graph, virtu8[numv8 - 3], virtu8[numv8 - 2], 37 | virtu8[numv8 - 1], NULL, output); 38 | 39 | for (i =0; i < numv8; ++i) 40 | vxReleaseImage(&virtu8[i]); 41 | return graph; 42 | } 43 | 44 | void main(int argc, void **argv) 45 | { 46 | if (argc != 3) 47 | { 48 | printf("Filter an image\n" 49 | "%s \n", (char *)argv[0]); 50 | } 51 | else 52 | { 53 | struct read_image_attributes attr; 54 | vx_context context = vxCreateContext(); 55 | vx_image image = createImageFromFile(context, (const char *)argv[1], &attr); 56 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 57 | vx_graph graph = makeFilterGraph(context, image, output); 58 | if (vxGetStatus((vx_reference)image)) 59 | printf("Could not create input image\n"); 60 | else if (vxProcessGraph(graph)) 61 | printf("Error processing graph\n"); 62 | else if (writeImage(output, (const char *)argv[2])) 63 | printf("Problem writing the output image\n"); 64 | vxReleaseContext(&context); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /book_samples/filter/filterImage.c: -------------------------------------------------------------------------------- 1 | /* 2 | copyImage.c 3 | Read and write an image. 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include "readImage.h" 9 | #include "writeImage.h" 10 | 11 | vx_graph makeFilterGraph(vx_context context, vx_image input, vx_image output) 12 | { 13 | /* creates a graph with one input image and one output image. 14 | You supply the input and output images, it is assumed that the input and output images are RGB. 15 | Replace the default processing with what you like! 16 | */ 17 | #define numv8 (6) /* Number of virtual U8 images we need */ 18 | vx_graph graph = vxCreateGraph(context); 19 | vx_image virtu8[numv8]; 20 | int i, j; 21 | 22 | for (i = 0; i < numv8; ++i) 23 | virtu8[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 24 | 25 | /* Set convolution coefficients to a 3x3 box filter */ 26 | vx_int16 scharr_coeffs[3][3] = { 27 | {3, 0, -3}, 28 | {10, 0, -10}, 29 | {3, 0, -3} 30 | }; 31 | /* Set the scale for convolution */ 32 | vx_uint32 scale = 2; 33 | 34 | /* Create convolution object, set convolution coefficients and scale*/ 35 | vx_convolution scharr = vxCreateConvolution(context, 3, 3); 36 | vxCopyConvolutionCoefficients(scharr, (vx_int16*)scharr_coeffs, VX_WRITE_ONLY, 37 | VX_MEMORY_TYPE_HOST); 38 | vxSetConvolutionAttribute(scharr, VX_CONVOLUTION_SCALE, &scale, sizeof(scale)); 39 | 40 | /* Do custom filtering on the input image */ 41 | /* First, extract R, G, and B channels to individual virtual images */ 42 | vxChannelExtractNode(graph, input, VX_CHANNEL_R, virtu8[0]); 43 | vxChannelExtractNode(graph, input, VX_CHANNEL_G, virtu8[1]); 44 | vxChannelExtractNode(graph, input, VX_CHANNEL_B, virtu8[2]); 45 | 46 | /* Now, run box filter on each of gray scale images */ 47 | for(i = 0; i < 3; i++) 48 | vxConvolveNode(graph, virtu8[i], scharr, virtu8[i + 3]); 49 | 50 | /* Now combine images together in the ouptut color image */ 51 | vxChannelCombineNode(graph, virtu8[3], virtu8[4], virtu8[5], NULL, output); 52 | 53 | for (i =0; i < numv8; ++i) 54 | vxReleaseImage(&virtu8[i]); 55 | return graph; 56 | } 57 | 58 | void main(int argc, char **argv) 59 | { 60 | if (argc != 3) 61 | { 62 | printf("Filter an image\n" 63 | "%s \n", (char *)argv[0]); 64 | } 65 | else 66 | { 67 | struct read_image_attributes attr; 68 | vx_context context = vxCreateContext(); 69 | vx_image image = createImageFromFile(context, (const char *)argv[1], &attr); 70 | vx_image output = vxCreateImage(context, attr.width, attr.height, attr.format); 71 | vx_graph graph = makeFilterGraph(context, image, output); 72 | if (vxGetStatus((vx_reference)image)) 73 | printf("Could not create input image\n"); 74 | else if (vxProcessGraph(graph)) 75 | printf("Error processing graph\n"); 76 | else if (writeImage(output, (const char *)argv[2])) 77 | printf("Problem writing the output image\n"); 78 | vxReleaseContext(&context); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /book_samples/filter/filterImageROI.c: -------------------------------------------------------------------------------- 1 | /* 2 | copyImage.c 3 | Read and write an image. 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include "readImage.h" 9 | #include "writeImage.h" 10 | 11 | vx_graph makeFilterGraph(vx_context context, vx_image input, 12 | vx_rectangle_t* rect, vx_image output) 13 | { 14 | /* creates a graph with one input image and one output image. 15 | You supply the input and output images, it is assumed that the input and output images are RGB. 16 | Replace the default processing with what you like! 17 | */ 18 | #define numv8 (6) /* Number of virtual U8 images we need */ 19 | vx_graph graph = vxCreateGraph(context); 20 | vx_image virtu8[numv8]; 21 | int i, j; 22 | 23 | for (i = 0; i < numv8; ++i) 24 | virtu8[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 25 | 26 | /* Set convolution coefficients to a 3x3 box filter */ 27 | vx_int16 scharr_coeffs[3][3] = { 28 | {3, 0, -3}, 29 | {10, 0, -10}, 30 | {3, 0, -3} 31 | }; 32 | /* Set the scale for convolution */ 33 | vx_uint32 scale = 2; 34 | 35 | /* Create convolution object, set convolution coefficients and scale*/ 36 | vx_convolution scharr = vxCreateConvolution(context, 3, 3); 37 | vxCopyConvolutionCoefficients(scharr, (vx_int16*)scharr_coeffs, VX_WRITE_ONLY, 38 | VX_MEMORY_TYPE_HOST); 39 | vxSetConvolutionAttribute(scharr, VX_CONVOLUTION_SCALE, &scale, sizeof(scale)); 40 | 41 | /* create ROI */ 42 | vx_image roi = vxCreateImageFromROI(input, rect); 43 | 44 | /* Do scharr filtering on the input image */ 45 | /* First, extract R, G, and B channels to individual virtual images */ 46 | vxChannelExtractNode(graph, roi, VX_CHANNEL_R, virtu8[0]); 47 | vxChannelExtractNode(graph, roi, VX_CHANNEL_G, virtu8[1]); 48 | vxChannelExtractNode(graph, roi, VX_CHANNEL_B, virtu8[2]); 49 | 50 | /* Now, run box filter on each of gray scale images */ 51 | for(i = 0; i < 3; i++) 52 | vxConvolveNode(graph, virtu8[i], scharr, virtu8[i + 3]); 53 | 54 | /* Now combine images together in the ouptut color image */ 55 | vxChannelCombineNode(graph, virtu8[3], virtu8[4], virtu8[5], NULL, output); 56 | 57 | for (i =0; i < numv8; ++i) 58 | vxReleaseImage(&virtu8[i]); 59 | return graph; 60 | } 61 | 62 | void main(int argc, char **argv) 63 | { 64 | struct read_image_attributes attr; 65 | vx_context context = vxCreateContext(); 66 | vx_image image = createImageFromFile(context, "cup.ppm", &attr); 67 | 68 | vx_rectangle_t rect; 69 | rect.start_x = 48; 70 | rect.start_y = 98; 71 | rect.end_x = 258; 72 | rect.end_y = 202; 73 | int width = rect.end_x - rect.start_x; 74 | int height = rect.end_y - rect.start_y; 75 | 76 | vx_image output = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 77 | vx_graph graph = makeFilterGraph(context, image, &rect, output); 78 | if (vxGetStatus((vx_reference)image)) 79 | printf("Could not create input image\n"); 80 | else if (vxProcessGraph(graph)) 81 | printf("Error processing graph\n"); 82 | else if (writeImage(output, "cup_scharr_roi.ppm")) 83 | printf("Problem writing the output image\n"); 84 | vxReleaseContext(&context); 85 | } 86 | -------------------------------------------------------------------------------- /book_samples/filter/filterImageROIvxu.c: -------------------------------------------------------------------------------- 1 | /* 2 | copyImage.c 3 | Read and write an image. 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "readImage.h" 10 | #include "writeImage.h" 11 | 12 | void main(int argc, char **argv) 13 | { 14 | printf("Started\n"); 15 | struct read_image_attributes attr; 16 | vx_context context = vxCreateContext(); 17 | 18 | vx_border_t border_mode; 19 | border_mode.mode = VX_BORDER_REPLICATE; 20 | vxSetContextAttribute(context, VX_CONTEXT_IMMEDIATE_BORDER, &border_mode, sizeof(border_mode)); 21 | 22 | vx_image input = createImageFromFile(context, "cup.ppm", &attr); 23 | printf("input image created\n"); 24 | if(vxGetStatus(input) != VX_SUCCESS) 25 | { 26 | printf("Status error: %d", vxGetStatus(input)); 27 | } 28 | 29 | vx_rectangle_t rect; 30 | rect.start_x = 48; 31 | rect.start_y = 98; 32 | rect.end_x = 258; 33 | rect.end_y = 202; 34 | vx_image roi = vxCreateImageFromROI(input, &rect); 35 | 36 | int width = rect.end_x - rect.start_x; 37 | int height = rect.end_y - rect.start_y; 38 | 39 | /* create temporary images for working with edge images */ 40 | vx_image copy_channel[3], roi_channel[3], edges, edges_inv; 41 | edges = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); 42 | edges_inv = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); 43 | 44 | /* set the threshold value */ 45 | vx_pixel_value_t lower, higher; 46 | lower.U32 = 50; 47 | higher.U32 = 100; 48 | 49 | /* create a threshold object */ 50 | vx_threshold threshold = vxCreateThresholdForImage(context, 51 | VX_THRESHOLD_TYPE_RANGE, VX_DF_IMAGE_U8, VX_DF_IMAGE_U8); 52 | if(vxGetStatus(threshold) != VX_SUCCESS) 53 | { 54 | printf("Threshold creation failed\n"); 55 | } 56 | 57 | /* set threshold values */ 58 | vxCopyThresholdRange(threshold, &lower, &higher, VX_WRITE_ONLY, 59 | VX_MEMORY_TYPE_HOST); 60 | 61 | enum vx_channel_e channels[] = {VX_CHANNEL_R, VX_CHANNEL_G, VX_CHANNEL_B}; 62 | for(int i = 0; i < 3; i++) 63 | { 64 | roi_channel[i] = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); 65 | copy_channel[i] = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); 66 | vxuChannelExtract(context, roi, channels[i], roi_channel[i]); 67 | 68 | vxuCannyEdgeDetector(context, roi_channel[i], threshold, 3, VX_NORM_L2, edges); 69 | 70 | vxuNot(context, edges, edges_inv); 71 | vxuAnd(context, roi_channel[i], edges_inv, copy_channel[i]); 72 | } 73 | 74 | vxuChannelCombine(context, copy_channel[0], copy_channel[1], copy_channel[2], NULL, roi); 75 | for(int i = 0; i < 3; i++) 76 | { 77 | vxReleaseImage(©_channel[i]); 78 | vxReleaseImage(&roi_channel[i]); 79 | } 80 | vxReleaseImage(&edges); 81 | vxReleaseImage(&edges_inv); 82 | vxReleaseThreshold(&threshold); 83 | 84 | if(writeImage(input, "cup_roi.ppm")) 85 | printf("Problem writing the output image\n"); 86 | 87 | vxReleaseImage(&roi); 88 | vxReleaseImage(&input); 89 | vxReleaseContext(&context); 90 | } 91 | -------------------------------------------------------------------------------- /book_samples/hough/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(hough houghLines.c) 2 | target_link_libraries(hough ${OpenCV_LIBS} vxa ${OPENVX}) 3 | 4 | add_executable(houghEx houghLinesEx.c) 5 | target_link_libraries(houghEx ${OpenCV_LIBS} vxa ${OPENVX}) 6 | -------------------------------------------------------------------------------- /book_samples/hough/houghLines.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file houghLines.c 26 | * \example houghLines 27 | * \brief This OpenVX sample finds lines in an input image using Hough 28 | * Transform and draws them on top of of the input image. 29 | * \author Victor Erukhimov 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "readImage.h" 38 | #include "writeImage.h" 39 | 40 | void log_callback(vx_context context, vx_reference ref, 41 | vx_status status, const char* string) 42 | { 43 | printf("Log message: status %d, text: %s\n", (int)status, string); 44 | } 45 | 46 | vx_graph makeHoughLinesGraph(vx_context context, vx_image input, 47 | vx_image* binary, vx_array lines) 48 | { 49 | vx_uint32 width, height; 50 | vxQueryImage(input, VX_IMAGE_WIDTH, &width, sizeof(vx_uint32)); 51 | vxQueryImage(input, VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32)); 52 | printf("Read width %d, height %d\n", width, height); 53 | 54 | int widthr = width/4; 55 | int heightr = height/4; 56 | 57 | vx_graph graph = vxCreateGraph(context); 58 | 59 | #define nums16 (3) 60 | vx_image virt_s16[nums16]; 61 | 62 | /* create virtual images */ 63 | vx_image virt_nv12 = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_NV12); 64 | vx_image virt_y = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 65 | vx_image virt_yr = vxCreateVirtualImage(graph, widthr, heightr, 66 | VX_DF_IMAGE_U8); 67 | vx_image binary_thresh = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 68 | 69 | for(int i = 0; i < nums16; i++) 70 | { 71 | virt_s16[i] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_S16); 72 | } 73 | 74 | *binary = vxCreateImage(context, widthr, heightr, VX_DF_IMAGE_U8); 75 | 76 | /* extract grayscale channel */ 77 | vxColorConvertNode(graph, input, virt_nv12); 78 | vxChannelExtractNode(graph, virt_nv12, VX_CHANNEL_Y, virt_y); 79 | 80 | /* resize down */ 81 | vxScaleImageNode(graph, virt_y, virt_yr, VX_INTERPOLATION_BILINEAR); 82 | 83 | /* compute gradient */ 84 | vxSobel3x3Node(graph, virt_yr, virt_s16[0], virt_s16[1]); 85 | vxMagnitudeNode(graph, virt_s16[0], virt_s16[1], virt_s16[2]); 86 | 87 | /* setup threshold value */ 88 | vx_threshold thresh = vxCreateThresholdForImage(context, 89 | VX_THRESHOLD_TYPE_BINARY, VX_DF_IMAGE_S16, VX_DF_IMAGE_U8); 90 | vx_pixel_value_t pixel_value; 91 | pixel_value.S16 = 256; 92 | vxCopyThresholdValue(thresh, &pixel_value, VX_WRITE_ONLY, 93 | VX_MEMORY_TYPE_HOST); 94 | 95 | vx_status status = vxGetStatus((vx_reference)thresh); 96 | if(status != VX_SUCCESS) 97 | { 98 | printf("Issue with thresh: %d\n", status); 99 | } 100 | 101 | vx_node thresh_node = vxThresholdNode(graph, virt_s16[2], thresh, 102 | binary_thresh); 103 | status = vxGetStatus((vx_reference)thresh_node); 104 | if(status != VX_SUCCESS) 105 | { 106 | printf("Issue with threshold node: %d\n", status); 107 | } 108 | 109 | /* dilate the threshold output */ 110 | vxDilate3x3Node(graph, binary_thresh, *binary); 111 | 112 | /* run hough transform */ 113 | vx_hough_lines_p_t hough_params; 114 | hough_params.rho = 1.0f; 115 | hough_params.theta = 3.14f/180; 116 | hough_params.threshold = 100; 117 | hough_params.line_length = 100; 118 | hough_params.line_gap = 10; 119 | hough_params.theta_max = 3.14; 120 | hough_params.theta_min = 0.0; 121 | 122 | vx_scalar num_lines = vxCreateScalar(context, VX_TYPE_SIZE, NULL); 123 | vxHoughLinesPNode(graph, *binary, &hough_params, lines, num_lines); 124 | 125 | return graph; 126 | } 127 | 128 | int main(int argc, char **argv) 129 | { 130 | if (argc != 4) 131 | { 132 | printf("Find straight lines in an image\n" 133 | "%s \n", (char *)argv[0]); 134 | exit(0); 135 | } 136 | 137 | const char* input_filename = argv[1]; 138 | const char* binary_filename = argv[2]; 139 | const char* lines_filename = argv[3]; 140 | 141 | vx_context context = vxCreateContext(); 142 | vx_image image, binary; 143 | vxa_read_image((const char *)input_filename, context, &image); 144 | 145 | vx_uint32 width, height; 146 | vxQueryImage(image, VX_IMAGE_WIDTH, &width, sizeof(vx_uint32)); 147 | vxQueryImage(image, VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32)); 148 | 149 | /* create an array for storing hough lines output */ 150 | const vx_size max_num_lines = 2000; 151 | vx_array lines = vxCreateArray(context, VX_TYPE_LINE_2D, max_num_lines); 152 | vx_graph graph = makeHoughLinesGraph(context, image, &binary, lines); 153 | 154 | vxRegisterLogCallback(context, log_callback, vx_true_e); 155 | 156 | vxProcessGraph(graph); 157 | 158 | vxa_write_image(binary, binary_filename); 159 | 160 | // draw the lines 161 | vx_pixel_value_t color; 162 | color.RGB[0] = 0; 163 | color.RGB[1] = 255; 164 | color.RGB[2] = 0; 165 | vx_image image_lines; 166 | vx_size _num_lines; 167 | vxQueryArray(lines, VX_ARRAY_NUMITEMS, &_num_lines, sizeof(_num_lines)); 168 | draw_lines(context, binary, lines, _num_lines, 169 | &color, 2, &image_lines); 170 | vxa_write_image(image_lines, lines_filename); 171 | 172 | vxReleaseContext(&context); 173 | return(0); 174 | } 175 | -------------------------------------------------------------------------------- /book_samples/opencl_interop/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # Permission is hereby granted, free of charge, to any person obtaining a 3 | # copy of this software and/or associated documentation files (the 4 | # "Materials"), to deal in the Materials without restriction, including 5 | # without limitation the rights to use, copy, modify, merge, publish, 6 | # distribute, sublicense, and/or sell copies of the Materials, and to 7 | # permit persons to whom the Materials are furnished to do so, subject to 8 | # the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included 11 | # in all copies or substantial portions of the Materials. 12 | # 13 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 16 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 17 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 18 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 19 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 20 | # 21 | 22 | cmake_minimum_required(VERSION 2.8.9) 23 | 24 | find_package(OpenCL REQUIRED) 25 | 26 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -DCL_USE_DEPRECATED_OPENCL_1_2_APIS") 27 | 28 | # opencl_example 29 | add_executable(opencl_kernel_example opencl_kernel_example.cpp) 30 | target_link_libraries(opencl_kernel_example ${OpenCL_LIBRARIES}) 31 | 32 | # interop_example 33 | add_executable(opencl_interop_example opencl_interop_example.cpp my_vx_tensor_map_impl.cpp) 34 | target_link_libraries(opencl_interop_example openvx ${OpenCL_LIBRARIES}) 35 | -------------------------------------------------------------------------------- /book_samples/opencl_interop/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Permission is hereby granted, free of charge, to any person obtaining a 3 | * copy of this software and/or associated documentation files (the 4 | * "Materials"), to deal in the Materials without restriction, including 5 | * without limitation the rights to use, copy, modify, merge, publish, 6 | * distribute, sublicense, and/or sell copies of the Materials, and to 7 | * permit persons to whom the Materials are furnished to do so, subject to 8 | * the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included 11 | * in all copies or substantial portions of the Materials. 12 | * 13 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 16 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 17 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 18 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 19 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 20 | */ 21 | 22 | /*! 23 | * \file common.h 24 | * \brief some used macros 25 | * \author Radhakrishna Giduthuri 26 | */ 27 | 28 | #ifndef common_h__ 29 | #define common_h__ 30 | 31 | #include 32 | #include 33 | 34 | //////// 35 | // Useful macros for OpenVX/OpenCL error checking: 36 | // ERROR_CHECK_NOT_NULL - check and abort if returned value is NULL 37 | // ERROR_CHECK_STATUS - check and abort if returned status is an error 38 | // ERROR_CHECK_STATUS_RET - check and return if status is an error 39 | // 40 | #define ERROR_CHECK_NOT_NULL( obj ) { \ 41 | if((obj) == NULL) { \ 42 | printf("ERROR: returned NULL at " __FILE__ "#%d\n", __LINE__); \ 43 | exit(1); \ 44 | } \ 45 | } 46 | #define ERROR_CHECK_STATUS( status ) { \ 47 | auto status_ = (status); \ 48 | if(status_) { \ 49 | printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ 50 | exit(1); \ 51 | } \ 52 | } 53 | #define ERROR_CHECK_STATUS_RET( status ) { \ 54 | auto status_ = (status); \ 55 | if(status_) { \ 56 | printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ 57 | return status_; \ 58 | } \ 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /book_samples/opencl_interop/my_vx_tensor_map_impl.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Permission is hereby granted, free of charge, to any person obtaining a 3 | * copy of this software and/or associated documentation files (the 4 | * "Materials"), to deal in the Materials without restriction, including 5 | * without limitation the rights to use, copy, modify, merge, publish, 6 | * distribute, sublicense, and/or sell copies of the Materials, and to 7 | * permit persons to whom the Materials are furnished to do so, subject to 8 | * the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included 11 | * in all copies or substantial portions of the Materials. 12 | * 13 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 16 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 17 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 18 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 19 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 20 | */ 21 | 22 | /*! 23 | * \file my_vx_tensor_map_impl.c 24 | * \brief This is a simple reference implementation (not optimized) 25 | * to demonstrate vxMapTensorPatch functionality, since it is 26 | * not yet available in KhronosGroup/OpenVX-sample-impl repo 27 | * \author Radhakrishna Giduthuri 28 | */ 29 | 30 | #include 31 | #include "my_vx_tensor_map_impl.h" 32 | #include "common.h" 33 | 34 | //// 35 | // constants 36 | // 37 | #define MAX_TENSOR_DIMS 6 38 | 39 | //// 40 | // local data structure to pass data between vxMapTensorPatch & vxUnmapTensorPatch 41 | // 42 | struct my_vx_tensor_map_id { 43 | vx_size number_of_dims; 44 | vx_size view_start[MAX_TENSOR_DIMS]; 45 | vx_size view_end[MAX_TENSOR_DIMS]; 46 | vx_size stride[MAX_TENSOR_DIMS]; 47 | void * ptr; 48 | vx_enum usage; 49 | vx_enum mem_type; 50 | }; 51 | 52 | //// 53 | // simple reference implementation (not optimized) for vxMapTensorPatch 54 | // this creates a temporary buffer and uses a copy, instead of returning 55 | // OpenVX internal buffer allocated for the tensor (so the copy overheads) 56 | // 57 | vx_status myVxMapTensorPatch( 58 | vx_tensor tensor, 59 | vx_size number_of_dims, 60 | const vx_size* view_start, 61 | const vx_size* view_end, 62 | vx_map_id* map_id, 63 | vx_size* stride, 64 | void** ptr, 65 | vx_enum usage, 66 | vx_enum mem_type) 67 | { 68 | //// 69 | // calculate output stride values 70 | // 71 | vx_enum data_type; 72 | vx_size dims[MAX_TENSOR_DIMS]; 73 | ERROR_CHECK_STATUS( vxQueryTensor(tensor, VX_TENSOR_DATA_TYPE, &data_type, sizeof(vx_enum)) ); 74 | ERROR_CHECK_STATUS( vxQueryTensor(tensor, VX_TENSOR_DIMS, &dims, sizeof(vx_size)*number_of_dims) ); 75 | switch(data_type) { 76 | case VX_TYPE_INT16: stride[0] = sizeof(vx_int16); break; 77 | case VX_TYPE_UINT8: stride[0] = sizeof(vx_uint8); break; 78 | default: return VX_ERROR_NOT_SUPPORTED; 79 | } 80 | for(size_t dim = 1; dim < number_of_dims; dim++) { 81 | stride[dim] = dims[dim-1] * stride[dim-1]; 82 | } 83 | vx_size size = dims[number_of_dims-1] * stride[number_of_dims-1]; 84 | 85 | //// 86 | // create map_id and allocate temporary buffers to return 87 | // 88 | my_vx_tensor_map_id * id = new my_vx_tensor_map_id; 89 | ERROR_CHECK_NOT_NULL( id ); 90 | id->number_of_dims = number_of_dims; 91 | id->usage = usage; 92 | id->mem_type = mem_type; 93 | if(id->mem_type == VX_MEMORY_TYPE_OPENCL_BUFFER) 94 | { 95 | vx_context context = vxGetContext((vx_reference)tensor); 96 | cl_context opencl_ctx; 97 | ERROR_CHECK_STATUS( vxQueryContext(context, VX_CONTEXT_CL_CONTEXT, &opencl_ctx, sizeof(cl_context)) ); 98 | cl_int err; 99 | id->ptr = clCreateBuffer(opencl_ctx, CL_MEM_READ_WRITE, size, NULL, &err); 100 | ERROR_CHECK_STATUS( err ); 101 | } 102 | else 103 | { 104 | id->ptr = new vx_uint8[size]; 105 | ERROR_CHECK_NOT_NULL( id->ptr ); 106 | } 107 | for(size_t dim = 0; dim < number_of_dims; dim++) { 108 | id->view_start[dim] = view_start ? view_start[dim] : 0; 109 | id->view_end[dim] = view_end ? view_end[dim] : dims[dim]; 110 | id->stride[dim] = stride[dim]; 111 | } 112 | *map_id = (vx_map_id)id; 113 | *ptr = id->ptr; 114 | 115 | //// 116 | // copy tensor patch into temporarily allocated map buffer if read requested 117 | // 118 | if(id->usage == VX_READ_ONLY || id->usage == VX_READ_AND_WRITE) 119 | { 120 | vx_status status = vxCopyTensorPatch(tensor, id->number_of_dims, 121 | id->view_start, id->view_end, id->stride, id->ptr, 122 | VX_READ_ONLY, id->mem_type); 123 | if(status != VX_SUCCESS) 124 | { 125 | // release resources in case or error 126 | if(id->mem_type == VX_MEMORY_TYPE_OPENCL_BUFFER) { 127 | ERROR_CHECK_STATUS( clReleaseMemObject((cl_mem)id->ptr) ); 128 | } 129 | else { 130 | delete (vx_uint8 *)id->ptr; 131 | } 132 | delete id; 133 | return status; 134 | } 135 | } 136 | 137 | return VX_SUCCESS; 138 | } 139 | 140 | //// 141 | // simple reference implementation (not optimized) for vxUnmapTensorPatch 142 | // this uses the temporary buffer used for mapping in myVxMapTensorPatch 143 | // 144 | vx_status myVxUnmapTensorPatch( 145 | vx_tensor tensor, 146 | const vx_map_id map_id) 147 | { 148 | //// 149 | // access mapped data 150 | // 151 | my_vx_tensor_map_id * id = (my_vx_tensor_map_id *)map_id; 152 | 153 | //// 154 | // copy tensor patch into temporarily allocated map buffer if read requested 155 | // 156 | if(id->usage == VX_WRITE_ONLY || id->usage == VX_READ_AND_WRITE) 157 | { 158 | vx_status status = vxCopyTensorPatch(tensor, id->number_of_dims, 159 | id->view_start, id->view_end, id->stride, id->ptr, 160 | VX_WRITE_ONLY, id->mem_type); 161 | if(status != VX_SUCCESS) { 162 | // release resources in case or error 163 | if(id->mem_type == VX_MEMORY_TYPE_OPENCL_BUFFER) { 164 | ERROR_CHECK_STATUS( clReleaseMemObject((cl_mem)id->ptr) ); 165 | } 166 | else { 167 | delete (vx_uint8 *)id->ptr; 168 | } 169 | delete id; 170 | return status; 171 | } 172 | } 173 | 174 | //// 175 | // release temporary buffers allocated for mapping 176 | // 177 | if(id->mem_type == VX_MEMORY_TYPE_OPENCL_BUFFER) { 178 | ERROR_CHECK_STATUS( clReleaseMemObject((cl_mem)id->ptr) ); 179 | } 180 | else { 181 | delete (vx_uint8 *)id->ptr; 182 | } 183 | delete id; 184 | 185 | return VX_SUCCESS; 186 | } 187 | -------------------------------------------------------------------------------- /book_samples/opencl_interop/my_vx_tensor_map_impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Permission is hereby granted, free of charge, to any person obtaining a 3 | * copy of this software and/or associated documentation files (the 4 | * "Materials"), to deal in the Materials without restriction, including 5 | * without limitation the rights to use, copy, modify, merge, publish, 6 | * distribute, sublicense, and/or sell copies of the Materials, and to 7 | * permit persons to whom the Materials are furnished to do so, subject to 8 | * the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included 11 | * in all copies or substantial portions of the Materials. 12 | * 13 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 14 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 15 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 16 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 17 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 18 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 19 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 20 | */ 21 | 22 | /*! 23 | * \file my_vx_tensor_map_impl.h 24 | * \brief This is a simple reference implementation (not optimized) 25 | * to demonstrate vxMapTensorPatch functionality, since it is 26 | * not yet available in KhronosGroup/OpenVX-sample-impl repo 27 | * \author Radhakrishna Giduthuri 28 | */ 29 | 30 | #ifndef my_vx_tensor_map_impl_h__ 31 | #define my_vx_tensor_map_impl_h__ 32 | 33 | #include 34 | 35 | //// 36 | // simple reference implementations (not optimized) for vxMapTensorPatch & vxUnmapTensorPatch 37 | // 1. refer to my_vx_tensor_map_impl.cpp for the unoptimized source 38 | // 2. the my_vx_tensor_map_impl.h & my_vx_tensor_map_impl.cpp can be removed 39 | // once the sample implementation supports the vxMapTensorPatch and 40 | // vxUnmapTensorPatch APIs 41 | // 42 | #define vxMapTensorPatch myVxMapTensorPatch 43 | #define vxUnmapTensorPatch myVxUnmapTensorPatch 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | vx_status vxMapTensorPatch( 50 | vx_tensor tensor, 51 | vx_size number_of_dims, 52 | const vx_size* view_start, 53 | const vx_size* view_end, 54 | vx_map_id* map_id, 55 | vx_size* stride, 56 | void** ptr, 57 | vx_enum usage, 58 | vx_enum mem_type); 59 | 60 | //// 61 | // simple reference implementation (not optimized) for vxUnmapTensorPatch 62 | // this uses the temporary buffer used for mapping in myVxMapTensorPatch 63 | // 64 | vx_status vxUnmapTensorPatch( 65 | vx_tensor tensor, 66 | const vx_map_id map_id); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /book_samples/ppm-io/readImage.h: -------------------------------------------------------------------------------- 1 | /* 2 | readImage.h 3 | Read an image file into a vx_image. 4 | */ 5 | #ifndef _readImage_h_included_ 6 | #define _readImage_h_included_ 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | enum read_image_crop { 11 | READ_IMAGE_USE_NONE, /* Error if file image too large in either dimension */ 12 | READ_IMAGE_USE_TOP_LEFT, /* Use left of rows and top of columns */ 13 | READ_IMAGE_USE_TOP_RIGHT, /* use right of rows and top of columns */ 14 | READ_IMAGE_USE_BOTTOM_LEFT, /* use left of rows and bottom of columns */ 15 | READ_IMAGE_USE_BOTTOM_RIGHT, /* use right of rows and bottom of columns */ 16 | READ_IMAGE_USE_CENTRE /* use centre of image. Indices are truncated when 17 | there are an odd number of pixels left */ 18 | }; 19 | 20 | enum read_image_place { 21 | READ_IMAGE_PLACE_NONE, /* Error is file image too small in either direction */ 22 | READ_IMAGE_PLACE_TOP_LEFT, /* Spare pixels in the vx_image are at the right and bottom */ 23 | READ_IMAGE_PLACE_TOP_RIGHT, /* Spare pixels in the vx_image are at the left and bottom */ 24 | READ_IMAGE_PLACE_BOTTOM_LEFT, /* Spare pixels in the vx_image are at the top and right */ 25 | READ_IMAGE_PLACE_BOTTOM_RIGHT, /* Spare pixels in the vx_image are at the top and left */ 26 | READ_IMAGE_PLACE_CENTRE /* Spare pixels inthe vx_image are distributed evenly. Indices 27 | are truncated when there is an odd number of spare pixels */ 28 | }; 29 | 30 | enum read_image_fill { 31 | READ_IMAGE_FILL_NONE, /* Leave spare locations in the target image unchanged */ 32 | READ_IMAGE_FILL_ZERO, /* Fill with zeros (except A in RGBA will always be max) */ 33 | READ_IMAGE_FILL_ONES /* Fill with maximum value */ 34 | }; 35 | 36 | struct read_image_attributes { /* Struct to report back the attributes of the image created from file */ 37 | vx_uint32 width, height; 38 | vx_df_image format; 39 | }; 40 | 41 | vx_status readImage(vx_image image, const char *filename, enum read_image_crop crop, 42 | enum read_image_place place, enum read_image_fill fill); 43 | 44 | vx_image createImageFromFile(vx_context context, const char *filename, struct read_image_attributes *attr); 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | #endif -------------------------------------------------------------------------------- /book_samples/ppm-io/writeImage.c: -------------------------------------------------------------------------------- 1 | /* 2 | writeImage.c 3 | Write an image out to a .ppm or .pgm file. 4 | Supported image formats: 5 | VX_DF_IMAGE_U8: creates a portable greyscale map (P5) with a maximum value of 255 6 | VX_DF_IMAGE_U16:creates a portable greyscale map (P5) with a maximum value of 65535 7 | VX_DF_IMAGE_RGB: creates a portable pixel map (P6) with a maximum value of 255 8 | VX_DF_IMAGE_RGBX: creates a portable pixel map (P6) with a maximum value of 255, the fourth channel is not output. 9 | Return values: 10 | VX_SUCCESS : all is OK! 11 | VX_FAILURE: Could not open file or write error 12 | VX_ERROR_NOT_SUPPORTED: Image is not of a supported format 13 | Another error may be reported if there is a problem with the image itself. 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include "writeImage.h" 19 | 20 | vx_status writeImage(vx_image image, const char *filename) 21 | { 22 | vx_uint32 image_width; 23 | vx_uint32 image_height; 24 | vx_df_image image_format; 25 | vx_status status = vxGetStatus((vx_reference)image) || 26 | vxQueryImage(image, VX_IMAGE_WIDTH, &image_width, sizeof(image_width)) || 27 | vxQueryImage(image, VX_IMAGE_HEIGHT, &image_height, sizeof(image_height)) || 28 | vxQueryImage(image, VX_IMAGE_FORMAT, &image_format, sizeof(image_format)); 29 | vx_rectangle_t rect = {.start_x = 0, .start_y = 0, .end_x = image_width, .end_y = image_height}; 30 | vx_imagepatch_addressing_t addr = VX_IMAGEPATCH_ADDR_INIT; 31 | void * imgp; 32 | vx_map_id map_id; 33 | int maxval, psz; 34 | char fmt; 35 | switch (image_format) 36 | { 37 | case VX_DF_IMAGE_U8: 38 | fmt = '5'; 39 | psz = 1; 40 | maxval = 255; 41 | break; 42 | case VX_DF_IMAGE_U16: 43 | fmt = '5'; 44 | psz = 2; 45 | maxval = 65535; 46 | break; 47 | case VX_DF_IMAGE_RGB: 48 | case VX_DF_IMAGE_RGBX: 49 | fmt = '6'; 50 | psz = 3; 51 | maxval = 255; 52 | break; 53 | default: 54 | status = VX_ERROR_NOT_SUPPORTED; 55 | } 56 | status = status || vxMapImagePatch(image, &rect, 0, &map_id, &addr, &imgp, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X); 57 | if (VX_SUCCESS == status) 58 | { 59 | FILE *fp = fopen(filename, "wb"); 60 | if (fp) 61 | { 62 | fprintf(fp, "P%c\n%d %d\n%d\n", fmt, image_width, image_height, maxval); 63 | int x, y; 64 | for (y = 0; y < image_height; ++y) 65 | for (x = 0; x < image_width; ++x) 66 | { 67 | if (fwrite(vxFormatImagePatchAddress2d(imgp, x, y, &addr), 1, psz, fp) != psz) 68 | status = VX_FAILURE; 69 | } 70 | if (fclose(fp)) 71 | status = VX_FAILURE; 72 | } 73 | else 74 | status = VX_FAILURE; 75 | vxUnmapImagePatch(image, map_id); 76 | } 77 | return status; 78 | } -------------------------------------------------------------------------------- /book_samples/ppm-io/writeImage.h: -------------------------------------------------------------------------------- 1 | /* 2 | writeImage.h 3 | Write an image out to a .ppm or .pgm file. 4 | Supported image formats: 5 | VX_DF_IMAGE_U8: creates a portable greyscale map (P5) with a maximum value of 255 6 | VX_DF_IMAGE_U16:creates a portable greyscale map (P5) with a maximum value of 65535 7 | VX_DF_IMAGE_RGB: creates a portable pixel map (P6) with a maximum value of 255 8 | VX_DF_IMAGE_RGBX: creates a portable pixel map (P6) with a maximum value of 255, the fourth channel is not output. 9 | */ 10 | #ifndef _writeImage_h_included_ 11 | #define _writeImage_h_included_ 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | vx_status writeImage(vx_image image, const char *filename); 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | #endif 20 | -------------------------------------------------------------------------------- /book_samples/stitch/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(stitch stitch.c) 2 | target_link_libraries(stitch ${OpenCV_LIBS} vxa ${OPENVX}) 3 | 4 | add_executable(stitch-debug stitch-debug.c) 5 | target_link_libraries(stitch-debug ${OpenCV_LIBS} vxa ${OPENVX}) 6 | 7 | add_executable(stitch-multiband stitch-multiband.c) 8 | target_link_libraries(stitch-multiband ${OpenCV_LIBS} vxa ${OPENVX}) 9 | 10 | add_executable(homography homography-opencv.cpp) 11 | target_link_libraries(homography ${OpenCV_LIBS}) 12 | 13 | add_executable(homography-multiband homography-multiband-opencv.cpp) 14 | target_link_libraries(homography-multiband ${OpenCV_LIBS}) 15 | -------------------------------------------------------------------------------- /book_samples/stitch/stitch-debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file stitch_debug.c 26 | * \example stitch_debug 27 | * \brief This OpenVX sample blends two images using predefined remap 28 | * transformations and blending coefficients (see homography-opencv.cpp), 29 | * computing intermediate results for debugging purposes. 30 | * \author Victor Erukhimov 31 | */ 32 | 33 | 34 | #include 35 | #include 36 | #include "VX/vx.h" 37 | #include "vxa/vxa.h" 38 | 39 | vx_graph makeFilterGraph(vx_context context, vx_image image1, vx_image image2, 40 | vx_remap remap1, vx_image coeffs1, vx_remap remap2, vx_image coeffs2, 41 | vx_image output, vx_image output_remapped1, vx_image output_remapped2, 42 | vx_image output_weighted1, vx_image output_weighted2) 43 | { 44 | /* Create virtual images */ 45 | const int numu8 = 5; 46 | vx_image virtu8[numu8][3]; 47 | const int nums16 = 1; 48 | vx_image virts16[nums16][3]; 49 | 50 | vx_graph graph = vxCreateGraph(context); 51 | 52 | int i, j; 53 | 54 | for(i = 0; i < numu8; i++) 55 | { 56 | for (j = 0; j < 3; j++) 57 | virtu8[i][j] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 58 | } 59 | for(i = 0; i < nums16; i++) 60 | { 61 | for (j = 0; j < 3; j++) 62 | virts16[i][j] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_S16); 63 | } 64 | 65 | int output_width, output_height; 66 | vxQueryImage(output, VX_IMAGE_WIDTH, &output_width, sizeof(int)); 67 | vxQueryImage(output, VX_IMAGE_HEIGHT, &output_height, sizeof(int)); 68 | 69 | /* Create temporary images */ 70 | vx_image image_remapped1[3], image_remapped2[3], 71 | image_weighted1[3], image_weighted2[3]; 72 | 73 | for(i = 0; i < 3; i++) 74 | { 75 | image_remapped1[i] = vxCreateImage(context, output_width, output_height, 76 | VX_DF_IMAGE_U8); 77 | image_remapped2[i] = vxCreateImage(context, output_width, output_height, 78 | VX_DF_IMAGE_U8); 79 | image_weighted1[i] = vxCreateImage(context, output_width, output_height, 80 | VX_DF_IMAGE_S16); 81 | image_weighted2[i] = vxCreateImage(context, output_width, output_height, 82 | VX_DF_IMAGE_S16); 83 | } 84 | 85 | 86 | /* Create the same processing subgraph for each channel */ 87 | enum vx_channel_e channels[] = {VX_CHANNEL_R, VX_CHANNEL_G, VX_CHANNEL_B}; 88 | 89 | float _scale = 1.0f/(1<<12); 90 | vx_scalar scale = vxCreateScalar(context, VX_TYPE_FLOAT32, &_scale); 91 | 92 | int _shift = 0; 93 | vx_scalar shift = vxCreateScalar(context, VX_TYPE_INT32, &_shift); 94 | 95 | for(i = 0; i < 3; i++) 96 | { 97 | /* First, extract input and logo R, G, and B channels to individual 98 | virtual images */ 99 | vxChannelExtractNode(graph, image1, channels[i], virtu8[0][i]); 100 | vxChannelExtractNode(graph, image2, channels[i], virtu8[1][i]); 101 | 102 | /* Add remap nodes */ 103 | vxRemapNode(graph, virtu8[0][i], remap1, VX_INTERPOLATION_BILINEAR, 104 | image_remapped1[i]); 105 | vxRemapNode(graph, virtu8[1][i], remap2, VX_INTERPOLATION_BILINEAR, 106 | image_remapped2[i]); 107 | 108 | /* add multiply nodes */ 109 | vxMultiplyNode(graph, image_remapped1[i], coeffs1, scale, 110 | VX_CONVERT_POLICY_SATURATE, VX_ROUND_POLICY_TO_NEAREST_EVEN, 111 | image_weighted1[i]); 112 | vxMultiplyNode(graph, image_remapped2[i], coeffs2, scale, 113 | VX_CONVERT_POLICY_SATURATE, VX_ROUND_POLICY_TO_NEAREST_EVEN, 114 | image_weighted2[i]); 115 | 116 | vxAddNode(graph, image_weighted1[i], image_weighted2[i], 117 | VX_CONVERT_POLICY_SATURATE, virts16[0][i]); 118 | 119 | /* convert from S16 to U8 */ 120 | vxConvertDepthNode(graph, virts16[0][i], virtu8[2][i], 121 | VX_CONVERT_POLICY_SATURATE, shift); 122 | 123 | vxConvertDepthNode(graph, image_weighted1[i], virtu8[3][i], 124 | VX_CONVERT_POLICY_SATURATE, shift); 125 | vxConvertDepthNode(graph, image_weighted2[i], virtu8[4][i], 126 | VX_CONVERT_POLICY_SATURATE, shift); 127 | } 128 | 129 | vxChannelCombineNode(graph, virtu8[2][0], virtu8[2][1], virtu8[2][2], 130 | NULL, output); 131 | vxChannelCombineNode(graph, image_remapped1[0], image_remapped1[1], 132 | image_remapped1[2], NULL, output_remapped1); 133 | vxChannelCombineNode(graph, image_remapped2[0], image_remapped2[1], 134 | image_remapped2[2], NULL, output_remapped2); 135 | vxChannelCombineNode(graph, virtu8[3][0], virtu8[3][1], 136 | virtu8[3][2], NULL, output_weighted1); 137 | vxChannelCombineNode(graph, virtu8[4][0], virtu8[4][1], 138 | virtu8[4][2], NULL, output_weighted2); 139 | 140 | for (i = 0; i < numu8; i++) 141 | for(j = 0; j < 3; j++) 142 | vxReleaseImage(&virtu8[i][j]); 143 | for (i = 0; i < nums16; ++i) 144 | for(j = 0; j < 3; j++) 145 | vxReleaseImage(&virts16[i][j]); 146 | 147 | for(i = 0; i < 3; i++) 148 | { 149 | vxReleaseImage(&image_remapped1[i]); 150 | vxReleaseImage(&image_remapped2[i]); 151 | vxReleaseImage(&image_weighted1[i]); 152 | vxReleaseImage(&image_weighted2[i]); 153 | } 154 | 155 | vxReleaseScalar(&scale); 156 | vxReleaseScalar(&shift); 157 | return graph; 158 | } 159 | 160 | void log_callback(vx_context context, vx_reference ref, 161 | vx_status status, const char* string) 162 | { 163 | printf("Log message: status %d, text: %s\n", (int)status, string); 164 | } 165 | 166 | int main(int argc, char **argv) 167 | { 168 | int i; 169 | 170 | if(argc != 5) 171 | { 172 | printf("stitch_debug \n"); 173 | return(-1); 174 | } 175 | 176 | const char* image1_filename = argv[1]; 177 | const char* image2_filename = argv[2]; 178 | const char* config_filename = argv[3]; 179 | const char* output_filename = argv[4]; 180 | 181 | vx_context context = vxCreateContext(); 182 | 183 | /* Read the input images*/ 184 | vx_image image1, image2; 185 | if(vxa_read_image(image1_filename, context, &image1) != 1) 186 | { 187 | printf("Error reading image 1\n"); 188 | return(-1); 189 | } 190 | if(vxa_read_image(image2_filename, context, &image2) != 1) 191 | { 192 | printf("Error reading image 2\n"); 193 | return(-1); 194 | } 195 | 196 | /* Read config images and remaps */ 197 | vx_image coeffs1, coeffs2; 198 | if(vxa_import_opencv_image(config_filename, "coeffs1", context, 199 | &coeffs1, NULL, NULL) != 1) 200 | { 201 | printf("Error reading coeffs1\n"); 202 | return(-1); 203 | } 204 | if(vxa_import_opencv_image(config_filename, "coeffs2", context, 205 | &coeffs2, NULL, NULL) != 1) 206 | { 207 | printf("Error reading coeffs2\n"); 208 | return(-1); 209 | } 210 | 211 | int width, height; 212 | vx_remap remap1, remap2; 213 | if(vxa_import_opencv_remap(config_filename, "remap1", context, &remap1, 214 | &width, &height) != 1) 215 | { 216 | printf("Error reading remap1\n"); 217 | return(-1); 218 | } 219 | if(vxa_import_opencv_remap(config_filename, "remap2", context, &remap2, 220 | NULL, NULL) != 1) 221 | { 222 | printf("Error reading remap2\n"); 223 | return(-1); 224 | } 225 | /* Create an output image */ 226 | vx_image output = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 227 | vx_image remapped1 = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 228 | vx_image remapped2 = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 229 | vx_image weighted1 = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 230 | vx_image weighted2 = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 231 | 232 | /* Create a graph */ 233 | vx_status status; 234 | vx_graph graph = makeFilterGraph(context, image1, image2, 235 | remap1, coeffs1, remap2, coeffs2, output, remapped1, remapped2, 236 | weighted1, weighted2); 237 | 238 | vxRegisterLogCallback(context, log_callback, vx_true_e); 239 | 240 | if((status = vxVerifyGraph(graph))) 241 | { 242 | printf("Graph verification failed, error code %d, %d\n", 243 | (int)status, (int)VX_ERROR_NOT_SUFFICIENT); 244 | } 245 | else if (vxProcessGraph(graph)) 246 | printf("Error processing graph\n"); 247 | else if (vxa_write_image(output, output_filename) != 1) 248 | printf("Problem writing the output image\n"); 249 | 250 | /* Save indermediate results */ 251 | vxa_write_image(remapped1, "remapped1.jpg"); 252 | vxa_write_image(remapped2, "remapped2.jpg"); 253 | vxa_write_image(weighted1, "weighted1.jpg"); 254 | vxa_write_image(weighted2, "weighted2.jpg"); 255 | 256 | vxReleaseImage(&image1); 257 | vxReleaseImage(&image2); 258 | vxReleaseRemap(&remap1); 259 | vxReleaseRemap(&remap2); 260 | vxReleaseImage(&coeffs1); 261 | vxReleaseImage(&coeffs2); 262 | vxReleaseImage(&output); 263 | vxReleaseImage(&remapped1); 264 | vxReleaseImage(&remapped2); 265 | vxReleaseImage(&weighted1); 266 | vxReleaseImage(&weighted2); 267 | 268 | vxReleaseContext(&context); 269 | } 270 | -------------------------------------------------------------------------------- /book_samples/stitch/stitch.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file stitch.c 26 | * \example stitch 27 | * \brief This OpenVX sample blends two images using predefined remap 28 | * transformations and blending coefficients (see homography-opencv.cpp) 29 | * \author Victor Erukhimov 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include "vxa/vxa.h" 36 | 37 | vx_graph makeFilterGraph(vx_context context, vx_image image1, vx_image image2, 38 | vx_remap remap1, vx_image coeffs1, vx_remap remap2, vx_image coeffs2, 39 | vx_image output) 40 | { 41 | /* Create virtual images */ 42 | const int numu8 = 5; 43 | vx_image virtu8[numu8][3]; 44 | const int nums16 = 3; 45 | vx_image virts16[nums16][3]; 46 | 47 | vx_graph graph = vxCreateGraph(context); 48 | 49 | int i, j; 50 | 51 | for(i = 0; i < numu8; i++) 52 | { 53 | for (j = 0; j < 3; j++) 54 | virtu8[i][j] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 55 | } 56 | for(i = 0; i < nums16; i++) 57 | { 58 | for (j = 0; j < 3; j++) 59 | virts16[i][j] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_S16); 60 | } 61 | 62 | /* Create the same processing subgraph for each channel */ 63 | enum vx_channel_e channels[] = {VX_CHANNEL_R, VX_CHANNEL_G, VX_CHANNEL_B}; 64 | 65 | float _scale = 1.0f/(1<<12); 66 | vx_scalar scale = vxCreateScalar(context, VX_TYPE_FLOAT32, &_scale); 67 | 68 | int _shift = 0; 69 | vx_scalar shift = vxCreateScalar(context, VX_TYPE_INT32, &_shift); 70 | 71 | for(i = 0; i < 3; i++) 72 | { 73 | /* First, extract input and logo R, G, and B channels to individual 74 | virtual images */ 75 | vxChannelExtractNode(graph, image1, channels[i], virtu8[0][i]); 76 | vxChannelExtractNode(graph, image2, channels[i], virtu8[1][i]); 77 | 78 | /* Add remap nodes */ 79 | vxRemapNode(graph, virtu8[0][i], remap1, VX_INTERPOLATION_BILINEAR, 80 | virtu8[3][i]); 81 | vxRemapNode(graph, virtu8[1][i], remap2, VX_INTERPOLATION_BILINEAR, 82 | virtu8[4][i]); 83 | 84 | /* add multiply nodes */ 85 | vxMultiplyNode(graph, virtu8[3][i], coeffs1, scale, 86 | VX_CONVERT_POLICY_SATURATE, VX_ROUND_POLICY_TO_NEAREST_EVEN, 87 | virts16[0][i]); 88 | vxMultiplyNode(graph, virtu8[4][i], coeffs2, scale, 89 | VX_CONVERT_POLICY_SATURATE, VX_ROUND_POLICY_TO_NEAREST_EVEN, 90 | virts16[1][i]); 91 | 92 | vxAddNode(graph, virts16[0][i], virts16[1][i], VX_CONVERT_POLICY_SATURATE, 93 | virts16[2][i]); 94 | 95 | /* convert from S16 to U8 */ 96 | vxConvertDepthNode(graph, virts16[2][i], virtu8[2][i], 97 | VX_CONVERT_POLICY_SATURATE, shift); 98 | } 99 | 100 | vxChannelCombineNode(graph, virtu8[2][0], virtu8[2][1], virtu8[2][2], 101 | NULL, output); 102 | 103 | for (i = 0; i < numu8; i++) 104 | for(j = 0; j < 3; j++) 105 | vxReleaseImage(&virtu8[i][j]); 106 | for (i = 0; i < nums16; ++i) 107 | for(j = 0; j < 3; j++) 108 | vxReleaseImage(&virts16[i][j]); 109 | 110 | vxReleaseScalar(&scale); 111 | vxReleaseScalar(&shift); 112 | return graph; 113 | } 114 | 115 | void log_callback(vx_context context, vx_reference ref, 116 | vx_status status, const char* string) 117 | { 118 | printf("Log message: status %d, text: %s\n", (int)status, string); 119 | } 120 | 121 | int main(int argc, char **argv) 122 | { 123 | if(argc != 5) 124 | { 125 | printf("stitch \n"); 126 | return(-1); 127 | } 128 | 129 | const char* image1_filename = argv[1]; 130 | const char* image2_filename = argv[2]; 131 | const char* config_filename = argv[3]; 132 | const char* output_filename = argv[4]; 133 | 134 | vx_context context = vxCreateContext(); 135 | 136 | /* Read the input images*/ 137 | vx_image image1, image2; 138 | if(vxa_read_image(image1_filename, context, &image1) != 1) 139 | { 140 | printf("Error reading image 1\n"); 141 | return(-1); 142 | } 143 | if(vxa_read_image(image2_filename, context, &image2) != 1) 144 | { 145 | printf("Error reading image 2\n"); 146 | return(-1); 147 | } 148 | 149 | /* Read config images and remaps */ 150 | vx_image coeffs1, coeffs2; 151 | if(vxa_import_opencv_image(config_filename, "coeffs1", context, 152 | &coeffs1, NULL, NULL) != 1) 153 | { 154 | printf("Error reading coeffs1\n"); 155 | return(-1); 156 | } 157 | if(vxa_import_opencv_image(config_filename, "coeffs2", context, 158 | &coeffs2, NULL, NULL) != 1) 159 | { 160 | printf("Error reading coeffs2\n"); 161 | return(-1); 162 | } 163 | 164 | int width, height; 165 | vx_remap remap1, remap2; 166 | if(vxa_import_opencv_remap(config_filename, "remap1", context, &remap1, 167 | &width, &height) != 1) 168 | { 169 | printf("Error reading remap1\n"); 170 | return(-1); 171 | } 172 | if(vxa_import_opencv_remap(config_filename, "remap2", context, &remap2, 173 | NULL, NULL) != 1) 174 | { 175 | printf("Error reading remap2\n"); 176 | return(-1); 177 | } 178 | /* Create an output image */ 179 | vx_image output = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 180 | 181 | /* Create a graph */ 182 | vx_status status; 183 | vx_graph graph = makeFilterGraph(context, image1, image2, 184 | remap1, coeffs1, remap2, coeffs2, output); 185 | 186 | vxRegisterLogCallback(context, log_callback, vx_true_e); 187 | 188 | if((status = vxVerifyGraph(graph))) 189 | { 190 | printf("Graph verification failed, error code %d, %d\n", 191 | (int)status, (int)VX_ERROR_NOT_SUFFICIENT); 192 | } 193 | else if (vxProcessGraph(graph)) 194 | printf("Error processing graph\n"); 195 | else if (vxa_write_image(output, output_filename) != 1) 196 | printf("Problem writing the output image\n"); 197 | vxReleaseContext(&context); 198 | } 199 | -------------------------------------------------------------------------------- /book_samples/tracking/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(tracking_example tracking_example.cpp centroid_tracking.c) 2 | target_link_libraries(tracking_example ${OpenCV_LIBS} ${OPENVX}) 3 | -------------------------------------------------------------------------------- /book_samples/tracking/centroid_tracking.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Stephen Ramm 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | #ifndef _CENTROID_TRACKING_H_ 25 | #define _CENTROID_TRACKING_H_ 26 | 27 | /*! 28 | * \file centroid_tracking.h 29 | * \example centroid_tracking 30 | * \brief Defines types and APIs for user nodes required for the centroid_tracking 31 | * example. 32 | * \author Stephen Ramm 33 | */ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #include "VX/vx.h" 40 | #include 41 | #include 42 | #include "readImage.h" 43 | #include "writeImage.h" 44 | 45 | #define ERROR_CHECK_STATUS( status ) { \ 46 | vx_status status_ = (status); \ 47 | if(status_ != VX_SUCCESS) { \ 48 | printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ 49 | exit(1); \ 50 | } \ 51 | } 52 | 53 | #define ERROR_CHECK_OBJECT( obj ) { \ 54 | vx_status status_ = vxGetStatus((vx_reference)(obj)); \ 55 | if(status_ != VX_SUCCESS) { \ 56 | printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ 57 | exit(1); \ 58 | } \ 59 | } 60 | 61 | enum user_library_e 62 | { 63 | USER_LIBRARY_EXAMPLE = 1, 64 | }; 65 | 66 | enum user_kernel_e 67 | { 68 | USER_KERNEL_INITIAL_CENTROID_CALCULATION = VX_KERNEL_BASE( VX_ID_DEFAULT, USER_LIBRARY_EXAMPLE ) + 0x004, 69 | USER_KERNEL_TRACK_CENTROIDS = VX_KERNEL_BASE( VX_ID_DEFAULT, USER_LIBRARY_EXAMPLE ) + 0x005, 70 | USER_KERNEL_CLEAR_OUTSIDE_BOUNDS = VX_KERNEL_BASE( VX_ID_DEFAULT, USER_LIBRARY_EXAMPLE ) + 0x006, 71 | }; 72 | 73 | typedef struct { 74 | vx_rectangle_t bounding_box; // The last calculated object bounding box 75 | vx_coordinates2df_t bb_centroid; // The original bounding box centroid 76 | vx_coordinates2df_t bb_std_dev; // The original bounding box standard deviation 77 | vx_coordinates2df_t spread_ratio; // The ratio of bounding box to features std.dev. 78 | vx_coordinates2df_t displacement; // The displacement of bounding box from features 79 | vx_coordinates2df_t bb_vector; // The rate of change in displacement of the bounding box 80 | vx_coordinates2df_t bb_zoom; // The rate of change in scale of the bounding box 81 | vx_uint32 num_corners; // The last valid number of features 82 | } userTrackingData; 83 | 84 | extern vx_enum user_struct_userTrackingData; 85 | 86 | vx_node intialCentroidCalculationNode( 87 | /* 88 | Create a node to perform the initial centroid tracking calculations. 89 | The initial data consists of detected features and an object bounding box. 90 | The output scalar "valid" is a boolean that determines if tracking can continue. 91 | We use the otherwise unsused "scale" and "orientation" fields of the vx_keypoint_t 92 | struct to record the x and y coordinates of the feature position relative to the 93 | original centroid of the object. 94 | */ 95 | vx_graph graph, 96 | vx_array bounding_box, // Object coordinates (input) 97 | vx_array corners, // Detected features (input) 98 | vx_array output_data, // Holds a userTrackingData (output) 99 | vx_array output_corners, // Output parameter of filtered features, must be same capacity as input array 100 | vx_scalar valid // Holds a vx_bool 101 | ); 102 | 103 | vx_node trackCentroidsNode( 104 | /* 105 | Create a node to perform the centroid tracking update calculation 106 | We caculate a new position and size for the bounding box, and also the 107 | rate of change of deisplacement and size. 108 | We reject any features that are not behaving correctly, and recalculate the ratio and displacement. 109 | We signal with an output boolean if tracking can continue, or if new features need to be found 110 | */ 111 | vx_graph graph, 112 | vx_array originals, // Original features in case we need to recalculate 113 | vx_array corners, // Input features 114 | vx_array input_data, // Holds a userTrackingData (input) 115 | vx_array output_data, // Holds a userTrackingData (output) 116 | vx_array output_corners, // Filtered features (output), same capacity as input array 117 | vx_scalar valid // Holds a vx_bool 118 | ); 119 | 120 | vx_node clearOutsideBoundsNode( 121 | /* 122 | Create a node to clear all pixels outside a bounding box 123 | */ 124 | vx_graph graph, 125 | vx_image input_image, 126 | vx_array bounds, // Holds a vx_rect (input) 127 | vx_image output_image 128 | ); 129 | 130 | vx_status registerCentroidNodes(vx_context context); 131 | #ifdef __cplusplus 132 | } 133 | #endif 134 | 135 | #endif /* _CENTROID_TRACKING_H_ */ 136 | -------------------------------------------------------------------------------- /book_samples/undistort/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(undistort undistort-remap.c) 2 | target_link_libraries(undistort ${OpenCV_LIBS} vxa)# ${OPENVX}) 3 | 4 | add_executable(undistortOpenCV undistortOpenCV.cpp) 5 | target_link_libraries(undistortOpenCV ${OpenCV_LIBS}) 6 | -------------------------------------------------------------------------------- /book_samples/undistort/undistort-remap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file undistort-remap.c 26 | * \example houghLines 27 | * \brief This OpenVX sample applies an undistort remap transformation computed by the 28 | * undistortOpenCV.cpp sample. 29 | * \author Victor Erukhimov 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | #include "vxa/vxa.h" 36 | 37 | vx_graph makeRemapGraph(vx_context context, vx_image input_image, 38 | vx_remap remap, vx_image output_image) 39 | { 40 | /* Create virtual images */ 41 | const int numu8 = 2; 42 | vx_image virtu8[numu8][3]; 43 | int i, j; 44 | 45 | vx_graph graph = vxCreateGraph(context); 46 | 47 | for(i = 0; i < numu8; i++) 48 | for (j = 0; j < 3; j++) 49 | virtu8[i][j] = vxCreateVirtualImage(graph, 0, 0, VX_DF_IMAGE_U8); 50 | 51 | /* Create the same processing subgraph for each channel */ 52 | enum vx_channel_e channels[] = {VX_CHANNEL_R, VX_CHANNEL_G, VX_CHANNEL_B}; 53 | 54 | for(i = 0; i < 3; i++) 55 | { 56 | /* First, extract input and logo R, G, and B channels to individual 57 | virtual images */ 58 | vxChannelExtractNode(graph, input_image, channels[i], virtu8[0][i]); 59 | 60 | /* Add remap nodes */ 61 | vxRemapNode(graph, virtu8[0][i], remap, VX_INTERPOLATION_BILINEAR, 62 | virtu8[1][i]); 63 | } 64 | 65 | vxChannelCombineNode(graph, virtu8[1][0], virtu8[1][1], virtu8[1][2], 66 | NULL, output_image); 67 | 68 | for (i = 0; i < numu8; i++) 69 | for(j = 0; j < 3; j++) 70 | vxReleaseImage(&virtu8[i][j]); 71 | 72 | return graph; 73 | } 74 | 75 | void log_callback(vx_context context, vx_reference ref, 76 | vx_status status, const char* string) 77 | { 78 | printf("Log message: status %d, text: %s\n", (int)status, string); 79 | } 80 | 81 | int main(int argc, char **argv) 82 | { 83 | if(argc != 4) 84 | { 85 | printf("undistort \n"); 86 | return(-1); 87 | } 88 | 89 | const char* remap_filename = argv[1]; 90 | const char* image_filename = argv[2]; 91 | const char* output_filename = argv[3]; 92 | 93 | vx_context context = vxCreateContext(); 94 | 95 | /* Read the input images*/ 96 | vx_image input_image; 97 | if(vxa_read_image(image_filename, context, &input_image) != 1) 98 | { 99 | printf("Error reading image 1\n"); 100 | return(-1); 101 | } 102 | 103 | int width, height; 104 | vx_remap remap; 105 | if(vxa_import_opencv_remap(remap_filename, "remap", context, &remap, 106 | &width, &height) != 1) 107 | { 108 | printf("Error reading remap1\n"); 109 | return(-1); 110 | } 111 | 112 | /* Create an output image */ 113 | vx_image output_image = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); 114 | 115 | /* Create a graph */ 116 | vx_status status; 117 | vx_graph graph = makeRemapGraph(context, input_image, 118 | remap, output_image); 119 | 120 | vxRegisterLogCallback(context, log_callback, vx_true_e); 121 | 122 | if((status = vxVerifyGraph(graph))) 123 | { 124 | printf("Graph verification failed, error code %d, %d\n", 125 | (int)status, (int)VX_ERROR_NOT_SUFFICIENT); 126 | } 127 | else if (vxProcessGraph(graph)) 128 | printf("Error processing graph\n"); 129 | else if (vxa_write_image(output_image, output_filename) != 1) 130 | printf("Problem writing the output image\n"); 131 | vxReleaseContext(&context); 132 | } 133 | -------------------------------------------------------------------------------- /book_samples/undistort/undistortOpenCV.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019 Victor Erukhimov 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file undistortOpenCV.cpp 26 | * \example undistortOpenCV 27 | * \brief This OpenCV sample creates a remap transformation that undistorts 28 | * an input image. 29 | * \author Victor Erukhimov 30 | */ 31 | 32 | #include "opencv2/opencv.hpp" 33 | 34 | using namespace cv; 35 | 36 | int main(int argc, char** argv) 37 | { 38 | if(argc != 5) 39 | { 40 | printf("undistortOpenCV \n"); 41 | return(0); 42 | } 43 | 44 | const char* camera_file = argv[1]; 45 | const char* in_fname = argv[2]; 46 | const char* out_fname = argv[3]; 47 | const char* map_fname = argv[4]; 48 | 49 | FileStorage fs(camera_file, FileStorage::READ); 50 | Mat intrinsic_params, dist_coeffs; 51 | fs["camera_matrix"] >> intrinsic_params; 52 | fs["distortion_coefficients"] >> dist_coeffs; 53 | int width, height; 54 | fs["image_width"] >> width; 55 | fs["image_height"] >> height; 56 | 57 | printf("Read width = %d, height = %d\n", width, height); 58 | std::cout << intrinsic_params << std::endl; 59 | std::cout << dist_coeffs << std::endl; 60 | 61 | Mat map1, map2, new_camera; 62 | initUndistortRectifyMap(intrinsic_params, dist_coeffs, Mat(), 63 | intrinsic_params, Size(width, height), CV_32FC2, map1, map2); 64 | 65 | printf("Completed undistort map\n"); 66 | 67 | FileStorage fs1(map_fname, FileStorage::WRITE); 68 | fs1 << "remap" << map1; 69 | fs1 << "remap_src_width" << width; 70 | fs1 << "remap_src_height" << height; 71 | fs1 << "remap_dst_width" << width; 72 | fs1 << "remap_dst_height" << height; 73 | 74 | // now apply the remap to the input image 75 | Mat input_image = imread(in_fname); 76 | Mat output_image(input_image.cols, input_image.rows, input_image.type()); 77 | remap(input_image, output_image, map1, Mat(), INTER_LINEAR); 78 | imwrite(out_fname, output_image); 79 | } 80 | -------------------------------------------------------------------------------- /scripts/clean-all.sh: -------------------------------------------------------------------------------- 1 | # remove all downloaded and intermediate files 2 | # 3 | \rm -rf build-* tutorial_exercises/CMakeLists.txt.user tutorial_videos tutorial_exercises/amdovx-core 4 | -------------------------------------------------------------------------------- /scripts/download-amdovx.sh: -------------------------------------------------------------------------------- 1 | # download and extract amdovx-core/openvx into tutorial_exercises folder 2 | # 3 | \rm -rf master.zip tutorial_exercises/amdovx-core 4 | wget https://github.com/GPUOpen-ProfessionalCompute-Libraries/amdovx-core/archive/master.zip 5 | echo creating tutorial_exercises/amdovx-core ... 6 | unzip -q master.zip 7 | mv amdovx-core-master tutorial_exercises/amdovx-core 8 | \rm -rf master.zip 9 | -------------------------------------------------------------------------------- /scripts/download-khronos-sample.sh: -------------------------------------------------------------------------------- 1 | # download and extract openvx_sample into tutorial_exercises folder 2 | # 3 | \rm -rf openvx_sample_1.1.tar.bz2 tutorial_exercises/openvx_sample 4 | wget https://www.khronos.org/registry/vx/sample/openvx_sample_1.1.tar.bz2 5 | echo creating tutorial_exercises/openvx_sample ... 6 | bunzip2 -c openvx_sample_1.1.tar.bz2 | tar xf - 7 | mv openvx_sample tutorial_exercises/openvx_sample 8 | \rm -rf openvx_sample_1.1.tar.bz2 9 | -------------------------------------------------------------------------------- /scripts/download-videos.sh: -------------------------------------------------------------------------------- 1 | # run this script from openvx_tutorial folder; the current C code requires 2 | # tutorial_videos folder to be under the openvx_tutorial folder to be able 3 | # pick the default video sequence when launching from Qt Creator 4 | # 5 | \rm -rf tutorial_videos 6 | mkdir tutorial_videos 7 | cd tutorial_videos 8 | wget http://ewh.ieee.org/r6/scv/sps/openvx-material/PETS09-S1-L1-View001.avi 9 | cd .. 10 | -------------------------------------------------------------------------------- /scripts/qt-clean.sh: -------------------------------------------------------------------------------- 1 | # remove Qt cmake intermediate files; run this script from openvx_tutorial folder 2 | # 3 | \rm -rf build-* tutorial_exercises/CMakeLists.txt.user 4 | -------------------------------------------------------------------------------- /tutorial_exercises/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # 24 | # Supported cmake variables and CACHE entries: 25 | # OpenVX_SOURCE_DIR -- OpenVX open source folder with CMakeLists.txt 26 | # OpenVX_LIBS -- list of OpenVX libraries 27 | # OpenVX_LIBS_DIR -- path to OpenVX libraries 28 | # OpenVX_INCLUDE_DIRS -- path to non-khronos OpenVX header files (optional) 29 | # ENABLE_OPENCL -- flag to enable OpenCL with OpenVX source build (optional) 30 | # ENABLE_NN_AMD -- flag to enable Neural Network extension with AMD ROCm (optional) 31 | # DISABLE_DISPLAY -- flag to display OpenCV windows 32 | # 33 | # Here are few examples: 34 | # * Build exerciese using an open source implementation with using OpenCL and NN 35 | # % cmake -DENABLE_NN_AMD=TRUE \ 36 | # .../openvx_tutorial/tutorial_exercises 37 | # * Build exerciese using an open source implementation without using OpenCL 38 | # % cmake -DOpenVX_SOURCE_DIR=amdovx-modules/deps/amdovx-core/openvx \ 39 | # -DCMAKE_DISABLE_FIND_PACKAGE_OpenCL=TRUE \ 40 | # .../openvx_tutorial/tutorial_exercises 41 | # * Build exerciese using a 3rd-party OpenVX library 42 | # % cmake -DOpenVX_LIBS_DIR= \ 43 | # -DOpenVX_LIBS= \ 44 | # .../openvx_tutorial/tutorial_exercises 45 | 46 | cmake_minimum_required ( VERSION 2.8 ) 47 | project ( tutorial_exercises ) 48 | 49 | if( POLICY CMP0054 ) 50 | cmake_policy( SET CMP0054 OLD ) 51 | endif() 52 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 53 | set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) 54 | else() 55 | set ( CMAKE_CXX_STANDARD 11 ) 56 | set ( CMAKE_MACOSX_RPATH ON ) 57 | endif() 58 | 59 | # by default, use open-source OpenVX without OpenCL 60 | if( ENABLE_NN_AMD ) 61 | set ( OpenVX_LIBS openvx vx_nn CACHE INTERNAL OpenVX_LIBS ) 62 | set ( OpenVX_LIBS_DIR /opt/rocm/lib CACHE INTERNAL OpenVX_LIBS_DIR ) 63 | elseif( NOT OpenVX_LIBS_DIR ) 64 | if( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" ) 65 | EXEC_PROGRAM(cat ARGS "/proc/cpuinfo" OUTPUT_VARIABLE CPUINFO) 66 | if( ${CPUINFO} MATCHES "sse4_2" ) 67 | set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available on host") 68 | else() 69 | set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host") 70 | endif() 71 | elseif( ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" ) 72 | EXEC_PROGRAM("/usr/sbin/sysctl -n machdep.cpu.features" OUTPUT_VARIABLE CPUINFO) 73 | if( ${CPUINFO} MATCHES "SSE4.2" ) 74 | set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available on host") 75 | else() 76 | set(SSE4_2_FOUND false CACHE BOOL "SSE4.2 available on host") 77 | endif() 78 | else() 79 | set(SSE4_2_FOUND true CACHE BOOL "SSE4.2 available on host") 80 | endif() 81 | if( SSE4_2_FOUND ) 82 | set ( OpenVX_SOURCE_DIR amdovx-modules/deps/amdovx-core/openvx CACHE INTERNAL OpenVX_SOURCE_DIR ) 83 | if( EXISTS ${CMAKE_SOURCE_DIR}/${OpenVX_SOURCE_DIR} ) 84 | set ( OpenVX_LIBS openvx CACHE INTERNAL OpenVX_LIBS ) 85 | if( NOT ENABLE_OPENCL ) 86 | set ( CMAKE_DISABLE_FIND_PACKAGE_OpenCL TRUE CACHE INTERNAL CMAKE_DISABLE_FIND_PACKAGE_OpenCL ) 87 | else() 88 | set ( CMAKE_DISABLE_FIND_PACKAGE_OpenCL FALSE CACHE INTERNAL CMAKE_DISABLE_FIND_PACKAGE_OpenCL ) 89 | endif() 90 | endif() 91 | endif( SSE4_2_FOUND ) 92 | endif() 93 | 94 | if( OpenVX_SOURCE_DIR AND EXISTS ${CMAKE_SOURCE_DIR}/${OpenVX_SOURCE_DIR} ) 95 | if( NOT OpenVX_LIBS ) 96 | set ( OpenVX_LIBS openvx CACHE INTERNAL OpenVX_LIBS ) 97 | endif() 98 | elseif( NOT OpenVX_LIBS_DIR ) 99 | set ( OpenVX_SAMPLE /home/openvx/openvx_sample/install/Linux/x64 ) 100 | set ( OpenVX_LIBS openvx vxu CACHE INTERNAL OpenVX_LIBS ) 101 | if( CMAKE_BUILD_TYPE MATCHES ".*Deb.*" ) 102 | set ( OpenVX_LIBS_DIR ${OpenVX_SAMPLE}/Debug/bin CACHE INTERNAL OpenVX_LIBPATH ) 103 | else( CMAKE_BUILD_TYPE MATCHES ".*Deb.*" ) 104 | set ( OpenVX_LIBS_DIR ${OpenVX_SAMPLE}/Release/bin CACHE INTERNAL OpenVX_LIBPATH ) 105 | endif( CMAKE_BUILD_TYPE MATCHES ".*Deb.*" ) 106 | endif() 107 | 108 | if( ENABLE_OPENCL OR ENABLE_NN_AMD ) 109 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 110 | find_package(OpenCL REQUIRED) 111 | endif() 112 | 113 | if( OpenVX_SOURCE_DIR AND EXISTS ${CMAKE_SOURCE_DIR}/${OpenVX_SOURCE_DIR} ) 114 | add_subdirectory ( ${OpenVX_SOURCE_DIR} ) 115 | endif() 116 | if( DISABLE_DISPLAY ) 117 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_DISPLAY=0") 118 | endif() 119 | add_subdirectory ( exercise1 ) 120 | add_subdirectory ( solution_exercise1 ) 121 | add_subdirectory ( exercise2 ) 122 | add_subdirectory ( solution_exercise2 ) 123 | if( ENABLE_NN_AMD ) 124 | add_subdirectory ( exercise3 ) 125 | add_subdirectory ( solution_exercise3 ) 126 | add_subdirectory ( exercise4 ) 127 | add_subdirectory ( solution_exercise4 ) 128 | elseif( OpenVX_SOURCE_DIR AND EXISTS ${CMAKE_SOURCE_DIR}/${OpenVX_SOURCE_DIR} ) 129 | add_subdirectory ( exercise3 ) 130 | add_subdirectory ( solution_exercise3 ) 131 | if( ENABLE_OPENCL ) 132 | add_subdirectory ( exercise4 ) 133 | add_subdirectory ( solution_exercise4 ) 134 | endif() 135 | endif() 136 | -------------------------------------------------------------------------------- /tutorial_exercises/cmake/FindOpenCL.cmake: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 3 | # MIT License 4 | # 5 | # Copyright (c) 2017 Advanced Micro Devices, Inc. 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy 8 | # of this software and associated documentation files (the "Software"), to deal 9 | # in the Software without restriction, including without limitation the rights 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | # copies of the Software, and to permit persons to whom the Software is 12 | # furnished to do so, subject to the following conditions: 13 | # 14 | # The above copyright notice and this permission notice shall be included in all 15 | # copies or substantial portions of the Software. 16 | # 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | # SOFTWARE. 24 | # 25 | ################################################################################ 26 | find_path(OPENCL_INCLUDE_DIRS 27 | NAMES OpenCL/cl.h CL/cl.h 28 | HINTS 29 | ${OPENCL_ROOT}/include 30 | $ENV{AMDAPPSDKROOT}/include 31 | $ENV{CUDA_PATH}/include 32 | PATHS 33 | /usr/include 34 | /usr/local/include 35 | /usr/local/cuda/include 36 | /opt/cuda/include 37 | /opt/rocm/opencl/include 38 | DOC "OpenCL header file path" 39 | ) 40 | mark_as_advanced( OPENCL_INCLUDE_DIRS ) 41 | 42 | if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") 43 | find_library( OPENCL_LIBRARIES 44 | NAMES OpenCL 45 | HINTS 46 | ${OPENCL_ROOT}/lib 47 | $ENV{AMDAPPSDKROOT}/lib 48 | $ENV{CUDA_PATH}/lib 49 | DOC "OpenCL dynamic library path" 50 | PATH_SUFFIXES x86_64 x64 x86_64/sdk 51 | PATHS 52 | /usr/lib 53 | /usr/local/cuda/lib 54 | /opt/cuda/lib 55 | /opt/rocm/opencl/lib 56 | ) 57 | else( ) 58 | find_library( OPENCL_LIBRARIES 59 | NAMES OpenCL 60 | HINTS 61 | ${OPENCL_ROOT}/lib 62 | $ENV{AMDAPPSDKROOT}/lib 63 | $ENV{CUDA_PATH}/lib 64 | DOC "OpenCL dynamic library path" 65 | PATH_SUFFIXES x86 Win32 66 | 67 | PATHS 68 | /usr/lib 69 | /usr/local/cuda/lib 70 | /opt/cuda/lib 71 | ) 72 | endif( ) 73 | mark_as_advanced( OPENCL_LIBRARIES ) 74 | 75 | include( FindPackageHandleStandardArgs ) 76 | find_package_handle_standard_args( OPENCL DEFAULT_MSG OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS ) 77 | 78 | set(OpenCL_FOUND ${OPENCL_FOUND} CACHE INTERNAL "") 79 | set(OpenCL_LIBRARIES ${OPENCL_LIBRARIES} CACHE INTERNAL "") 80 | set(OpenCL_INCLUDE_DIRS ${OPENCL_INCLUDE_DIRS} CACHE INTERNAL "") 81 | 82 | if( NOT OPENCL_FOUND ) 83 | message( STATUS "FindOpenCL looked for libraries named: OpenCL" ) 84 | endif() 85 | -------------------------------------------------------------------------------- /tutorial_exercises/exercise1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | link_directories ( ${OpenVX_LIBS_DIR} ) 33 | if( POLICY CMP0054 ) 34 | cmake_policy( SET CMP0054 OLD ) 35 | endif() 36 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 37 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 38 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 39 | endif() 40 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 41 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 42 | -------------------------------------------------------------------------------- /tutorial_exercises/exercise2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | link_directories ( ${OpenVX_LIBS_DIR} ) 33 | if( POLICY CMP0054 ) 34 | cmake_policy( SET CMP0054 OLD ) 35 | endif() 36 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 37 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 38 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 39 | endif() 40 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 41 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 42 | -------------------------------------------------------------------------------- /tutorial_exercises/exercise3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | include_directories ( ${CMAKE_SOURCE_DIR}/amdovx-modules/deps/amdovx-core/openvx/include ) 33 | link_directories ( ${OpenVX_LIBS_DIR} ) 34 | if( POLICY CMP0054 ) 35 | cmake_policy( SET CMP0054 OLD ) 36 | endif() 37 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 38 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 39 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 40 | endif() 41 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 42 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 43 | -------------------------------------------------------------------------------- /tutorial_exercises/exercise4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | include_directories ( ${CMAKE_SOURCE_DIR}/amdovx-modules/deps/amdovx-core/openvx/include ) 33 | link_directories ( ${OpenVX_LIBS_DIR} ) 34 | if( POLICY CMP0054 ) 35 | cmake_policy( SET CMP0054 OLD ) 36 | endif() 37 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 38 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 39 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 40 | endif() 41 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 42 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 43 | -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2012-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _OPENVX_H_ 19 | #define _OPENVX_H_ 20 | 21 | /*! 22 | * \file 23 | * \brief The top level OpenVX Header. 24 | */ 25 | 26 | /*! \brief Defines the length of the implementation name string, including the trailing zero. 27 | * \ingroup group_context 28 | */ 29 | #define VX_MAX_IMPLEMENTATION_NAME (64) 30 | 31 | /*! \brief Defines the length of a kernel name string to be added to OpenVX, including the trailing zero. 32 | * \ingroup group_kernel 33 | */ 34 | #define VX_MAX_KERNEL_NAME (256) 35 | 36 | /*! \brief Defines the length of a message buffer to copy from the log, including the trailing zero. 37 | * \ingroup group_basic_features 38 | */ 39 | #define VX_MAX_LOG_MESSAGE_LEN (1024) 40 | 41 | /*! \brief Defines the length of the reference name string, including the trailing zero. 42 | * \ingroup group_reference 43 | * \see vxSetReferenceName 44 | */ 45 | #define VX_MAX_REFERENCE_NAME (64) 46 | 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | /*! \brief Defines the major version number macro. 54 | * \ingroup group_basic_features 55 | */ 56 | #define VX_VERSION_MAJOR(x) ((x & 0xFF) << 8) 57 | 58 | /*! \brief Defines the minor version number macro. 59 | * \ingroup group_basic_features 60 | */ 61 | #define VX_VERSION_MINOR(x) ((x & 0xFF) << 0) 62 | 63 | /*! \brief Defines the predefined version number for 1.0. 64 | * \ingroup group_basic_features 65 | */ 66 | #define VX_VERSION_1_0 (VX_VERSION_MAJOR(1) | VX_VERSION_MINOR(0)) 67 | 68 | /*! \brief Defines the predefined version number for 1.1. 69 | * \ingroup group_basic_features 70 | */ 71 | #define VX_VERSION_1_1 (VX_VERSION_MAJOR(1) | VX_VERSION_MINOR(1)) 72 | 73 | /*! \brief Defines the predefined version number for 1.2. 74 | * \ingroup group_basic_features 75 | */ 76 | #define VX_VERSION_1_2 (VX_VERSION_MAJOR(1) | VX_VERSION_MINOR(2)) 77 | 78 | /*! \brief Defines the OpenVX Version Number. 79 | * \ingroup group_basic_features 80 | */ 81 | #define VX_VERSION VX_VERSION_1_2 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx_khr_class.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2012-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _VX_KHR_CLASSIFIER_H_ 19 | #define _VX_KHR_CLASSIFIER_H_ 20 | 21 | /*! 22 | * \file 23 | * \brief The Khronos Extension for general classification. 24 | * 25 | */ 26 | 27 | #define OPENVX_KHR_CLASS "vx_khr_class" 28 | 29 | #include 30 | 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | /*! \brief The Classifier Extension Library Set 36 | * \ingroup group_classifier 37 | */ 38 | #define VX_LIBRARY_KHR_CLASS_EXTENSION (0x2) 39 | /*! \brief The list of Classifier Extension Kernels. 40 | * \ingroup group_classifier 41 | */ 42 | enum vx_kernel_nn_ext_e { 43 | /*! \brief The Classifier Extension scan kernel. 44 | * \see group_classifier 45 | */ 46 | VX_KERNEL_SCAN_CLASSIFIER = VX_KERNEL_BASE(VX_ID_KHRONOS, VX_LIBRARY_KHR_CLASS_EXTENSION) + 0x0, 47 | }; 48 | 49 | /*! \brief Classifier Extension type enums. 50 | * \ingroup group_classifier 51 | */ 52 | enum vx_class_enum_e 53 | { 54 | VX_ENUM_CLASSIFIER_MODEL= 0x1E, /*!< \brief Classifier model */ 55 | }; 56 | 57 | /*! 58 | * \brief classification model to be used in \ref vxScanClassifierNode. 59 | * The classification models are loadable by undefined binary format see \ref vxImportClassifierModel. 60 | * Extensions will be added to the specification, to support a defined binary format. 61 | * \ingroup group_object_classifier_model 62 | */ 63 | typedef struct _vx_classifier_model* vx_classifier_model; 64 | 65 | /*! \brief Classifier model format enums. 66 | * In the main specification only undefined binary format is supported. Extensions to the specification will be added in order to support specific binary format. 67 | * \ingroup group_object_classifier_model 68 | */ 69 | enum vx_classifier_model_format_e 70 | { 71 | /*! \brief Undefined binary format. 72 | * Using this enumeration will result in an implementation defined behaviour. 73 | */ 74 | VX_CLASSIFIER_MODEL_UNDEFINED = VX_ENUM_BASE( VX_ID_KHRONOS, VX_ENUM_CLASSIFIER_MODEL ) + 0x0, 75 | }; 76 | /*! \brief The type enumeration lists all classifier extension types. 77 | * \ingroup group_object_classifier_model 78 | */ 79 | enum vx_classifier_type_e { 80 | VX_TYPE_CLASSIFER_MODEL = 0x02C,/*!< \brief A \ref vx_classifier_model. type */ 81 | }; 82 | 83 | /*============================================================================== 84 | CLASSIFIER MODEL 85 | =============================================================================*/ 86 | /*! 87 | * \brief Creates an opaque reference classifier model 88 | * This function creates a classifier model to be used in \ref vxScanClassifierNode. The object classifier object is a read-only constant object. It cannot be changed during graph execution. 89 | * \param [in] context Reference to the context where to create the ClassifierModel. 90 | * \param [in] format The binary format which contain the classifier model. See \ref vx_classifier_model_format_e. Currently only undefined binary format is supported. 91 | * Extensions will be added to the specification, to support a classification model defined binary format. 92 | * \param [in] ptr A memory pointer to the binary format. 93 | * \param [in] length size in bytes of binary format data. 94 | * \returns A ClassifierModel reference \ref vx_classifier_model. Any possible errors preventing a 95 | * successful creation should be checked using \ref vxGetStatus. 96 | * \ingroup group_object_classifier_model 97 | */ 98 | VX_API_ENTRY vx_classifier_model vxImportClassifierModel(vx_context context, vx_enum format, const vx_uint8* ptr, vx_size length); 99 | 100 | /*! 101 | * \brief Releases a reference of an ClassifierModel object. 102 | * The object may not be garbage collected until its total reference and its contained objects 103 | * count is zero. After returning from this function the reference is zeroed/cleared. 104 | * \param [in] model The pointer to the ClassifierModel to release. 105 | * \return A \ref vx_status_e enumeration. 106 | * \retval \ref VX_SUCCESS No errors; all other values indicate failure 107 | * \retval * An error occurred. See vx_status_e. 108 | * \ingroup group_object_classifier_model 109 | */ 110 | VX_API_ENTRY vx_status vxReleaseClassifierModel(vx_classifier_model* model); 111 | 112 | /*! \brief [Graph] Scans a feature-map (input_feature_map) and detect the classification for each scan-window. 113 | * \param [in] graph The reference to the graph 114 | * \param [in] input_feature_map The Feature-map, example is the output of \ref vxHOGFeaturesNode. 115 | * \param [in] model The pre-trained model loaded. Loaded using \ref vxImportClassifierModel 116 | * \param [in] scan_window_width Width of the scan window 117 | * \param [in] scan_window_height Height of the scan window 118 | * \param [in] step_x Horizontal step-size (along x-axis) 119 | * \param [in] step_y Vertical step-size (along y-axis) 120 | * \param [out] object_confidences [Optional] An array of confidences measure, the measure is of type \ref VX_TYPE_UINT16. The confidence measure is defined by the extensions which define classification model with defined binary format. 121 | * This output can be used as class index as well. In case we detect several different classes in single execution. The output will be an array of indexes of the classes. 122 | * \param [out] object_rectangles An array of object positions, in \ref VX_TYPE_RECTANGLE 123 | * \param [out] num_objects [optional] The number of object detected in a \ref VX_SIZE scalar 124 | * \note The border mode \ref VX_NODE_BORDER value \ref VX_BORDER_UNDEFINED is supported. 125 | * \ingroup group_vision_function_classifier 126 | * \return \ref vx_node. 127 | * \retval vx_node A node reference. Any possible errors preventing a successful creation should be checked using \ref vxGetStatus 128 | */ 129 | 130 | VX_API_ENTRY vx_node vxScanClassifierNode(vx_graph graph,vx_tensor input_feature_map, vx_classifier_model model, vx_int32 scanwindow_width, vx_int32 scanwindow_height, vx_int32 step_x, vx_int32 step_y, 131 | vx_array object_confidences, vx_array object_rectangles, vx_scalar num_objects); 132 | 133 | 134 | #ifdef __cplusplus 135 | } 136 | #endif 137 | 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx_khr_icd.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2017-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | /*! \file 19 | * \defgroup group_icd OpenVX ICD Loader API 20 | * \brief The OpenVX Installable Client Driver (ICD) Loader API. 21 | * \details The vx_khr_icd extension provides a mechanism for vendors to implement Installable Client Driver (ICD) for OpenVX. The OpenVX ICD Loader API provides a mechanism for applications to access these vendor implementations. 22 | */ 23 | 24 | #ifndef _VX_KHR_ICD_H_ 25 | #define _VX_KHR_ICD_H_ 26 | 27 | #include 28 | #include 29 | 30 | /*! \brief Platform handle of an implementation. 31 | * \ingroup group_icd 32 | */ 33 | typedef struct _vx_platform * vx_platform; 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /*! \brief Queries list of available platforms. 40 | * \param [in] capacity Maximum number of items that platform[] can hold. 41 | * \param [out] platform[] List of platform handles. 42 | * \param [out] pNumItems Number of platform handles returned. 43 | * \return A \ref vx_status_e enumeration. 44 | * \retval VX_SUCCESS No errors. 45 | * \retval VX_FAILURE If no platforms are found. 46 | * \ingroup group_icd 47 | */ 48 | vx_status VX_API_CALL vxIcdGetPlatforms(vx_size capacity, vx_platform platform[], vx_size * pNumItems); 49 | 50 | /*! \brief Queries the platform for some specific information. 51 | * \param [in] platform The platform handle. 52 | * \param [in] attribute The attribute to query. Use one of the following: 53 | * \ref VX_CONTEXT_VENDOR_ID, 54 | * \ref VX_CONTEXT_VERSION, 55 | * \ref VX_CONTEXT_EXTENSIONS_SIZE, 56 | * \ref VX_CONTEXT_EXTENSIONS. 57 | * \param [out] ptr The location at which to store the resulting value. 58 | * \param [in] size The size in bytes of the container to which \a ptr points. 59 | * \return A \ref vx_status_e enumeration. 60 | * \retval VX_SUCCESS No errors. 61 | * \retval VX_ERROR_INVALID_REFERENCE If the platform is not a \ref vx_platform. 62 | * \retval VX_ERROR_INVALID_PARAMETERS If any of the other parameters are incorrect. 63 | * \retval VX_ERROR_NOT_SUPPORTED If the attribute is not supported on this implementation. 64 | * \ingroup group_icd 65 | */ 66 | vx_status VX_API_CALL vxQueryPlatform(vx_platform platform, vx_enum attribute, void *ptr, vx_size size); 67 | 68 | /*! \brief Creates a \ref vx_context from a \ref vx_platform. 69 | * \details This creates a top-level object context for OpenVX from a platform handle. 70 | * \returns The reference to the implementation context \ref vx_context. Any possible errors 71 | * preventing a successful creation should be checked using \ref vxGetStatus. 72 | * \ingroup group_icd 73 | */ 74 | vx_context VX_API_CALL vxCreateContextFromPlatform(vx_platform platform); 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx_khr_ix.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2012-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | #ifndef _OPENVX_IMPORT_EXPORT_H_ 18 | #define _OPENVX_IMPORT_EXPORT_H_ 19 | 20 | /*! 21 | * \file 22 | * \brief The OpenVX Export and Import extension API. 23 | */ 24 | 25 | #define OPENVX_KHR_IX "vx_khr_ix" 26 | 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /*============================================================================= 34 | Export to host memory 35 | =============================================================================*/ 36 | 37 | /*! \brief Exports selected objects to memory in a vendor-specific format.\n 38 | * 39 | * \details A list of references in the given context is supplied to this function, and all information 40 | * required to re-create these is stored in memory in such a way that those objects may be re-created 41 | * with the corresponding import function, according to the usage specified by the *uses* parameter[*REQ*].\n 42 | * The information must be context independent in that it may be written to external storage for later 43 | * retreival with another instantiation of a compatible implementation[*REQ*].\n 44 | * The list of objects to export may contain only valid references (i.e. vxGetStatus() will return VX_SUCCESS) 45 | * to vx_graph and non-virtual data objects or the function will fail[*REQ*]. 46 | * (Specifically not vx_context, vx_import, vx_node, vx_kernel, vx_parameter or vx_meta_format)\n 47 | * Some node creation functions take C parameters rather than OpenVX data objects (such as the *gradient_size* 48 | * parameter of \ref vxHarrisCornersNode that is provided as a vx_int32), because these are intended 49 | * to be fixed at node creation time; nevertheless OpenVX data objects may be assigned to them, for example if 50 | * the \ref vxCreateGenericNode API is used. 51 | * A data object corresponding to a node parameter that is intended to be fixed at node creation time must not be 52 | * in the list of exported objects nor attached as a graph parameter or the export operation will fail[*REQ*].\n 53 | * The *uses* array specifies how the objects in the corresponding *refs* array will be exported. A data object 54 | * will always have its meta-data (e.g. dimensions and format of an image) exported, and optionally 55 | * may have its data (e.g. pixel values) exported, and additionally you can decide whether the importing 56 | * application will create data objects to replace those attached to graphs, or if the implementation will 57 | * automatically create them: 58 | * - \ref VX_IX_USE_APPLICATION_CREATE \n 59 | * Export sufficient data to check that an application-supplied 60 | * object is compatible when the data is later imported[*REQ*]. 61 | * \note This value must be given for images created from handles, or the the export operation 62 | * will fail[*REQ*] 63 | * - \ref VX_IX_USE_EXPORT_VALUES\n 64 | * Export complete information (for example image data or value of a 65 | * scalar)[*REQ*]. 66 | * - \ref VX_IX_USE_NO_EXPORT_VALUES\n 67 | * Export meta-data only; the importing application will set values 68 | * as applicable[*REQ*] 69 | * 70 | * The values in *uses* are applicable only for data objects and are ignored for vx_graph objects[*REQ*].\n 71 | * If the list *refs* contains vx_graph objects, these graphs will be verified during the export operation and the export operation will fail if verification fails; when successfully exported graphs are subsequently imported they will appear as verified [*REQ*].\n 72 | * \note The implementation may also choose to re-verify any previously verified graphs and apply 73 | * optimisations based upon which references are to be exported and how.\n 74 | * Any data objects attached to a graph that are hidden, i.e. their references are not in the list *refs*, 75 | * may be treated by the implementation as virtual objects, since they can never be visible when the graph is 76 | * subsequently imported.\n 77 | * Note that imported graphs cannot become unverified. Attempts to change the 78 | * graph that might normally cause the graph to be unverified, e.g. calling 79 | * vxSetGraphParameterByIndex with an object with different metadata, will fail.\n 80 | * The implementation should make sure that all permissible changes of exported objects are possible 81 | * without re-verification. For example: 82 | * - A uniform image may be swapped for a non-uniform image, so corresponding optimisations should be 83 | * inhibited if a uniform image appears in the *refs* list 84 | * - An image that is a region of interest of another image may be similarly replaced by any other image of 85 | * matching size and format, and vice-versa 86 | * 87 | * If a graph is exported that has delays registered for auto-aging, then this information is also 88 | * exported[*REQ*].\n 89 | * If the function is called with NULL for any of its parameters, this is an error [*REQ*].\n 90 | * The reference counts of objects as visible to the calling application will not be affected 91 | * by calling this function [*REQ*].\n 92 | * The export operation will fail if more than one object whose reference is listed at *refs* 93 | * has been given the same non-zero length name (via \ref vxSetReferenceName)[*REQ*].\n 94 | * If a graph listed for export has any graph parameters not listed at *refs*, then the 95 | * export operation will fail[*REQ*]. 96 | * \note The order of the references supplied in the *refs* array will be the order in which the 97 | * framwork will supply references for the corresponding import operation with \ref vxImportObjectsFromMemory.\n 98 | * The same length of *uses* array, containing the same values, and the same value of *numrefs*, must be supplied 99 | * for the corresponding import operation. 100 | * 101 | * For objects not listed in *refs*, the following rules apply: 102 | * 1. In any one graph, if an object is not connected as an output of a node in a graph being exported 103 | * then its data values will be exported (for subsequent import)[*REQ*]. 104 | * 2. Where the object in (1) is a composite object (such as a pyramid) then rule (1) applies to 105 | * all of its sub-objects[*REQ*]. 106 | * 3. Where the object in (1) is a sub-object such as a region of interest, and the composite object 107 | * (in this case the parent image) does not meet the conditions of rule (1), then rule (1) applies 108 | * to the sub-object only[*REQ*]. 109 | * \param [in] context context from which to export objects, must be valid [*REQ*]. 110 | * \param [in] numrefs number of references to export [*REQ*]. 111 | * \param [in] refs references to export. This is an array of length numrefs populated with 112 | * the references to export[*REQ*]. 113 | * \param [in] uses how to export the references. This is an array of length numrefs containing 114 | * values as described above[*REQ*]. 115 | * \param [out] ptr returns pointer to binary buffer. On error this is set to NULL[*REQ*]. 116 | * \param [out] length number of bytes at \*ptr. On error this is set to zero[*REQ*]. 117 | * \return A \ref vx_status value. 118 | * \retval VX_SUCCESS If no errors occurred and the objects were sucessfully exported[*REQ*]. 119 | * An error is indicated when the return value is not VX_SUCCESS.\n 120 | * An implementation may provide several different return values to give useful diagnostic 121 | * information in the event of failure to export, but these are not required to indicate 122 | * possible recovery mechanisms, and for safety critical use assume errors are not recoverable. 123 | * \post \ref vxReleaseExportedMemory is used to deallocate the memory. 124 | * \ingroup group_import 125 | */ 126 | 127 | VX_API_ENTRY vx_status VX_API_CALL vxExportObjectsToMemory( 128 | vx_context context, 129 | vx_size numrefs, 130 | const vx_reference *refs, 131 | const vx_enum * uses, 132 | const vx_uint8 ** ptr, 133 | vx_size * length); 134 | 135 | /*! \brief Releases memory allocated for a binary export when it is no longer required. 136 | * \details This function releases memory allocated by \ref vxExportObjectsToMemory[*REQ*]. 137 | * \param [in] context The context for which \ref vxExportObjectsToMemory was called[*REQ*]. 138 | * \param [in,out] ptr A pointer previously set by calling \ref vxExportObjectsToMemory[*REQ*]. 139 | * The function will fail if *ptr does not contain an address of memory previously 140 | * allocated by \ref vxExportObjectsToMemory[*REQ*]. 141 | * \post After returning from sucessfully from this function \*ptr is set to NULL[*REQ*]. 142 | * \return A \ref vx_status value. 143 | * \retval VX_SUCCESS If no errors occurred and the memory was sucessfully released[*REQ*].\n 144 | * An error is indicated when the return value is not VX_SUCCESS[*REQ*].\n 145 | * An implementation may provide several different return values to give useful diagnostic 146 | * information in the event of failure to export, but these are not required to indicate 147 | * possible recovery mechanisms, and for safety critical use assume errors are not recoverable. 148 | * \pre \ref vxExportObjectsToMemory is used to allocate the memory. 149 | * \ingroup group_import 150 | */ 151 | VX_API_ENTRY vx_status VX_API_CALL vxReleaseExportedMemory( 152 | vx_context context, const vx_uint8 ** ptr); 153 | 154 | #ifdef __cplusplus 155 | } 156 | #endif 157 | 158 | #endif -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx_khr_xml.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2012-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _VX_KHR_XML_H_ 19 | #define _VX_KHR_XML_H_ 20 | 21 | /*! \file 22 | * \brief The OpenVX XML Schema Extension Header. 23 | * 24 | * \defgroup group_xml Extension: XML API 25 | * \brief The Khronos Extension for OpenVX XML Import and Export Support. 26 | */ 27 | 28 | #define OPENVX_KHR_XML "vx_khr_xml" 29 | 30 | #include 31 | 32 | /*! \brief The Object Type Enumeration for Imports. 33 | * \ingroup group_xml 34 | */ 35 | enum vx_ext_import_type_e { 36 | VX_TYPE_IMPORT = 0x814,/*!< \brief A \ref vx_import */ 37 | }; 38 | 39 | /*! \brief The import type enumeration. 40 | * \ingroup group_xml 41 | * \see VX_IMPORT_ATTRIBUTE_TYPE 42 | */ 43 | enum vx_ext_import_types_e { 44 | VX_IMPORT_TYPE_XML = 0,/*!< \brief The XML import type */ 45 | }; 46 | 47 | /*! \brief The import attributes list 48 | * \ingroup group_xml 49 | * \see vxQueryImport 50 | */ 51 | enum vx_import_attribute_e { 52 | /*! \brief Returns the number of references in the import object. Use a \ref vx_uint32 parameter.*/ 53 | VX_IMPORT_ATTRIBUTE_COUNT = VX_ATTRIBUTE_BASE(VX_ID_KHRONOS, VX_TYPE_IMPORT) + 0x0, 54 | /*! \brief Returns the type of import. Use a \ref vx_ext_import_types_e parameter */ 55 | VX_IMPORT_ATTRIBUTE_TYPE = VX_ATTRIBUTE_BASE(VX_ID_KHRONOS, VX_TYPE_IMPORT) + 0x1, 56 | }; 57 | 58 | /*! \brief An abstract handle to an import object. 59 | * \ingroup group_xml 60 | * \extends vx_reference 61 | */ 62 | typedef struct _vx_import *vx_import; 63 | 64 | 65 | #ifdef __cplusplus 66 | extern "C" { 67 | #endif 68 | 69 | /*! \brief Exports all objects in the context to an XML file which uses the OpenVX 70 | * XML Schema. 71 | * \param [in] context The context to export. 72 | * \param [in] xmlfile The file name to write the XML into. 73 | * \note The reference numbers contained in the xml file can appear in any order but 74 | * should be inclusive from index number 0 to [number of references - 1]. For example, 75 | * if there are 20 references in the xml file, none of the reference indices should be >= 20. 76 | * \return A \ref vx_status_e enumeration. 77 | * \see https://www.khronos.org/registry/vx/schema/openvx-1-1.xsd 78 | * \ingroup group_xml 79 | */ 80 | VX_API_ENTRY vx_status VX_API_CALL vxExportToXML(vx_context context, vx_char xmlfile[]); 81 | 82 | 83 | /*! \brief Imports all framework and data objects from an XML file into the given context. 84 | * \param [in] context The context to import into. 85 | * \param [in] xmlfile The XML file to read. 86 | * \note The reference indices in the import object corresponds with the reference numbers in the 87 | * XML file. It is assumed that the program has some means to know which references to use from 88 | * imported list (either by name: \ref vxGetImportReferenceByName, or by index from looking at the XML 89 | * file (debug use case): \ref vxGetImportReferenceByIndex). Alternativly, the program can use 90 | * \ref vxGetImportReferenceByIndex in a loop and query each one to understand what was imported. After 91 | * all references of interest have been retrieved, this import obects should be released using 92 | * \ref vxReleaseImport. 93 | * \return \ref vx_import object containing references to the imported objects in the context 94 | * \see https://www.khronos.org/registry/vx/schema/openvx-1-1.xsd 95 | * \ingroup group_xml 96 | */ 97 | VX_API_ENTRY vx_import VX_API_CALL vxImportFromXML(vx_context context, vx_char xmlfile[]); 98 | 99 | /*! \brief Used to retrieve a reference by name from the import when the name is known beforehand. If 100 | * multiple references have the same name, then *any* one of them may be returned. 101 | * \param [in] import The reference to the import object. 102 | * \param [in] name The reference string name. 103 | * \return \ref vx_reference 104 | * \retval 0 Invalid import object or name does not match a reference in the import object. 105 | * \retval * The reference matching the requested name. 106 | * \note Use \ref vxReleaseReference to release the reference before releasing the context. 107 | * \pre \ref vxImportFromXML 108 | * \ingroup group_xml 109 | */ 110 | VX_API_ENTRY vx_reference VX_API_CALL vxGetImportReferenceByName(vx_import import, const vx_char *name); 111 | 112 | /*! \brief Used to retrieve a reference by the index from the import. 113 | * \param [in] import The reference to the import object. 114 | * \param [in] index The index of the reference in the import object to return. 115 | * \return \ref vx_reference 116 | * \retval 0 Invalid import object or index. 117 | * \retval * The reference at the requested index number. 118 | * \note Use \ref vxQueryImport with \ref VX_IMPORT_ATTRIBUTE_COUNT to retrieve 119 | * the upper limit of references in the import. 120 | * \note Use \ref vxReleaseReference to release the reference before releasing the context. 121 | * \pre \ref vxImportFromXML 122 | * \ingroup group_xml 123 | */ 124 | VX_API_ENTRY vx_reference VX_API_CALL vxGetImportReferenceByIndex(vx_import import, vx_uint32 index); 125 | 126 | /*! \brief Used to query the import about its properties. 127 | * \param [in] import The reference to the import object. 128 | * \param [in] attribute The \ref vx_import_attribute_e value to query for. 129 | * \param [out] ptr The location at which the resulting value will be stored. 130 | * \param [in] size The size of the container to which ptr points. 131 | * \return A \ref vx_status_e enumeration. 132 | * \pre \ref vxImportFromXML 133 | * \ingroup group_xml 134 | */ 135 | VX_API_ENTRY vx_status VX_API_CALL vxQueryImport(vx_import import, vx_enum attribute, void *ptr, vx_size size); 136 | 137 | /*! \brief Releases a reference to an import object. 138 | * Also internally releases its references to its imported objects. These 139 | * imported objects may not be garbage collected until their total reference 140 | * counts are zero. 141 | * \param [in] import The pointer to the import object to release. 142 | * \return A \ref vx_status_e enumeration. 143 | * \retval VX_SUCCESS No errors. 144 | * \retval VX_ERROR_INVALID_REFERENCE If import is not a \ref vx_import. 145 | * \note After returning from this function the reference will be zeroed. 146 | * \pre \ref vxImportFromXML 147 | * \ingroup group_xml 148 | */ 149 | VX_API_ENTRY vx_status VX_API_CALL vxReleaseImport(vx_import *import); 150 | 151 | #ifdef __cplusplus 152 | } 153 | #endif 154 | 155 | #endif 156 | 157 | -------------------------------------------------------------------------------- /tutorial_exercises/include/VX/vx_vendors.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | * Copyright (c) 2012-2017 The Khronos Group Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef _OPENVX_VENDORS_H_ 19 | #define _OPENVX_VENDORS_H_ 20 | 21 | /*! 22 | * \file 23 | * \brief The Vendor ID list for OpenVX. 24 | */ 25 | 26 | /*! \brief The Vendor ID of the Implementation. As new vendors submit their 27 | * implementations, this enumeration will grow. 28 | * \ingroup group_basic_features 29 | */ 30 | enum vx_vendor_id_e { 31 | VX_ID_KHRONOS = 0x000, /*!< \brief The Khronos Group */ 32 | VX_ID_TI = 0x001, /*!< \brief Texas Instruments, Inc. */ 33 | VX_ID_QUALCOMM = 0x002, /*!< \brief Qualcomm, Inc. */ 34 | VX_ID_NVIDIA = 0x003, /*!< \brief NVIDIA Corporation */ 35 | VX_ID_ARM = 0x004, /*!< \brief ARM Ltd. */ 36 | VX_ID_BDTI = 0x005, /*!< \brief Berkley Design Technology, Inc. */ 37 | VX_ID_RENESAS = 0x006, /*!< \brief Renasas Electronics */ 38 | VX_ID_VIVANTE = 0x007, /*!< \brief Vivante Corporation */ 39 | VX_ID_XILINX = 0x008, /*!< \brief Xilinx Inc. */ 40 | VX_ID_AXIS = 0x009, /*!< \brief Axis Communications */ 41 | VX_ID_MOVIDIUS = 0x00A, /*!< \brief Movidius Ltd. */ 42 | VX_ID_SAMSUNG = 0x00B, /*!< \brief Samsung Electronics */ 43 | VX_ID_FREESCALE = 0x00C, /*!< \brief Freescale Semiconductor */ 44 | VX_ID_AMD = 0x00D, /*!< \brief Advanced Micro Devices */ 45 | VX_ID_BROADCOM = 0x00E, /*!< \brief Broadcom Corporation */ 46 | VX_ID_INTEL = 0x00F, /*!< \brief Intel Corporation */ 47 | VX_ID_MARVELL = 0x010, /*!< \brief Marvell Technology Group Ltd. */ 48 | VX_ID_MEDIATEK = 0x011, /*!< \brief MediaTek, Inc. */ 49 | VX_ID_ST = 0x012, /*!< \brief STMicroelectronics */ 50 | VX_ID_CEVA = 0x013, /*!< \brief CEVA DSP */ 51 | VX_ID_ITSEEZ = 0x014, /*!< \brief Itseez, Inc. */ 52 | VX_ID_IMAGINATION=0x015, /*!< \brief Imagination Technologies */ 53 | VX_ID_NXP = 0x016, /*!< \brief NXP Semiconductors */ 54 | VX_ID_VIDEANTIS = 0x017, /*!< \brief Videantis */ 55 | VX_ID_SYNOPSYS = 0x018, /*!< \brief Synopsys */ 56 | VX_ID_CADENCE = 0x019, /*!< \brief Cadence */ 57 | VX_ID_HUAWEI = 0x01A, /*!< \brief Huawei */ 58 | VX_ID_SOCIONEXT = 0x01B, /*!< \brief Socionext */ 59 | /* Add new vendor code above this line */ 60 | VX_ID_USER = 0xFFE, /*!< \brief For use by vxAllocateUserKernelId and vxAllocateUserKernelLibraryId */ 61 | VX_ID_MAX = 0xFFF, 62 | /*! \brief For use by all Kernel authors until they can obtain an assigned ID. */ 63 | VX_ID_DEFAULT = VX_ID_MAX, 64 | }; 65 | 66 | #endif 67 | 68 | -------------------------------------------------------------------------------- /tutorial_exercises/include/opencv_camera_display.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 The Khronos Group Inc. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a 5 | * copy of this software and/or associated documentation files (the 6 | * "Materials"), to deal in the Materials without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Materials, and to 9 | * permit persons to whom the Materials are furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included 13 | * in all copies or substantial portions of the Materials. 14 | * 15 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 | * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 22 | */ 23 | 24 | /*! 25 | * \file opencv_camera_display.h 26 | * \brief wrapper for OpenCV camera/file-input and display 27 | * \author Radhakrishna Giduthuri 28 | */ 29 | 30 | #ifndef __opencv_camera_display_h__ 31 | #define __opencv_camera_display_h__ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #ifndef DEFAULT_VIDEO_SEQUENCE 38 | #define DEFAULT_VIDEO_SEQUENCE "../../tutorial_videos/PETS09-S1-L1-View001.avi" 39 | #endif 40 | 41 | #ifndef DEFAULT_WAITKEY_DELAY 42 | #define DEFAULT_WAITKEY_DELAY 1 /* waitKey delay time in milliseconds after each frame processing */ 43 | #endif 44 | 45 | #ifndef ENABLE_DISPLAY 46 | #define ENABLE_DISPLAY 1 /* display results using OpenCV GUI */ 47 | #endif 48 | 49 | class CGuiModule 50 | { 51 | public: 52 | CGuiModule( const char * captureFile ) 53 | : m_cap( captureFile ? captureFile : DEFAULT_VIDEO_SEQUENCE ) 54 | { 55 | captureFile = captureFile ? captureFile : DEFAULT_VIDEO_SEQUENCE; 56 | m_windowName = captureFile; 57 | if( !m_cap.isOpened()) 58 | { 59 | printf( "ERROR: unable to open: %s\n", captureFile ); 60 | exit( 1 ); 61 | } 62 | printf( "OK: FILE %s %dx%d\n", captureFile, GetWidth(), GetHeight()); 63 | #if ENABLE_DISPLAY 64 | cv::namedWindow(m_windowName); 65 | #endif 66 | } 67 | 68 | CGuiModule( int captureDevice ) 69 | : m_cap( captureDevice ) 70 | { 71 | char name[64]; sprintf( name, "CAMERA#%d", captureDevice ); 72 | m_windowName = name; 73 | if( !m_cap.isOpened()) 74 | { 75 | printf( "ERROR: CAMERA#%d not available\n", captureDevice ); 76 | exit( 1 ); 77 | } 78 | printf( "OK: CAMERA#%d %dx%d\n", captureDevice, GetWidth(), GetHeight()); 79 | #if ENABLE_DISPLAY 80 | cv::namedWindow(m_windowName); 81 | #endif 82 | } 83 | 84 | int GetWidth() 85 | { 86 | return (int) m_cap.get( cv::CAP_PROP_FRAME_WIDTH ); 87 | } 88 | 89 | int GetHeight() 90 | { 91 | #if 1 // TBD: workaround for reported OpenCV+Windows bug that returns width instead of height 92 | return 480; 93 | #else 94 | return (int) m_cap.get( cv::CAP_PROP_FRAME_HEIGHT ); 95 | #endif 96 | } 97 | 98 | int GetStride() 99 | { 100 | return (int) m_imgRGB.step; 101 | } 102 | 103 | unsigned char * GetBuffer() 104 | { 105 | return m_imgRGB.data; 106 | } 107 | 108 | bool Grab() 109 | { 110 | m_cap >> m_imgBGR; 111 | if( m_imgBGR.empty() ) 112 | { 113 | return false; 114 | } 115 | cv::cvtColor( m_imgBGR, m_imgRGB, cv::COLOR_BGR2RGB ); 116 | return true; 117 | } 118 | 119 | void DrawText( int x, int y, const char * text ) 120 | { 121 | cv::putText( m_imgBGR, text, cv::Point( x, y ), 122 | cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cv::Scalar( 128, 0, 0 ), 1, cv::LineTypes::LINE_AA ); 123 | #if !ENABLE_DISPLAY 124 | printf("text: %s\n", text); 125 | #endif 126 | } 127 | 128 | void DrawPoint( int x, int y ) 129 | { 130 | cv::Point center( x, y ); 131 | cv::circle( m_imgBGR, center, 1, cv::Scalar( 0, 0, 255 ), 2 ); 132 | } 133 | 134 | void DrawArrow( int x0, int y0, int x1, int y1 ) 135 | { 136 | DrawPoint( x0, y0 ); 137 | float dx = (float) ( x1 - x0 ), dy = (float) ( y1 - y0 ), arrow_len = sqrtf( dx * dx + dy * dy ); 138 | if(( arrow_len >= 3.0f ) && ( arrow_len <= 50.0f ) ) 139 | { 140 | cv::Scalar color = cv::Scalar( 0, 255, 255 ); 141 | float tip_len = 5.0f + arrow_len * 0.1f, angle = atan2f( dy, dx ); 142 | cv::line( m_imgBGR, cv::Point( x0, y0 ), cv::Point( x1, y1 ), color, 1 ); 143 | 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 ); 144 | 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 ); 145 | } 146 | } 147 | 148 | void Show() 149 | { 150 | #if ENABLE_DISPLAY 151 | cv::imshow( m_windowName, m_imgBGR ); 152 | #endif 153 | } 154 | 155 | bool AbortRequested() 156 | { 157 | char key = cv::waitKey( DEFAULT_WAITKEY_DELAY ); 158 | if( key == ' ' ) 159 | { 160 | key = cv::waitKey( 0 ); 161 | } 162 | if(( key == 'q' ) || ( key == 'Q' ) || ( key == 27 ) /*ESC*/ ) 163 | { 164 | return true; 165 | } 166 | return false; 167 | } 168 | 169 | void WaitForKey() 170 | { 171 | #if ENABLE_DISPLAY 172 | cv::waitKey( 0 ); 173 | #endif 174 | } 175 | 176 | protected: 177 | std::string m_windowName; 178 | cv::VideoCapture m_cap; 179 | cv::Mat m_imgBGR; 180 | cv::Mat m_imgRGB; 181 | }; 182 | 183 | #endif 184 | -------------------------------------------------------------------------------- /tutorial_exercises/solution_exercise1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | link_directories ( ${OpenVX_LIBS_DIR} ) 33 | if( POLICY CMP0054 ) 34 | cmake_policy( SET CMP0054 OLD ) 35 | endif() 36 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 37 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 38 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 39 | endif() 40 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 41 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 42 | -------------------------------------------------------------------------------- /tutorial_exercises/solution_exercise2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | link_directories ( ${OpenVX_LIBS_DIR} ) 33 | if( POLICY CMP0054 ) 34 | cmake_policy( SET CMP0054 OLD ) 35 | endif() 36 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 37 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 38 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 39 | endif() 40 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 41 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 42 | -------------------------------------------------------------------------------- /tutorial_exercises/solution_exercise3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | include_directories ( ${CMAKE_SOURCE_DIR}/amdovx-modules/deps/amdovx-core/openvx/include ) 33 | link_directories ( ${OpenVX_LIBS_DIR} ) 34 | if( POLICY CMP0054 ) 35 | cmake_policy( SET CMP0054 OLD ) 36 | endif() 37 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 38 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 39 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 40 | endif() 41 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 42 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 43 | -------------------------------------------------------------------------------- /tutorial_exercises/solution_exercise4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 The Khronos Group Inc. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a 4 | # copy of this software and/or associated documentation files (the 5 | # "Materials"), to deal in the Materials without restriction, including 6 | # without limitation the rights to use, copy, modify, merge, publish, 7 | # distribute, sublicense, and/or sell copies of the Materials, and to 8 | # permit persons to whom the Materials are furnished to do so, subject to 9 | # the following conditions: 10 | # 11 | # The above copyright notice and this permission notice shall be included 12 | # in all copies or substantial portions of the Materials. 13 | # 14 | # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 21 | 22 | # Author: Radhakrishna Giduthuri (radha.giduthuri@ieee.org) 23 | # Assumes that there is only one .cpp file with same name as project folder name 24 | 25 | cmake_minimum_required ( VERSION 2.8 ) 26 | get_filename_component ( project_dir ${CMAKE_CURRENT_LIST_DIR} NAME ) 27 | project ( ${project_dir} ) 28 | find_package ( OpenCV REQUIRED ) 29 | include_directories ( ${OpenCV_INCLUDE_DIRS} ) 30 | include_directories ( ${OpenVX_INCLUDE_DIRS} ) 31 | include_directories ( ${CMAKE_SOURCE_DIR}/include ) 32 | include_directories ( ${CMAKE_SOURCE_DIR}/amdovx-modules/deps/amdovx-core/openvx/include ) 33 | link_directories ( ${OpenVX_LIBS_DIR} ) 34 | if( POLICY CMP0054 ) 35 | cmake_policy( SET CMP0054 OLD ) 36 | endif() 37 | if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" ) 38 | set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" ) 39 | set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" ) 40 | endif() 41 | add_executable ( ${PROJECT_NAME} ${project_dir}.cpp ) 42 | target_link_libraries ( ${PROJECT_NAME} ${OpenVX_LIBS} ${OpenCV_LIBRARIES} ) 43 | -------------------------------------------------------------------------------- /tutorial_exercises/vgg16-nnef-openvx/README.md: -------------------------------------------------------------------------------- 1 | # How to convert VGG16 tensorflow model to NNEF and run inference on OpenVX. 2 | 3 | ## Pre-requisites: 4 | * [Tensorflow](https://www.tensorflow.org/) installed. 5 | * Neural Network extension module of [amdovx-modules](https://github.com/GPUOpen-ProfessionalCompute-Libraries/amdovx-modules) installed. 6 | 7 | ## Steps to run VGG-16 example: 8 | 9 | First,clone the following repository: 10 | 11 | ``` 12 | % git clone -b cl/vgg16 https://github.com/lcskrishna/NNEF-Tools.git 13 | % mkdir exercise; cd exercise 14 | ``` 15 | 16 | ### Where to find VGG 16 sample model? 17 | A sample VGG-16 model can be found here : [Link](http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz) 18 | Save the model into exercise folder and untar the model. 19 | 20 | ### How to convert tensorflow model to NNEF? 21 | ``` 22 | % python /converter/tensorflow/src/vgg16_export.py --ckpt-file 23 | ``` 24 | 25 | This generates a folder named vgg_16-nnef that contains a binary folder and a graph.nnef file 26 | 27 | ### How to generate OpenVX code from NNEF? 28 | 29 | Navigate to the executables that are obtained after installing amdovx-modules, and execute the following command to generate OpenVX inference code. 30 | 31 | ``` 32 | % nnef2openvx vgg_16-nnef openx_vgg16 33 | ``` 34 | 35 | This generates a folder named openvx_vgg16 where OpenVX inference code is generated. 36 | 37 | ### How to execute the OpenVX inference code? 38 | 39 | Firstly, convert an image to a FP32 tensor as shown in the example of [ReadMe](https://github.com/GPUOpen-ProfessionalCompute-Libraries/amdovx-modules/tree/develop/vx_nn). Name it as input.f32 40 | Execute the following commands: 41 | 42 | ``` 43 | % cd openvx_vgg16 ; mkdir build; cd build 44 | % cmake .. ; make 45 | % ./anntest ../weights.bin input.f32 output.f32 46 | ``` 47 | 48 | This generates a output tensor. Now, use this tensor to post-process the data, by adding an argmax layer and identifying the classification that is generated. 49 | You can use argmax.cpp file to get the classification label. 50 | 51 | ``` 52 | % g++ argmax.c++ -o argmax 53 | % ./argmax output.f32 labels.txt 54 | ``` 55 | 56 | This prints the label classified to an image. 57 | -------------------------------------------------------------------------------- /tutorial_exercises/vgg16-nnef-openvx/vgg16_export.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2017 The Khronos Group Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | import tensorflow as tf 17 | import numpy as np 18 | import tf2nnef # this is required for exporting to NNEF 19 | 20 | from tensorflow.contrib.slim.python.slim.nets.vgg import vgg_16 # only needed for the example 21 | import argparse 22 | 23 | parser = argparse.ArgumentParser() 24 | parser.add_argument('--ckpt-filepath', type = str, required = True, help = 'Path the checkpoint file (.ckpt)') 25 | 26 | args = parser.parse_args() 27 | 28 | input_shape = [1, 224, 224, 3] 29 | 30 | 31 | # define network here 32 | # function must not take any params 33 | # inputs must be defined via placeholders 34 | # outputs must be returned as a tuple 35 | # only parts of the graph which lead to returned outputs will be exported 36 | def network(): 37 | input = tf.placeholder(dtype=tf.float32, name='input', shape=input_shape) 38 | net, end_points = vgg_16(input, spatial_squeeze = False, num_classes=1000, is_training=False) 39 | return net 40 | 41 | 42 | checkpoint = args.ckpt_filepath # replace with checkpoint file name if you also want to export weights and activations 43 | #checkpoint = None 44 | 45 | 46 | # provide network builder method and checkpoint to exporter 47 | # returns a list of tensors that may be further used 48 | converter = tf2nnef.export_network(network, checkpoint) 49 | 50 | if checkpoint is not None: 51 | # must feed palceholders created in network definition with some input data 52 | tf2nnef.export_activations(converter, checkpoint, feed_dict={'input:0': np.random.random(input_shape)}) 53 | --------------------------------------------------------------------------------