title: Image Processing on Vivado HLS
body: <p>I'm working on video processing project. At the initial stage, I started with a simple program with importing an image and just duplicate it in the main function and pass it back. It works but the resultant image is not what I put.</p>
  
  <p>Here are my files and attached images.</p>
  
  <p><strong>image_filter.h</strong></p>
  
  <pre><code>#ifndef _TOP_H_
  #define _TOP_H_
  #include "hls_video.h"
  #include &lt;ap_axi_sdata.h&gt;
  #define MAX_WIDTH 256
  #define MAX_HEIGHT 256
  // Image file path
  #define INPUT_IMAGE_CORE "D:\\IMP_PRACTICE\\cameraman.bmp"
  #define OUTPUT_IMAGE_CORE "D:\\IMP_PRACTICE\\cameraman_res.bmp"
  typedef hls::stream&lt;ap_axiu&lt;8,1,1,1&gt; &gt; AXI_STREAM;
  typedef hls::Mat&lt;MAX_HEIGHT, MAX_WIDTH, HLS_8UC1&gt; IMAGE_8;
  //typedef hls::Scalar&lt;2, unsigned char&gt; PIXEL_8;
  typedef unsigned char data_t;
  
  typedef ap_axiu&lt;8,1,1,1&gt; uint_8_side_channel;
  
  void image_filter(AXI_STREAM &amp;INPUT_STREAM, AXI_STREAM &amp;OUTPUT_STREAM, int rows, int cols);
  #endif
  </code></pre>
  
  <p><strong>image_filter.cpp</strong></p>
  
  <pre><code>#include "image_filter.h"
  void image_filter(AXI_STREAM &amp;video_in, AXI_STREAM &amp;video_out, int rows, int cols)
  {
  #pragma HLS INTERFACE axis port=video_in bundle=INPUT_STREAM
  #pragma HLS INTERFACE axis port=video_out bundle=OUTPUT_STREAM
  #pragma HLS INTERFACE s_axilite port=rows bundle=CONTROL_BUS //offset=0x14
  #pragma HLS INTERFACE s_axilite port=cols bundle=CONTROL_BUS //offset=0x1c
  #pragma HLS INTERFACE s_axilite port=return bundle=CONTROL_BUS
  
  IMAGE_8 img_0(rows, cols);
  IMAGE_8 img_1(rows, cols);
  IMAGE_8 img_2(rows, cols);
  #pragma HLS dataflow
  
  hls::AXIvideo2Mat(video_in, img_0);
  hls::Duplicate(img_0, img_1, img_2);
  hls::Mat2AXIvideo(img_1, video_out);
  </code></pre>
  
  <p>}</p>
  
  <p><strong>test_image_filter.cpp</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;hls_opencv.h&gt;
  #include "image_filter.h"
  
  // Image File path
  char outImage[MAX_WIDTH][MAX_HEIGHT];
  void saveImage(const std::string path, cv::InputArray inArr)
  {
  double min;
  double max;
  cv::minMaxIdx(inArr, &amp;min, &amp;max);
  cv::Mat adjMap;
  cv::convertScaleAbs(inArr, adjMap, 255 / max);
  cv::imwrite(path,adjMap);
  }
  int main()
  {
  // Read input image
  printf("Load image %s\n",INPUT_IMAGE_CORE);
  cv::Mat imageSrc;
  imagesrc=cv::imread(INPUT_IMAGE_CORE);
  // Convert to grayscale
  cv::cvtColor(imageSrc, imageSrc, CV_BGR2GRAY);
  printf("Image Rows:%d Cols:%d\n",imageSrc.rows, imageSrc.cols);
  // Define streams for input and output
  AXI_STREAM inputStream;
  AXI_STREAM outputStream;
  // OpenCV mat that point to a array (cv::Size(Width, Height))
  cv::Mat imgCvOut(cv::Size(imageSrc.cols, imageSrc.rows), CV_8UC1, outImage, cv::Mat::AUTO_STEP);
  // Populate the input stream with the image bytes
  for (int idxRows=0; idxRows &lt; imageSrc.rows; idxRows++)
  {
      for (int idxCols=0; idxCols &lt; imageSrc.cols; idxCols++)
      {
          uint_8_side_channel valIn;
          valIn.data = imageSrc.at&lt;unsigned char&gt;(idxRows,idxCols);
          valIn.keep = 1; valIn.strb = 1; valIn.user = 1; valIn.id = 0; valIn.dest = 0;
  inputStream &lt;&lt; valIn;
      }
  }
  
  printf("Calling Core function\n");
  image_filter(inputStream, outputStream, MAX_WIDTH, MAX_HEIGHT);
  printf("Core function ended\n");
  // Take data from the output stream to our array outImage (Pointed in opencv)
  for (int idxRows=0; idxRows &lt; imageSrc.rows; idxRows++)
  {
      for (int idxCols=0; idxCols &lt; imageSrc.cols; idxCols++)
      {
          uint_8_side_channel valOut;
  outputStream.read(valOut);
  outImage[idxRows][idxCols] = valOut.data;
      }
  }
  // Save image out file or display
  saveImage(std::string(OUTPUT_IMAGE_CORE) ,imgCvOut);
  return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>The resultant image "cameraman_res.bmp" is totally different from the input image "cameraman.bmp". </p>
  
  <p>Can anyone please highlight what mistakes I'm doing in this code?</p>
  
  <p>Many thanks!</p>