FC2カウンター FPGAの部屋 2023年09月04日
fc2ブログ

FPGAやCPLDの話題やFPGA用のツールの話題などです。 マニアックです。 日記も書きます。

FPGAの部屋

FPGAの部屋の有用と思われるコンテンツのまとめサイトを作りました。Xilinx ISEの初心者の方には、FPGAリテラシーおよびチュートリアルのページをお勧めいたします。

Vitis HLS 2023.1 で RGB の各色を n 倍する color_converter_RGB24 IP を作成する1

Vitis HLS 2023.1 で RGB の各色を n 倍する color_converter_RGB24 IP を作成する。
掛ける倍率は任意制度固定小数点型の ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> を使用した。

ソースコードやテストベンチ・コードを貼っておく。
まずは、color_converterRGB24.h から貼っておく。

// color_converterRGB24.h
// 2023/09/03 by marsee
//

#ifndef __COLOR_CONVERTER_RGB24_H__
#define __COLOR_CONVERTER_RGB24_H__

#define ORG_IMGwAxiVdma 0
#define COLOR_CONVwAxiVdma 1
#define ORG_IMGwAxiDma 2
#define COLOR_CONVwAxiDma 3

#endif


ソースコードの color_converter_RGB24.cpp を貼っておく。

// color_converter_RGB24.cpp
// 2023/09/03 by marsee
//

#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include <ap_fixed.h>

#include "color_converter_RGB24.h"

int color_conv(ap_uint<24> data, ap_uint<24> &val,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag);
int separate_rgb(ap_uint<24> rgb, ap_uint<8> &r, ap_uint<8> &g, ap_uint<8> &b);

int color_converter_RGB24(hls::stream<ap_axiu<24,1,1,1> >& ins,
        hls::stream<ap_axiu<24,1,1,1> >& outs, int32_t function,
        int32_t row_size, int32_t col_size,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag){
#pragma HLS INTERFACE mode=s_axilite port=blue_mag
#pragma HLS INTERFACE mode=s_axilite port=green_mag
#pragma HLS INTERFACE mode=s_axilite port=red_mag
#pragma HLS INTERFACE mode=s_axilite port=col_size
#pragma HLS INTERFACE mode=s_axilite port=row_size
#pragma HLS INTERFACE mode=s_axilite port=function
#pragma HLS INTERFACE mode=axis register_mode=both port=outs register
#pragma HLS INTERFACE mode=axis register_mode=both port=ins register
#pragma HLS INTERFACE mode=s_axilite port=return
    ap_axiu<24,1,1,1> pix;
    ap_axiu<24,1,1,1> colorc;
    ap_uint<24> val;

    LOOP_WAIT_USER : do {   // user が 1になった時にフレームがスタートする
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
        ins >> pix;
        if(function==ORG_IMGwAxiDma || function==COLOR_CONVwAxiDma)
            break;
    } while(pix.user == 0);

    LOOP_Y: for(int y=0; y<row_size; y++){
#pragma HLS LOOP_TRIPCOUNT avg=600 max=1080 min=48
        LOOP_X: for(int x=0; x<col_size; x++){
#pragma HLS LOOP_TRIPCOUNT avg=800 max=1920 min=64
#pragma HLS PIPELINE II=1
            if (!(x==0 && y==0))    // 最初の入力はすでに入力されている
                ins >> pix; // AXI4-Stream からの入力

            color_conv(pix.data, val, red_mag, green_mag, blue_mag);
            colorc.data = val;

            if(function==ORG_IMGwAxiVdma || function == COLOR_CONVwAxiVdma){
                if(x==0 && y==0) // 最初のピクセル
                    colorc.user = 1;
                else
                    colorc.user = 0;
                if(x == (col_size-1)) // 行の最後
                    colorc.last = 1;
                else
                    colorc.last = 0;
            }else{
                colorc.user = 0;
                colorc.last = pix.last;
            }
            colorc.keep = 0x7;
            colorc.strb = 0x7;
            if(function==COLOR_CONVwAxiVdma || function==COLOR_CONVwAxiDma)
                outs << colorc;
            else
                outs << pix;
        }
    }
    return(0);
}

// color_converter
// data の値に reg_mag を掛けて新しい赤の値とする。緑、青も同様
int color_conv(ap_uint<24> data, ap_uint<24> &val,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag){
    ap_uint<8> r, g, b;
    ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> calc_r, calc_g, calc_b;
    ap_uint<8> rt, gt, bt;

    separate_rgb(data, r, g, b);
    calc_r = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)r * red_mag;
    calc_g = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)g * green_mag;
    calc_b = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)b * blue_mag;
    rt = calc_r;
    gt = calc_g;
    bt = calc_b;

    val = ((ap_uint<24>)rt << 16)+((ap_uint<24>)gt << 8)+(ap_uint<24>)bt;
    return(0);
}

// separate_rgb
// RGBを分離する
// RBGのフォーマットは、{R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
//
int separate_rgb(ap_uint<24> rgb, ap_uint<8> &r, ap_uint<8> &g, ap_uint<8> &b){
    b = (ap_uint<8>)(rgb & 0xff);
    g = (ap_uint<8>)((rgb>>8) & 0xff);
    r = (ap_uint<8>)((rgb>>16) & 0xff);
    return(0);
}


テストベンチ・コードの color_converter_RGB24_tb.cpp を貼っておく。

// color_converter_RGB24_tb.cpp
// 2023/09/04 by marsee
// COLORCwXilinxVideoStandard を define すると axi_vdma 用となり、コメントアウトすると axi_dma 用になる
//

#include <stdio.h>
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"

#include "color_converter_RGB24.h"

//#define COLORCwXilinxVideoStandard

constexpr int size = 3;

int color_converter_RGB24(hls::stream<ap_axiu<24,1,1,1> >& ins,
        hls::stream<ap_axiu<24,1,1,1> >& outs, int32_t function,
        int32_t row_size, int32_t col_size,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag);
int color_converter_RGB24_soft(hls::stream<ap_axiu<24,1,1,1> >& ins,
        hls::stream<ap_axiu<24,1,1,1> >& outs, int32_t function,
        int32_t row_size, int32_t col_size,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag);
int color_conv_soft(ap_uint<24> data, ap_uint<24> &val,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag);
int separate_rgb_soft(ap_uint<24> rgb, ap_uint<8> &r, ap_uint<8> &g, ap_uint<8> &b);

const char INPUT_JPG_FILE[] = "test2.jpg";
const char OUTPUT_JPG_FILE[] = "color_conv.jpg";
const char ORG_OUT_JPG_FILE[] = "org.jpg";

int main(){
    hls::stream<ap_axiu<24,1,1,1> > ins, ins2;
    hls::stream<ap_axiu<24,1,1,1> > ins_soft;
    hls::stream<ap_axiu<24,1,1,1> > outs, outs2;
    hls::stream<ap_axiu<24,1,1,1> > outs_soft;
    ap_axiu<24,1,1,1> pix;
    ap_axiu<24,1,1,1> vals, vals_soft;

    // JPG ファイルをMat に読み込む
    cv::Mat img = cv::imread(INPUT_JPG_FILE);

    // ピクセルを入れる領域の確保
    std::vector<int32_t> rd_bmp(sizeof(int32_t)*img.cols*img.rows);
    std::vector<int32_t> hw_color_conv(sizeof(int32_t)*(img.cols)*(img.rows));
    std::vector<int32_t> sw_color_conv(sizeof(int32_t)*(img.cols)*(img.rows));

    // rd_bmp にJPGのピクセルを代入
    cv::Mat_<cv::Vec3b> dst_vec3b = cv::Mat_<cv::Vec3b>(img);
    for (int y=0; y<img.rows; y++){
        for (int x=0; x<img.cols; x++){
            cv::Vec3b pixel;
            pixel = dst_vec3b(y,x);
            rd_bmp[y*img.cols+x] = (pixel[0] & 0xff) | ((pixel[1] & 0xff)<<8) | ((pixel[2] & 0xff)<<16); // RGB 8 bits
            // blue - pixel[0]; green - pixel[1]; red - pixel[2];
        }
    }

#ifdef COLORCwXilinxVideoStandard
    // ins に入力データを用意する
    for(int i=0; i<5; i++){ // dummy data
        pix.user = 0;
        pix.data = i;
        pix.last = 0;
        pix.user = 0;
        pix.keep = 0x7;
        pix.strb = 0x7;
        ins << pix;
    }
#endif

    for(int j=0; j < img.rows; j++){
        for(int i=0; i < img.cols; i++){
            pix.data = (int32_t)rd_bmp[(j*img.cols)+i];
#ifdef COLORCwXilinxVideoStandard
            if (j==0 && i==0)   // 最初のデータの時に TUSER を 1 にする
                pix.user = 1;
            else
                pix.user = 0;

            if (i == img.cols-1) // 行の最後でTLASTをアサートする
                pix.last = 1;
            else
                pix.last = 0;
#else
            if(j==img.rows-1 && i==img.cols-1)
                pix.last = 1;
            else
                pix.last = 0;
            pix.user = 0;
#endif
            pix.keep = 0x7;
            pix.strb = 0x7;

            ins << pix;
            ins2 << pix;
            ins_soft << pix;
        }
    }

    // RGB の値を 2.0 倍にする
#ifdef COLORCwXilinxVideoStandard
    color_converter_RGB24(ins, outs, COLOR_CONVwAxiVdma, img.rows, img.cols, 2.0, 2.0, 2.0); // ハードウェアのメディアンフィルタ
    color_converter_RGB24_soft(ins_soft, outs_soft, COLOR_CONVwAxiVdma, img.rows, img.cols, 2.0, 2.0, 2.0);  // ソフトウェアのメディアンフィルタ
#else
    color_converter_RGB24(ins, outs, COLOR_CONVwAxiDma, img.rows, img.cols, 2.0, 2.0, 2.0); // ハードウェアのメディアンフィルタ
    color_converter_RGB24_soft(ins_soft, outs_soft, COLOR_CONVwAxiDma, img.rows, img.cols, 2.0, 2.0, 2.0);  // ソフトウェアのメディアンフィルタ
#endif

    // ハードウェアとソフトウェアのメディアンフィルタの値のチェック
    for (int y=0; y<img.rows; y++){ // 結果の画像サイズはx-2, y-2
        for (int x=0; x<img.cols; x++){
            outs >> vals;
            outs_soft >> vals_soft;
            ap_uint<32> val = vals.data;
            hw_color_conv[y*img.cols+x] = (int32_t)val;
            if (val != vals_soft.data){
                printf("ERROR HW and SW results mismatch x = %ld, y = %ld, HW = %x, SW = %x\n",
                        x, y, val, vals_soft.data);
                return(1);
            }
        }
    }
    printf("Success HW and SW results match\n");

    const int color_conv_row = img.rows;
    const int color_conv_cols = img.cols;
    cv::Mat wbmpf(color_conv_row, color_conv_cols, CV_8UC3);
    // wbmpf に色変換後の画像を入力
    cv::Mat_<cv::Vec3b> sob_vec3b = cv::Mat_<cv::Vec3b>(wbmpf);
    for (int y=0; y<wbmpf.rows; y++){
        for (int x=0; x<wbmpf.cols; x++){
            cv::Vec3b pixel;
            pixel = sob_vec3b(y,x);
            int32_t rbg = hw_color_conv[y*wbmpf.cols+x];
            pixel[0] = (rbg & 0xff); // blue
            pixel[1] = ((rbg >> 8) & 0xff); // green
            pixel[2] = ((rbg >> 16) & 0xff); // red
            sob_vec3b(y,x) = pixel;
        }
    }

    // ハードウェアのメディアンフィルタの結果を jpg ファイルへ出力する
    cv::imwrite(OUTPUT_JPG_FILE, wbmpf);

#ifdef COLORCwXilinxVideoStandard
    color_converter_RGB24(ins2, outs2, ORG_IMGwAxiVdma, img.rows, img.cols, 1.0, 1.0, 1.0); // ハードウェアのメディアンフィルタ
#else
    color_converter_RGB24(ins2, outs2, ORG_IMGwAxiDma, img.rows, img.cols, 1.0, 1.0, 1.0); // ハードウェアのメディアンフィルタ
#endif

    cv::Mat wbmpf2(color_conv_row, color_conv_cols, CV_8UC3);
    // wbmpf2 に元画像を入力
    sob_vec3b = cv::Mat_<cv::Vec3b>(wbmpf2);
    for (int y=0; y<wbmpf.rows; y++){
        for (int x=0; x<wbmpf.cols; x++){
            cv::Vec3b pixel;
            pixel = sob_vec3b(y,x);
            outs2 >> vals;
            int32_t val = vals.data;
            pixel[0] = (val & 0xff); // blue
            pixel[1] = ((val >> 8) & 0xff); // green
            pixel[2] = ((val >> 16) & 0xff); // red
            sob_vec3b(y,x) = pixel;
        }
    }

    // 元画像を jpg ファイルへ出力する
    cv::imwrite(ORG_OUT_JPG_FILE, wbmpf2);

    return(0);
}

int color_converter_RGB24_soft(hls::stream<ap_axiu<24,1,1,1> >& ins,
        hls::stream<ap_axiu<24,1,1,1> >& outs, int32_t function,
        int32_t row_size, int32_t col_size,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag){
    ap_axiu<24,1,1,1> pix;
    ap_axiu<24,1,1,1> colorc;
    ap_uint<24> val;

    LOOP_WAIT_USER : do {   // user が 1になった時にフレームがスタートする
        ins >> pix;
        if(function==ORG_IMGwAxiDma || function==COLOR_CONVwAxiDma)
            break;
    } while(pix.user == 0);

    LOOP_Y: for(int y=0; y<row_size; y++){
        LOOP_X: for(int x=0; x<col_size; x++){
            if (!(x==0 && y==0))    // 最初の入力はすでに入力されている
                ins >> pix; // AXI4-Stream からの入力

            color_conv_soft(pix.data, val, red_mag, green_mag, blue_mag);
            colorc.data = val;

            if(function==ORG_IMGwAxiVdma || function == COLOR_CONVwAxiVdma){
                if(x==0 && y==0) // 最初のピクセル
                    colorc.user = 1;
                else
                    colorc.user = 0;
                if(x == (col_size-1)) // 行の最後
                    colorc.last = 1;
                else
                    colorc.last = 0;
            }else{
                colorc.user = 0;
                colorc.last = pix.last;
            }
            colorc.keep = 0x7;
            colorc.strb = 0x7;
            if(function==COLOR_CONVwAxiVdma || function==COLOR_CONVwAxiDma)
                outs << colorc;
            else
                outs << pix;
        }
    }
    return(0);
}

// color_converter
// data の値に reg_mag を掛けて新しい赤の値とする。緑、青も同様
int color_conv_soft(ap_uint<24> data, ap_uint<24> &val,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> red_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> green_mag,
        ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> blue_mag){
    ap_uint<8> r, g, b;
    ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT> calc_r, calc_g, calc_b;
    ap_uint<8> rt, gt, bt;

    separate_rgb_soft(data, r, g, b);
    calc_r = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)r * red_mag;
    calc_g = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)g * green_mag;
    calc_b = (ap_ufixed<16, 8, AP_TRN_ZERO, AP_SAT>)b * blue_mag;
    rt = calc_r;
    gt = calc_g;
    bt = calc_b;

    val = ((ap_uint<24>)rt << 16)+((ap_uint<24>)gt << 8)+(ap_uint<24>)bt;
    return(0);
}

// separate_rgb
// RGBを分離する
// RBGのフォーマットは、{R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
//
int separate_rgb_soft(ap_uint<24> rgb, ap_uint<8> &r, ap_uint<8> &g, ap_uint<8> &b){
    b = (ap_uint<8>)(rgb & 0xff);
    g = (ap_uint<8>)((rgb>>8) & 0xff);
    r = (ap_uint<8>)((rgb>>16) & 0xff);
    return(0);
}


Vitis HLS 2023.1 で color_converter_RGB24 プロジェクトを作成した。このプロジェクトは ZUBoard 1CG 用だ。
zub1cg_pynq_234_230904.png

今回のテストベンチ・コードでは OpenCV ライブラリを使用している。
Vitis HLS 2023.1 には内蔵された OpenCV は無いので、別にインストールした OpenCV を指定する。
Vitis HLS の Project メニューから Project Settings... を選択して、Project Settings ダイアログを開いた。
Simulation タブを開いて、sobel_axis_RGB24_tb.cpp の CFLAGS に

-I/usr/local/include

を設定した。
Linker Flags に

-L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc

を設定した。

更に、 Synthesis をクリックして、 Top Function に color_converter_RGB24 を指定した。
  1. 2023年09月04日 04:53 |
  2. Vitis HLS
  3. | トラックバック:0
  4. | コメント:0