this example shows how to read an image with OpenCV, transfer the image to the GPU, and apply a simple flip filter written in OpenCL
| 24 | // this example shows how to read an image with OpenCV, transfer the |
| 25 | // image to the GPU, and apply a simple flip filter written in OpenCL |
| 26 | int main(int argc, char *argv[]) |
| 27 | { |
| 28 | // check command line |
| 29 | if(argc < 2){ |
| 30 | std::cerr << "usage: " << argv[0] << " FILENAME" << std::endl; |
| 31 | return -1; |
| 32 | } |
| 33 | |
| 34 | // read image with opencv |
| 35 | cv::Mat cv_image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR); |
| 36 | if(!cv_image.data){ |
| 37 | std::cerr << "failed to load image" << std::endl; |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | // get default device and setup context |
| 42 | compute::device gpu = compute::system::default_device(); |
| 43 | compute::context context(gpu); |
| 44 | compute::command_queue queue(context, gpu); |
| 45 | |
| 46 | // convert image to BGRA (OpenCL requires 16-byte aligned data) |
| 47 | cv::cvtColor(cv_image, cv_image, CV_BGR2BGRA); |
| 48 | |
| 49 | // transfer image to gpu |
| 50 | compute::image2d input_image = |
| 51 | compute::opencv_create_image2d_with_mat( |
| 52 | cv_image, compute::image2d::read_write, queue |
| 53 | ); |
| 54 | |
| 55 | // create output image |
| 56 | compute::image2d output_image( |
| 57 | context, |
| 58 | input_image.width(), |
| 59 | input_image.height(), |
| 60 | input_image.format(), |
| 61 | compute::image2d::write_only |
| 62 | ); |
| 63 | |
| 64 | // create flip program |
| 65 | const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE( |
| 66 | __kernel void flip_kernel(__read_only image2d_t input, |
| 67 | __write_only image2d_t output) |
| 68 | { |
| 69 | const sampler_t sampler = CLK_ADDRESS_NONE | CLK_FILTER_NEAREST; |
| 70 | int height = get_image_height(input); |
| 71 | int2 input_coord = { get_global_id(0), get_global_id(1) }; |
| 72 | int2 output_coord = { input_coord.x, height - input_coord.y - 1 }; |
| 73 | float4 value = read_imagef(input, sampler, input_coord); |
| 74 | write_imagef(output, output_coord, value); |
| 75 | } |
| 76 | ); |
| 77 | |
| 78 | compute::program flip_program = |
| 79 | compute::program::create_with_source(source, context); |
| 80 | flip_program.build(); |
| 81 | |
| 82 | // create flip kernel and set arguments |
| 83 | compute::kernel flip_kernel(flip_program, "flip_kernel"); |
nothing calls this directly
no test coverage detected