cd /media/masaaki/Ubuntu_Disk/HDL/2023.2/zub1cg/i10filters/
source i10filters.tcl
を指定した。-L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
// edge_enhancement_axis_RGB24.h
// 2023/11/29 by marsee
//
#ifndef __EDGE_ENHANCEMENT_AXIS_RGB24_H__
#define __EDGE_ENHANCEMENT_AXIS_RGB24_H__
#define ORG_IMGwAxiVdma 0
#define EDGE_ENHANCEMENTwAxiVdma 1
#define ORG_IMGwAxiDma 2
#define EDGE_ENHANCEMENTwAxiDma 3
#endif
// edge_enhancement_axis_RGB24.cpp
// 2023/11/29 by marsee
//
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "edge_enhancement_axis_RGB24.h"
constexpr int size = 3;
ap_uint<24> edge_enhancement_fil(ap_uint<24> (&xy)[size][size]);
ap_uint<8> edge_enhancement_file_calc(ap_int<32> (&xy)[size][size]);
ap_uint<32> separate_rgb(ap_uint<24> rbg, ap_int<32> &r, ap_int<32> &g, ap_int<32> &b);
int edge_enhancement_axis_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){
#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> edge_enhancement;
ap_uint<24> line_buf[size-1][1920]; // Up to HD resolution
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
ap_uint<24> pix_mat[size][size];
#pragma HLS array_partition variable=pix_mat complete
LOOP_WAIT_USER : do { // user が 1になった時にフレームがスタートする
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
ins >> pix;
if(function==ORG_IMGwAxiDma || function==EDGE_ENHANCEMENTwAxiDma)
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 からの入力
LOOP_PIX_MAT_K: for(int k=0; k<3; k++){
LOOP_PIX_MAT_M: for(int m=0; m<2; m++){
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
pix_mat[2][2] = pix.data;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = pix.data;
edge_enhancement.data = edge_enhancement_fil(pix_mat);
if(x<2 || y<2)
edge_enhancement.data = 0;
if(function==ORG_IMGwAxiVdma || function == EDGE_ENHANCEMENTwAxiVdma){
if(x==0 && y==0) // 最初のピクセル
edge_enhancement.user = 1;
else
edge_enhancement.user = 0;
if(x == (col_size-1)) // 行の最後
edge_enhancement.last = 1;
else
edge_enhancement.last = 0;
}else{
edge_enhancement.user = 0;
edge_enhancement.last = pix.last;
}
edge_enhancement.keep = 0x7;
edge_enhancement.strb = 0x7;
if(function==EDGE_ENHANCEMENTwAxiVdma || function==EDGE_ENHANCEMENTwAxiDma)
outs << edge_enhancement;
else
outs << pix;
}
}
return(0);
}
// edge_enhancement filter
ap_uint<24> edge_enhancement_fil(ap_uint<24> (&xy)[size][size]){
ap_int<32> pix_1d_r[size][size], pix_1d_b[size][size], pix_1d_g[size][size];
ap_uint<8> y_r, y_b, y_g;
ap_uint<24> y;
for(int i=0; i<size*size; i++){
separate_rgb(xy[i/3][i%3], pix_1d_r[i/3][i%3], pix_1d_g[i/3][i%3], pix_1d_b[i/3][i%3]);
}
y_r = edge_enhancement_file_calc(pix_1d_r);
y_g = edge_enhancement_file_calc(pix_1d_g);
y_b = edge_enhancement_file_calc(pix_1d_b);
return(((ap_uint<24>)y_r << 16) + ((ap_uint<24>)y_g << 8) + (ap_uint<24>)y_b);
}
// edge_enhancement filter
//
// 0 -1 0
// -1 5 -1
// 0 -1 0
ap_uint<8> edge_enhancement_file_calc(ap_int<32> (&xy)[size][size]){
ap_int<32> y;
y = -xy[0][1]
-xy[1][0] +(5 * xy[1][1]) -xy[1][2]
-xy[2][1];
if(y<0)
y = -y;
//y = 0;
else if(y>255) // 8 bits
y = 255;
return((ap_uint<8>)y);
}
// separate_rgb
// RGBを分離する
// RBGのフォーマットは、{R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
//
ap_uint<32> separate_rgb(ap_uint<24> rbg, ap_int<32> &r, ap_int<32> &g, ap_int<32> &b){
b = (ap_int<32>)(rbg & 0xff);
g = (ap_int<32>)((rbg>>8) & 0xff);
r = (ap_int<32>)((rbg>>16) & 0xff);
return(0);
}
// edge_enhancement_axis_RGB24_tb.cpp
// 2023/11/29 by marsee
// EDGE_ENHANCEMENTwXilinxVideoStandard を 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 "edge_enhancement_axis_RGB24.h"
#define EDGE_ENHANCEMENTwXilinxVideoStandard
constexpr int size = 3;
int edge_enhancement_axis_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);
int edge_enhancement_axis_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_uint<24> edge_enhancement_fil_soft(ap_uint<24> (&xy)[size][size]);
ap_uint<8> edge_enhancement_file_calc_soft(ap_int<32> (&xy)[size][size]);
ap_uint<32> separate_rgb_soft(ap_uint<24> rbg, ap_int<32> &r, ap_int<32> &g, ap_int<32> &b);
const char INPUT_JPG_FILE[] = "test2.jpg";
const char OUTPUT_JPG_FILE[] = "edge_enhancement.jpg";
const char ORG_OUT_JPG_FILE[] = "org_image.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_edge_enhancement(sizeof(int32_t)*(img.cols)*(img.rows));
std::vector<int32_t> sw_edge_enhancement(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 EDGE_ENHANCEMENTwXilinxVideoStandard
// 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;
ins2 << pix;
ins_soft << pix;
}
#endif
for(int j=0; j < img.rows; j++){
for(int i=0; i < img.cols; i++){
pix.data = (ap_int<32>)rd_bmp[(j*img.cols)+i];
#ifdef EDGE_ENHANCEMENTwXilinxVideoStandard
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;
}
}
#ifdef EDGE_ENHANCEMENTwXilinxVideoStandard
edge_enhancement_axis_RGB24(ins, outs, EDGE_ENHANCEMENTwAxiVdma, img.rows, img.cols); // ハードウェアのエッジ強調フィルタ
edge_enhancement_axis_RGB24_soft(ins_soft, outs_soft, EDGE_ENHANCEMENTwAxiVdma, img.rows, img.cols); // ソフトウェアのエッジ強調フィルタ
#else
edge_enhancement_axis_RGB24(ins, outs, EDGE_ENHANCEMENTwAxiDma, img.rows, img.cols); // ハードウェアのエッジ強調フィルタ
edge_enhancement_axis_RGB24_soft(ins_soft, outs_soft, EDGE_ENHANCEMENTwAxiDma, img.rows, img.cols); // ソフトウェアのエッジ強調フィルタ
#endif
// ハードウェアとソフトウェアのエッジ強調フィルタの値のチェック
for (int y=0; y<img.rows; y++){
for (int x=0; x<img.cols; x++){
outs >> vals;
ap_uint<32> val = vals.data;
hw_edge_enhancement[y*img.cols+x] = (int32_t)val;
outs_soft >> vals_soft;
ap_uint<32> val_soft = vals_soft.data;
if (val != val_soft){
printf("ERROR HW and SW results mismatch x = %ld, y = %ld, HW = %x, SW = %x\n",
x, y, val, val_soft);
return(1);
}
}
}
printf("Success HW and SW results match\n");
const int edge_enhancement_row = img.rows;
const int edge_enhancement_cols = img.cols;
cv::Mat wbmpf(edge_enhancement_row, edge_enhancement_cols, CV_8UC3);
// wbmpf にedge_enhancement フィルタ処理後の画像を入力
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_edge_enhancement[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 EDGE_ENHANCEMENTwXilinxVideoStandard
edge_enhancement_axis_RGB24(ins2, outs2, ORG_IMGwAxiVdma, img.rows, img.cols); // 元画像出力
#else
edge_enhancement_axis_RGB24(ins2, outs2, ORG_IMGwAxiDma, img.rows, img.cols); // 元画像出力
#endif
cv::Mat wbmpf2(edge_enhancement_row, edge_enhancement_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 edge_enhancement_axis_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_axiu<24,1,1,1> pix;
ap_axiu<24,1,1,1> edge_enhancement;
ap_uint<24> line_buf[size-1][1920]; // Up to HD resolution
ap_uint<24> pix_mat[size][size];
do { // user が 1になった時にフレームがスタートする
ins >> pix;
if(function==ORG_IMGwAxiDma || function==EDGE_ENHANCEMENTwAxiDma)
break;
} while(pix.user == 0);
for(int y=0; y<row_size; y++){
for(int x=0; x<col_size; x++){
if (!(x==0 && y==0)) // 最初の入力はすでに入力されている
ins >> pix; // AXI4-Stream からの入力
for(int k=0; k<3; k++){
for(int m=0; m<2; m++){
pix_mat[k][m] = pix_mat[k][m+1];
}
}
pix_mat[0][2] = line_buf[0][x];
pix_mat[1][2] = line_buf[1][x];
pix_mat[2][2] = pix.data;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = pix.data;
edge_enhancement.data = edge_enhancement_fil_soft(pix_mat);
if(x<2 || y<2)
edge_enhancement.data = 0;
if(function==ORG_IMGwAxiVdma || function == EDGE_ENHANCEMENTwAxiVdma){
if(x==0 && y==0) // 最初のピクセル
edge_enhancement.user = 1;
else
edge_enhancement.user = 0;
if(x == (col_size-1)) // 行の最後
edge_enhancement.last = 1;
else
edge_enhancement.last = 0;
}else{
edge_enhancement.user = 0;
edge_enhancement.last = pix.last;
}
edge_enhancement.keep = 0x7;
edge_enhancement.strb = 0x7;
if(function==EDGE_ENHANCEMENTwAxiVdma || function==EDGE_ENHANCEMENTwAxiDma)
outs << edge_enhancement;
else
outs << pix;
}
}
return(0);
}
// edge_enhancement filter
ap_uint<24> edge_enhancement_fil_soft(ap_uint<24> (&xy)[size][size]){
ap_int<32> pix_1d_r[size][size], pix_1d_b[size][size], pix_1d_g[size][size];
ap_uint<8> y_r, y_b, y_g;
ap_uint<24> y;
for(int i=0; i<size*size; i++){
separate_rgb_soft(xy[i/3][i%3], pix_1d_r[i/3][i%3], pix_1d_g[i/3][i%3], pix_1d_b[i/3][i%3]);
}
y_r = edge_enhancement_file_calc_soft(pix_1d_r);
y_g = edge_enhancement_file_calc_soft(pix_1d_g);
y_b = edge_enhancement_file_calc_soft(pix_1d_b);
return(((ap_uint<24>)y_r << 16) + ((ap_uint<24>)y_g << 8) + (ap_uint<24>)y_b);
}
// edge_enhancement filter
//
// 0 -1 0
// -1 5 -1
// 0 -1 0
ap_uint<8> edge_enhancement_file_calc_soft(ap_int<32> (&xy)[size][size]){
ap_int<32> y;
y = -xy[0][1]
-xy[1][0] +(5 * xy[1][1]) -xy[1][2]
-xy[2][1];
if(y<0)
y = -y;
//y = 0;
else if(y>255) // 8 bits
y = 255;
return((ap_uint<8>)y);
}
// separate_rgb
// RGBを分離する
// RBGのフォーマットは、{R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
//
ap_uint<32> separate_rgb_soft(ap_uint<24> rbg, ap_int<32> &r, ap_int<32> &g, ap_int<32> &b){
b = (ap_int<32>)(rbg & 0xff);
g = (ap_int<32>)((rbg>>8) & 0xff);
r = (ap_int<32>)((rbg>>16) & 0xff);
return(0);
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | - | - | - | - | - | - |