Compute Library
 21.02
neon_cartoon_effect.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
26 
27 #include "arm_compute/core/Types.h"
28 #include "utils/ImageLoader.h"
29 #include "utils/Utils.h"
30 
31 using namespace arm_compute;
32 using namespace utils;
33 
34 class NEONCartoonEffectExample : public Example
35 {
36 public:
37  bool do_setup(int argc, char **argv) override
38  {
39  // Open PPM file
40  PPMLoader ppm;
41 
42  if(argc < 2)
43  {
44  // Print help
45  std::cout << "Usage: ./build/neon_cartoon_effect [input_image.ppm]\n\n";
46  std::cout << "No input_image provided, creating a dummy 640x480 image\n";
47  // Create an empty grayscale 640x480 image
48  src_img.allocator()->init(TensorInfo(640, 480, Format::U8));
49  }
50  else
51  {
52  ppm.open(argv[1]);
53  ppm.init_image(src_img, Format::U8);
54  }
55 
56  // Initialize just the dimensions and format of the images:
57  gaus5x5_img.allocator()->init(*src_img.info());
58  canny_edge_img.allocator()->init(*src_img.info());
59  dst_img.allocator()->init(*src_img.info());
60 
61  // Configure the functions to call
62  gaus5x5.configure(&src_img, &gaus5x5_img, BorderMode::REPLICATE);
63  canny_edge.configure(&src_img, &canny_edge_img, 100, 80, 3, 1, BorderMode::REPLICATE);
64  sub.configure(&gaus5x5_img, &canny_edge_img, &dst_img, ConvertPolicy::SATURATE);
65 
66  // Now that the padding requirements are known we can allocate the images:
67  src_img.allocator()->allocate();
68  dst_img.allocator()->allocate();
69  gaus5x5_img.allocator()->allocate();
70  canny_edge_img.allocator()->allocate();
71 
72  // Fill the input image with the content of the PPM image if a filename was provided:
73  if(ppm.is_open())
74  {
75  ppm.fill_image(src_img);
76  output_filename = std::string(argv[1]) + "_out.ppm";
77  }
78 
79  return true;
80  }
81 
82  void do_run() override
83  {
84  // Execute the functions:
85  gaus5x5.run();
86  canny_edge.run();
87  sub.run();
88  }
89 
90  void do_teardown() override
91  {
92  // Save the result to file:
93  if(!output_filename.empty())
94  {
95  save_to_ppm(dst_img, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
96  }
97  }
98 
99 private:
100  Image src_img{}, dst_img{}, gaus5x5_img{}, canny_edge_img{};
101  NEGaussian5x5 gaus5x5{};
102  NECannyEdge canny_edge{};
104  std::string output_filename{};
105 };
106 
107 /** Main program for cartoon effect test
108  *
109  * @param[in] argc Number of arguments
110  * @param[in] argv Arguments ( [optional] Path to PPM image to process )
111  */
112 int main(int argc, char **argv)
113 {
114  return utils::run_example<NEONCartoonEffectExample>(argc, argv);
115 }
void save_to_ppm(T &tensor, const std::string &ppm_filename)
Template helper function to save a tensor image to a PPM file.
Definition: Utils.h:547
void open(const std::string &filename) override
Open an image file and reads its metadata (Width, height)
Definition: ImageLoader.h:381
1 channel, 1 U8 per channel
Includes all the Neon functions at once.
Copyright (c) 2017-2021 Arm Limited.
Basic function to run cpu::kernels::CpuSubKernel.
Basic function to execute gaussian filter 5x5.
Definition: NEGaussian5x5.h:52
bool is_open() override
Return true if the image file is currently open.
Definition: ImageLoader.h:377
Basic function to execute canny edge on Neon.
Definition: NECannyEdge.h:58
Abstract Example class.
Definition: Utils.h:78
Structure to hold Image information.
Definition: helpers.h:659
void fill_image(T &image)
Fill an image with the content of the currently open image file.
Definition: ImageLoader.h:177
Pixels outside the image are assumed to have the same value as the closest image pixel.
int main(int argc, char **argv)
Main program for cartoon effect test.
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
void init_image(T &image, Format format)
Initialise an image&#39;s metadata with the dimensions of the image file currently open.
Definition: ImageLoader.h:161
PPM Image loader concrete implementation.
Definition: ImageLoader.h:367