Norris Lin
Published

Build and Install OpenCV in Vitis HLS 2023.2

This article introduces how to install OpenCV in Vitis HLS 2023.2 and successfully run examples from the Vitis Vision Library.

BeginnerProtip2 hours27
Build and Install OpenCV in Vitis HLS 2023.2

Things used in this project

Software apps and online services

OpenCV
OpenCV
MinGW
CMake
Vitis Vision Library
AMD-Xilinx - Vitis HLS

Story

Read more

Code

xf_axiconv_accel.cpp

C/C++
/*
 * Copyright 2022 Xilinx, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "xf_axiconv_accel_config.h"

void axiconv_accel(hls::stream<ap_axiu<8, 1, 1, 1> >& src, hls::stream<ap_axiu<8, 1, 1, 1> >& dst, int rows, int cols) {
// clang-format off
    #pragma HLS INTERFACE axis port=src
    #pragma HLS INTERFACE axis port=dst
    #pragma HLS INTERFACE s_axilite port=rows               
    #pragma HLS INTERFACE s_axilite port=cols               
    #pragma HLS INTERFACE s_axilite port=return
    // clang-format on

    xf::cv::Mat<IN_TYPE, XF_HEIGHT, XF_WIDTH, NPPCX, XF_CV_DEPTH_IN_1> src_mat(rows, cols);
// clang-format off
    #pragma HLS dataflow
    // clang-format on

    xf::cv::AXIvideo2xfMat(src, src_mat);
    xf::cv::xfMat2AXIvideo(src_mat, dst);

    return;
}

xf_axiconv_accel_config.h

C/C++
/*
 * Copyright 2022 Xilinx, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _XF_AXICONV_CONFIG_H_
#define _XF_AXICONV_CONFIG_H_

#include "hls_stream.h"
#include "common/xf_common.hpp"
#include "common/xf_utility.hpp"
#include "common/xf_infra.hpp"

/* config width and height */
#define XF_HEIGHT 720
#define XF_WIDTH 1280

#define XF_CV_DEPTH_IN_1 2

#define GRAY 1
#define RGB 0

#define NPPCX XF_NPPC1

#define IN_TYPE XF_8UC1
#define OUT_TYPE XF_8UC1

#define CV_IN_TYPE CV_8UC1
#define CV_OUT_TYPE CV_8UC1

#define INPUT_PTR_WIDTH 8
#define OUTPUT_PTR_WIDTH 8

void axiconv_accel(hls::stream<ap_axiu<8, 1, 1, 1> >& _src,
                   hls::stream<ap_axiu<8, 1, 1, 1> >& _dst,
                   int rows,
                   int cols);

#endif

xf_axiconv_tb.cpp

C/C++
/*
 * Copyright 2022 Xilinx, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common/xf_headers.hpp"
#include "xf_axiconv_accel_config.h"
#include "common/xf_axi.hpp"

using namespace std;

#define _W 8

int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "Invalid Number of Arguments!\nUsage: <executable> <image>\n");
        return -1;
    }

    cv::Mat img, diff;
    img = cv::imread(argv[1], 0);
    if (img.data == NULL) {
        fprintf(stderr, "Cannot open image at %s\n", argv[1]);
        return 0;
    }

    int rows = img.rows;
    int cols = img.cols;
    cv::Mat out_img(rows, cols, CV_OUT_TYPE);

    // convert input to axiStream
    hls::stream<ap_axiu<_W, 1, 1, 1> > _src;
    xf::cv::cvMat2AXIvideoxf<NPPCX, _W>(img, _src);

    // output axiStream
    hls::stream<ap_axiu<_W, 1, 1, 1> > _dst;

    // Launch the kernel
    axiconv_accel(_src, _dst, rows, cols);

    xf::cv::AXIvideo2cvMatxf<NPPCX>(_dst, out_img);

    // Write output image
    cv::imwrite("output.png", out_img);

    /**** validation ****/
    // diff
    diff.create(img.rows, img.cols, CV_OUT_TYPE);
    // Compute absolute difference image
    cv::absdiff(img, out_img, diff);
    imwrite("error.png", diff); // Save the difference image for debugging purpose
    float err_per;
    xf::cv::analyzeDiff(diff, 0, err_per);
    if (err_per > 0.0f) {
        fprintf(stderr, "ERROR: Test Failed.\n ");
        return 1;
    } else
        std::cout << "Test Passed " << std::endl;
    /**** end of validation ****/

    return 0;
}

Credits

Norris Lin

Norris Lin

1 project • 2 followers
An R&D professional passionate about FPGA development, skilled in system and IP integration.

Comments