diff --git a/amd_openvx_extensions/amd_opencv/include/vx_ext_opencv.h b/amd_openvx_extensions/amd_opencv/include/vx_ext_opencv.h index 8e92518eab..5156d26ac6 100644 --- a/amd_openvx_extensions/amd_opencv/include/vx_ext_opencv.h +++ b/amd_openvx_extensions/amd_opencv/include/vx_ext_opencv.h @@ -22,7 +22,6 @@ THE SOFTWARE. #include "VX/vx.h" -#include "vx_opencv.h" #include #include "opencv2/opencv.hpp" diff --git a/samples/C Samples/Canny/CMakeLists.txt b/samples/C Samples/Canny/CMakeLists.txt new file mode 100644 index 0000000000..ebe329147e --- /dev/null +++ b/samples/C Samples/Canny/CMakeLists.txt @@ -0,0 +1,37 @@ +################################################################################ +# +# MIT License +# +# Copyright (c) 2018 Advanced Micro Devices, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +################################################################################ + +cmake_minimum_required (VERSION 2.8) +project (cannyDetect) +set (CMAKE_CXX_STANDARD 11) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +find_package(OpenCL REQUIRED) +find_package(OpenCV REQUIRED) +include_directories (/opt/rocm/mivisionx/include) +link_directories (/opt/rocm/mivisionx/lib) +add_executable(cannyDetect canny.cpp) +target_link_libraries(${PROJECT_NAME} openvx ${OpenCV_LIBRARIES}) + diff --git a/samples/C Samples/Canny/canny.cpp b/samples/C Samples/Canny/canny.cpp new file mode 100644 index 0000000000..decd62648e --- /dev/null +++ b/samples/C Samples/Canny/canny.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + +using namespace cv; +using namespace std; + +#define ERROR_CHECK_STATUS( status ) { \ + vx_status status_ = (status); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +#define ERROR_CHECK_OBJECT( obj ) { \ + vx_status status_ = vxGetStatus((vx_reference)(obj)); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]) +{ + size_t len = strlen(string); + if (len > 0) { + printf("%s", string); + if (string[len - 1] != '\n') + printf("\n"); + fflush(stdout); + } +} + +int main(int argc, char **argv) +{ + if (argc < 2) { + printf("Usage:\n" + "./cannyDetect --image \n" + "./cannyDetect --live \n"); + return 0; + } + + int width = 480, height = 360; + + vx_context context = vxCreateContext(); + ERROR_CHECK_OBJECT(context); + vxRegisterLogCallback(context, log_callback, vx_false_e); + + vx_graph graph = vxCreateGraph(context); + ERROR_CHECK_OBJECT(graph); + + vx_image input_rgb_image = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); + vx_image output_filtered_image = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(input_rgb_image); + ERROR_CHECK_OBJECT(output_filtered_image); + + vx_image yuv_image = vxCreateVirtualImage(graph, width, height, VX_DF_IMAGE_IYUV); + vx_image luma_image = vxCreateVirtualImage(graph, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(yuv_image); + ERROR_CHECK_OBJECT(luma_image); + + vx_threshold hyst = vxCreateThreshold(context, VX_THRESHOLD_TYPE_RANGE, VX_TYPE_UINT8); + vx_int32 lower = 80, upper = 100; + vxSetThresholdAttribute(hyst, VX_THRESHOLD_ATTRIBUTE_THRESHOLD_LOWER, &lower, sizeof(lower)); + vxSetThresholdAttribute(hyst, VX_THRESHOLD_ATTRIBUTE_THRESHOLD_UPPER, &upper, sizeof(upper)); + ERROR_CHECK_OBJECT(hyst); + vx_int32 gradient_size = 3; + + vx_node nodes[] = + { + vxColorConvertNode(graph, input_rgb_image, yuv_image), + vxChannelExtractNode(graph, yuv_image, VX_CHANNEL_Y, luma_image), + vxCannyEdgeDetectorNode(graph, luma_image, hyst, gradient_size, VX_NORM_L1, output_filtered_image) + }; + + for( vx_size i = 0; i < sizeof( nodes ) / sizeof( nodes[0] ); i++ ) + { + ERROR_CHECK_OBJECT( nodes[i] ); + ERROR_CHECK_STATUS( vxReleaseNode( &nodes[i] ) ); + } + + ERROR_CHECK_STATUS( vxVerifyGraph( graph ) ); + + string option = argv[1]; + Mat input; + + if (option == "--image") { + input = imread(argv[2]); + if (input.empty()) { + printf("Image not found\n"); + return 0; + } + resize(input, input, Size(width, height)); + imshow("inputWindow", input); + vx_rectangle_t cv_rgb_image_region; + cv_rgb_image_region.start_x = 0; + cv_rgb_image_region.start_y = 0; + cv_rgb_image_region.end_x = width; + cv_rgb_image_region.end_y = height; + vx_imagepatch_addressing_t cv_rgb_image_layout; + cv_rgb_image_layout.stride_x = 3; + cv_rgb_image_layout.stride_y = input.step; + vx_uint8 * cv_rgb_image_buffer = input.data; + ERROR_CHECK_STATUS( vxCopyImagePatch( input_rgb_image, &cv_rgb_image_region, 0, + &cv_rgb_image_layout, cv_rgb_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + vx_rectangle_t rect = { 0, 0, (vx_uint32)width, (vx_uint32)height }; + vx_map_id map_id; + vx_imagepatch_addressing_t addr; + void * ptr; + ERROR_CHECK_STATUS( vxMapImagePatch( output_filtered_image, &rect, 0, &map_id, &addr, &ptr, + VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X ) ); + Mat mat( height, width, CV_8U, ptr, addr.stride_y ); + imshow( "CannyDetect", mat ); + waitKey(0); + ERROR_CHECK_STATUS( vxUnmapImagePatch( output_filtered_image, map_id ) ); + } + else if (option == "--live") { + VideoCapture cap(0); + if (!cap.isOpened()) { + printf("Unable to open camera\n"); + return 0; + } + for(;;) { + cap >> input; + resize(input, input, Size(width, height)); + imshow("inputWindow", input); + if(waitKey(30) >= 0) break; + vx_rectangle_t cv_rgb_image_region; + cv_rgb_image_region.start_x = 0; + cv_rgb_image_region.start_y = 0; + cv_rgb_image_region.end_x = width; + cv_rgb_image_region.end_y = height; + vx_imagepatch_addressing_t cv_rgb_image_layout; + cv_rgb_image_layout.stride_x = 3; + cv_rgb_image_layout.stride_y = input.step; + vx_uint8 * cv_rgb_image_buffer = input.data; + ERROR_CHECK_STATUS( vxCopyImagePatch( input_rgb_image, &cv_rgb_image_region, 0, + &cv_rgb_image_layout, cv_rgb_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + vx_rectangle_t rect = { 0, 0, (vx_uint32)width, (vx_uint32)height }; + vx_map_id map_id; + vx_imagepatch_addressing_t addr; + void * ptr; + ERROR_CHECK_STATUS( vxMapImagePatch( output_filtered_image, &rect, 0, &map_id, &addr, &ptr, + VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X ) ); + Mat mat( height, width, CV_8U, ptr, addr.stride_y ); + imshow( "CannyDetect", mat ); + if(waitKey(30) >= 0) break; + ERROR_CHECK_STATUS( vxUnmapImagePatch( output_filtered_image, map_id ) ); + } + } + else { + printf("Usage:\n" + "./cannyDetect --image \n" + "./cannyDetect --live \n"); + return 0; + } + + ERROR_CHECK_STATUS( vxReleaseGraph( &graph ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &yuv_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &luma_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &input_rgb_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &output_filtered_image ) ); + ERROR_CHECK_STATUS( vxReleaseContext( &context ) ); + return 0; +} diff --git a/samples/C Samples/Canny/cmake/FindOpenCL.cmake b/samples/C Samples/Canny/cmake/FindOpenCL.cmake new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/C Samples/OpenCV_orb/CMakeLists.txt b/samples/C Samples/OpenCV_orb/CMakeLists.txt new file mode 100644 index 0000000000..726be19040 --- /dev/null +++ b/samples/C Samples/OpenCV_orb/CMakeLists.txt @@ -0,0 +1,37 @@ +################################################################################ +# +# MIT License +# +# Copyright (c) 2018 Advanced Micro Devices, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +################################################################################ + +cmake_minimum_required (VERSION 2.8) +project (orbDetect) +set (CMAKE_CXX_STANDARD 11) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +find_package(OpenCL REQUIRED) +find_package(OpenCV REQUIRED) +include_directories (/opt/rocm/mivisionx/include) +link_directories (/opt/rocm/mivisionx/lib) +add_executable(orbDetect orb.cpp) +target_link_libraries(${PROJECT_NAME} openvx vx_opencv ${OpenCV_LIBRARIES}) + diff --git a/samples/C Samples/OpenCV_orb/cmake/FindOpenCL.cmake b/samples/C Samples/OpenCV_orb/cmake/FindOpenCL.cmake new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/C Samples/OpenCV_orb/orb.cpp b/samples/C Samples/OpenCV_orb/orb.cpp new file mode 100644 index 0000000000..72faf65961 --- /dev/null +++ b/samples/C Samples/OpenCV_orb/orb.cpp @@ -0,0 +1,128 @@ +#include +#include + +using namespace cv; +using namespace std; + +#define ERROR_CHECK_STATUS( status ) { \ + vx_status status_ = (status); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +#define ERROR_CHECK_OBJECT( obj ) { \ + vx_status status_ = vxGetStatus((vx_reference)(obj)); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]) +{ + size_t len = strlen(string); + if (len > 0) { + printf("%s", string); + if (string[len - 1] != '\n') + printf("\n"); + fflush(stdout); + } +} + +int main(int argc, char **argv) +{ + if (argc < 1) { + printf("Usage: ./orbDetect\n"); + return 0; + } + + vx_context context = vxCreateContext(); + ERROR_CHECK_OBJECT(context); + vxRegisterLogCallback(context, log_callback, vx_false_e); + + vxLoadKernels(context, "vx_opencv"); + + vx_graph graph = vxCreateGraph(context); + ERROR_CHECK_OBJECT(graph); + + int width = 1280, height = 720; + vx_image inter_luma = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(inter_luma); + + VideoCapture cap(0); + Mat input, output; + cap >> input; + cap >> output; + cvtColor(input, input, COLOR_RGB2GRAY); + + if (input.empty()) { + printf("Image not found\n"); + } + cv::resize(input, input, Size(width, height)); + cv::resize(output, output, Size(width, height)); + + vx_rectangle_t cv_image_region; + cv_image_region.start_x = 0; + cv_image_region.start_y = 0; + cv_image_region.end_x = width; + cv_image_region.end_y = height; + vx_imagepatch_addressing_t cv_image_layout; + cv_image_layout.stride_x = 1; + cv_image_layout.stride_y = input.step; + vx_uint8 * cv_image_buffer = input.data; + + ERROR_CHECK_STATUS( vxCopyImagePatch( inter_luma, &cv_image_region, 0, + &cv_image_layout, cv_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + + vx_array keypoints = vxCreateArray( context, VX_TYPE_KEYPOINT, 10000 ); + ERROR_CHECK_OBJECT( keypoints ); + + vx_int32 nFeatures = 1000; + vx_float32 scaleFactor = 1.2; + vx_int32 nlevels = 2; + vx_int32 edgeThreshold = 31; + vx_int32 firstLevel = 0; + vx_int32 WTA_K = 2; + vx_int32 scoreType = 0; + vx_int32 patchSize = 31; + + vx_node nodes[] = + { + vxExtCvNode_orbDetect(graph, inter_luma, inter_luma, keypoints, nFeatures, scaleFactor, nlevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize) + }; + + for( vx_size i = 0; i < sizeof( nodes ) / sizeof( nodes[0] ); i++ ) + { + ERROR_CHECK_OBJECT( nodes[i] ); + ERROR_CHECK_STATUS( vxReleaseNode( &nodes[i] ) ); + } + + ERROR_CHECK_STATUS( vxVerifyGraph( graph ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + + vx_size num_corners = 0; + ERROR_CHECK_STATUS(vxQueryArray (keypoints, VX_ARRAY_NUMITEMS, &num_corners, sizeof(num_corners))); + if (num_corners > 0) { + vx_size kp_stride; + vx_map_id kp_map; + vx_uint8 * kp_buf; + ERROR_CHECK_STATUS(vxMapArrayRange (keypoints, 0, num_corners, &kp_map, &kp_stride, (void **)&kp_buf, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, 0)); + for (vx_size i = 0; i < num_corners; i++) { + vx_keypoint_t * kp = (vx_keypoint_t *) (kp_buf + i*kp_stride); + cv::Point center (kp->x, kp->y); + cv::circle (output, center, 1, cv::Scalar(0,255, 0),2); + } + } + + imshow( "OrbDetect", output ); + waitKey(0); + + ERROR_CHECK_STATUS( vxReleaseGraph( &graph ) ); + ERROR_CHECK_STATUS( vxReleaseArray (&keypoints)); + ERROR_CHECK_STATUS( vxReleaseImage( &inter_luma ) ); + ERROR_CHECK_STATUS( vxReleaseContext( &context ) ); + return 0; +} diff --git a/samples/C_Samples/Canny/CMakeLists.txt b/samples/C_Samples/Canny/CMakeLists.txt new file mode 100644 index 0000000000..ebe329147e --- /dev/null +++ b/samples/C_Samples/Canny/CMakeLists.txt @@ -0,0 +1,37 @@ +################################################################################ +# +# MIT License +# +# Copyright (c) 2018 Advanced Micro Devices, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +################################################################################ + +cmake_minimum_required (VERSION 2.8) +project (cannyDetect) +set (CMAKE_CXX_STANDARD 11) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +find_package(OpenCL REQUIRED) +find_package(OpenCV REQUIRED) +include_directories (/opt/rocm/mivisionx/include) +link_directories (/opt/rocm/mivisionx/lib) +add_executable(cannyDetect canny.cpp) +target_link_libraries(${PROJECT_NAME} openvx ${OpenCV_LIBRARIES}) + diff --git a/samples/C_Samples/Canny/canny.cpp b/samples/C_Samples/Canny/canny.cpp new file mode 100644 index 0000000000..decd62648e --- /dev/null +++ b/samples/C_Samples/Canny/canny.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + +using namespace cv; +using namespace std; + +#define ERROR_CHECK_STATUS( status ) { \ + vx_status status_ = (status); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +#define ERROR_CHECK_OBJECT( obj ) { \ + vx_status status_ = vxGetStatus((vx_reference)(obj)); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]) +{ + size_t len = strlen(string); + if (len > 0) { + printf("%s", string); + if (string[len - 1] != '\n') + printf("\n"); + fflush(stdout); + } +} + +int main(int argc, char **argv) +{ + if (argc < 2) { + printf("Usage:\n" + "./cannyDetect --image \n" + "./cannyDetect --live \n"); + return 0; + } + + int width = 480, height = 360; + + vx_context context = vxCreateContext(); + ERROR_CHECK_OBJECT(context); + vxRegisterLogCallback(context, log_callback, vx_false_e); + + vx_graph graph = vxCreateGraph(context); + ERROR_CHECK_OBJECT(graph); + + vx_image input_rgb_image = vxCreateImage(context, width, height, VX_DF_IMAGE_RGB); + vx_image output_filtered_image = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(input_rgb_image); + ERROR_CHECK_OBJECT(output_filtered_image); + + vx_image yuv_image = vxCreateVirtualImage(graph, width, height, VX_DF_IMAGE_IYUV); + vx_image luma_image = vxCreateVirtualImage(graph, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(yuv_image); + ERROR_CHECK_OBJECT(luma_image); + + vx_threshold hyst = vxCreateThreshold(context, VX_THRESHOLD_TYPE_RANGE, VX_TYPE_UINT8); + vx_int32 lower = 80, upper = 100; + vxSetThresholdAttribute(hyst, VX_THRESHOLD_ATTRIBUTE_THRESHOLD_LOWER, &lower, sizeof(lower)); + vxSetThresholdAttribute(hyst, VX_THRESHOLD_ATTRIBUTE_THRESHOLD_UPPER, &upper, sizeof(upper)); + ERROR_CHECK_OBJECT(hyst); + vx_int32 gradient_size = 3; + + vx_node nodes[] = + { + vxColorConvertNode(graph, input_rgb_image, yuv_image), + vxChannelExtractNode(graph, yuv_image, VX_CHANNEL_Y, luma_image), + vxCannyEdgeDetectorNode(graph, luma_image, hyst, gradient_size, VX_NORM_L1, output_filtered_image) + }; + + for( vx_size i = 0; i < sizeof( nodes ) / sizeof( nodes[0] ); i++ ) + { + ERROR_CHECK_OBJECT( nodes[i] ); + ERROR_CHECK_STATUS( vxReleaseNode( &nodes[i] ) ); + } + + ERROR_CHECK_STATUS( vxVerifyGraph( graph ) ); + + string option = argv[1]; + Mat input; + + if (option == "--image") { + input = imread(argv[2]); + if (input.empty()) { + printf("Image not found\n"); + return 0; + } + resize(input, input, Size(width, height)); + imshow("inputWindow", input); + vx_rectangle_t cv_rgb_image_region; + cv_rgb_image_region.start_x = 0; + cv_rgb_image_region.start_y = 0; + cv_rgb_image_region.end_x = width; + cv_rgb_image_region.end_y = height; + vx_imagepatch_addressing_t cv_rgb_image_layout; + cv_rgb_image_layout.stride_x = 3; + cv_rgb_image_layout.stride_y = input.step; + vx_uint8 * cv_rgb_image_buffer = input.data; + ERROR_CHECK_STATUS( vxCopyImagePatch( input_rgb_image, &cv_rgb_image_region, 0, + &cv_rgb_image_layout, cv_rgb_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + vx_rectangle_t rect = { 0, 0, (vx_uint32)width, (vx_uint32)height }; + vx_map_id map_id; + vx_imagepatch_addressing_t addr; + void * ptr; + ERROR_CHECK_STATUS( vxMapImagePatch( output_filtered_image, &rect, 0, &map_id, &addr, &ptr, + VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X ) ); + Mat mat( height, width, CV_8U, ptr, addr.stride_y ); + imshow( "CannyDetect", mat ); + waitKey(0); + ERROR_CHECK_STATUS( vxUnmapImagePatch( output_filtered_image, map_id ) ); + } + else if (option == "--live") { + VideoCapture cap(0); + if (!cap.isOpened()) { + printf("Unable to open camera\n"); + return 0; + } + for(;;) { + cap >> input; + resize(input, input, Size(width, height)); + imshow("inputWindow", input); + if(waitKey(30) >= 0) break; + vx_rectangle_t cv_rgb_image_region; + cv_rgb_image_region.start_x = 0; + cv_rgb_image_region.start_y = 0; + cv_rgb_image_region.end_x = width; + cv_rgb_image_region.end_y = height; + vx_imagepatch_addressing_t cv_rgb_image_layout; + cv_rgb_image_layout.stride_x = 3; + cv_rgb_image_layout.stride_y = input.step; + vx_uint8 * cv_rgb_image_buffer = input.data; + ERROR_CHECK_STATUS( vxCopyImagePatch( input_rgb_image, &cv_rgb_image_region, 0, + &cv_rgb_image_layout, cv_rgb_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + vx_rectangle_t rect = { 0, 0, (vx_uint32)width, (vx_uint32)height }; + vx_map_id map_id; + vx_imagepatch_addressing_t addr; + void * ptr; + ERROR_CHECK_STATUS( vxMapImagePatch( output_filtered_image, &rect, 0, &map_id, &addr, &ptr, + VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X ) ); + Mat mat( height, width, CV_8U, ptr, addr.stride_y ); + imshow( "CannyDetect", mat ); + if(waitKey(30) >= 0) break; + ERROR_CHECK_STATUS( vxUnmapImagePatch( output_filtered_image, map_id ) ); + } + } + else { + printf("Usage:\n" + "./cannyDetect --image \n" + "./cannyDetect --live \n"); + return 0; + } + + ERROR_CHECK_STATUS( vxReleaseGraph( &graph ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &yuv_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &luma_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &input_rgb_image ) ); + ERROR_CHECK_STATUS( vxReleaseImage( &output_filtered_image ) ); + ERROR_CHECK_STATUS( vxReleaseContext( &context ) ); + return 0; +} diff --git a/samples/C_Samples/Canny/cmake/FindOpenCL.cmake b/samples/C_Samples/Canny/cmake/FindOpenCL.cmake new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/C_Samples/OpenCV_orb/CMakeLists.txt b/samples/C_Samples/OpenCV_orb/CMakeLists.txt new file mode 100644 index 0000000000..726be19040 --- /dev/null +++ b/samples/C_Samples/OpenCV_orb/CMakeLists.txt @@ -0,0 +1,37 @@ +################################################################################ +# +# MIT License +# +# Copyright (c) 2018 Advanced Micro Devices, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +################################################################################ + +cmake_minimum_required (VERSION 2.8) +project (orbDetect) +set (CMAKE_CXX_STANDARD 11) +list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) +find_package(OpenCL REQUIRED) +find_package(OpenCV REQUIRED) +include_directories (/opt/rocm/mivisionx/include) +link_directories (/opt/rocm/mivisionx/lib) +add_executable(orbDetect orb.cpp) +target_link_libraries(${PROJECT_NAME} openvx vx_opencv ${OpenCV_LIBRARIES}) + diff --git a/samples/C_Samples/OpenCV_orb/cmake/FindOpenCL.cmake b/samples/C_Samples/OpenCV_orb/cmake/FindOpenCL.cmake new file mode 100644 index 0000000000..e69de29bb2 diff --git a/samples/C_Samples/OpenCV_orb/orb.cpp b/samples/C_Samples/OpenCV_orb/orb.cpp new file mode 100644 index 0000000000..72faf65961 --- /dev/null +++ b/samples/C_Samples/OpenCV_orb/orb.cpp @@ -0,0 +1,128 @@ +#include +#include + +using namespace cv; +using namespace std; + +#define ERROR_CHECK_STATUS( status ) { \ + vx_status status_ = (status); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +#define ERROR_CHECK_OBJECT( obj ) { \ + vx_status status_ = vxGetStatus((vx_reference)(obj)); \ + if(status_ != VX_SUCCESS) { \ + printf("ERROR: failed with status = (%d) at " __FILE__ "#%d\n", status_, __LINE__); \ + exit(1); \ + } \ +} + +static void VX_CALLBACK log_callback(vx_context context, vx_reference ref, vx_status status, const vx_char string[]) +{ + size_t len = strlen(string); + if (len > 0) { + printf("%s", string); + if (string[len - 1] != '\n') + printf("\n"); + fflush(stdout); + } +} + +int main(int argc, char **argv) +{ + if (argc < 1) { + printf("Usage: ./orbDetect\n"); + return 0; + } + + vx_context context = vxCreateContext(); + ERROR_CHECK_OBJECT(context); + vxRegisterLogCallback(context, log_callback, vx_false_e); + + vxLoadKernels(context, "vx_opencv"); + + vx_graph graph = vxCreateGraph(context); + ERROR_CHECK_OBJECT(graph); + + int width = 1280, height = 720; + vx_image inter_luma = vxCreateImage(context, width, height, VX_DF_IMAGE_U8); + ERROR_CHECK_OBJECT(inter_luma); + + VideoCapture cap(0); + Mat input, output; + cap >> input; + cap >> output; + cvtColor(input, input, COLOR_RGB2GRAY); + + if (input.empty()) { + printf("Image not found\n"); + } + cv::resize(input, input, Size(width, height)); + cv::resize(output, output, Size(width, height)); + + vx_rectangle_t cv_image_region; + cv_image_region.start_x = 0; + cv_image_region.start_y = 0; + cv_image_region.end_x = width; + cv_image_region.end_y = height; + vx_imagepatch_addressing_t cv_image_layout; + cv_image_layout.stride_x = 1; + cv_image_layout.stride_y = input.step; + vx_uint8 * cv_image_buffer = input.data; + + ERROR_CHECK_STATUS( vxCopyImagePatch( inter_luma, &cv_image_region, 0, + &cv_image_layout, cv_image_buffer, + VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST ) ); + + vx_array keypoints = vxCreateArray( context, VX_TYPE_KEYPOINT, 10000 ); + ERROR_CHECK_OBJECT( keypoints ); + + vx_int32 nFeatures = 1000; + vx_float32 scaleFactor = 1.2; + vx_int32 nlevels = 2; + vx_int32 edgeThreshold = 31; + vx_int32 firstLevel = 0; + vx_int32 WTA_K = 2; + vx_int32 scoreType = 0; + vx_int32 patchSize = 31; + + vx_node nodes[] = + { + vxExtCvNode_orbDetect(graph, inter_luma, inter_luma, keypoints, nFeatures, scaleFactor, nlevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize) + }; + + for( vx_size i = 0; i < sizeof( nodes ) / sizeof( nodes[0] ); i++ ) + { + ERROR_CHECK_OBJECT( nodes[i] ); + ERROR_CHECK_STATUS( vxReleaseNode( &nodes[i] ) ); + } + + ERROR_CHECK_STATUS( vxVerifyGraph( graph ) ); + ERROR_CHECK_STATUS( vxProcessGraph( graph ) ); + + vx_size num_corners = 0; + ERROR_CHECK_STATUS(vxQueryArray (keypoints, VX_ARRAY_NUMITEMS, &num_corners, sizeof(num_corners))); + if (num_corners > 0) { + vx_size kp_stride; + vx_map_id kp_map; + vx_uint8 * kp_buf; + ERROR_CHECK_STATUS(vxMapArrayRange (keypoints, 0, num_corners, &kp_map, &kp_stride, (void **)&kp_buf, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, 0)); + for (vx_size i = 0; i < num_corners; i++) { + vx_keypoint_t * kp = (vx_keypoint_t *) (kp_buf + i*kp_stride); + cv::Point center (kp->x, kp->y); + cv::circle (output, center, 1, cv::Scalar(0,255, 0),2); + } + } + + imshow( "OrbDetect", output ); + waitKey(0); + + ERROR_CHECK_STATUS( vxReleaseGraph( &graph ) ); + ERROR_CHECK_STATUS( vxReleaseArray (&keypoints)); + ERROR_CHECK_STATUS( vxReleaseImage( &inter_luma ) ); + ERROR_CHECK_STATUS( vxReleaseContext( &context ) ); + return 0; +} diff --git a/samples/README.md b/samples/README.md index ad96b9bbc9..4673381620 100644 --- a/samples/README.md +++ b/samples/README.md @@ -44,3 +44,26 @@ usage: ```` runvx -frames:live OpenCV_orb-LIVE.gdf ```` + +## C / C++ Samples + +MIVisionX samples IN C / C++ + +### Canny + +usage: + +```` +cmake . +make +./cannyDetect --image +./cannyDetect --live +```` +### Orb Detect +usage: + +```` +cmake . +make +./orbDetect +````