Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C samples #34

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion amd_openvx_extensions/amd_opencv/include/vx_ext_opencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ THE SOFTWARE.


#include "VX/vx.h"
#include "vx_opencv.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merged in pr #33

#include <VX/vx_compatibility.h>

#include "opencv2/opencv.hpp"
Expand Down
37 changes: 37 additions & 0 deletions samples/C Samples/Canny/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

170 changes: 170 additions & 0 deletions samples/C Samples/Canny/canny.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include <VX/vx.h>
#include <VX/vx_compatibility.h>
#include <vx_ext_opencv.h>

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 <imageName>\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 <imageName>\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;
}
Empty file.
37 changes: 37 additions & 0 deletions samples/C Samples/OpenCV_orb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})

Empty file.
128 changes: 128 additions & 0 deletions samples/C Samples/OpenCV_orb/orb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <VX/vx.h>
#include <vx_ext_opencv.h>

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;
}
Loading