// krnl_lap_filter_dmaw.cpp
// 2020/02/08 by marsee
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include <stdint.h>
// RGBからYへの変換
// RGBのフォーマットは、{8'd0, R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int32_t conv_rgb2y(int32_t rgb){
int32_t r, g, b, y_f;
int32_t y;
b = rgb & 0xff;
g = (rgb>>8) & 0xff;
r = (rgb>>16) & 0xff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int32_t laplacian_fil(int32_t x0y0, int32_t x1y0, int32_t x2y0, int32_t x0y1,
int32_t x1y1, int32_t x2y1, int32_t x0y2, int32_t x1y2, int32_t x2y2)
{
int32_t y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
else if (y>255)
y = 255;
return(y);
}
void krnl_lap_filter(hls::stream<ap_axiu<32,0,0,0> >& ins, hls::stream<ap_axiu<32,0,0,0> >& outs,
int32_t x_size, int32_t y_size){
ap_axiu<32,0,0,0> pix;
ap_axiu<32,0,0,0> lap;
int32_t line_buf[2][1920]; // supported HD resolution
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
#pragma HLS resource variable=line_buf core=RAM_2P
int32_t pix_mat[3][3];
#pragma HLS array_partition variable=pix_mat complete
int32_t lap_fil_val;
LOOP_X : for (int y=0; y<y_size; y++){
#pragma HLS LOOP_TRIPCOUNT min=48 max=600
LOOP_Y : for (int x=0; x<x_size; x++){
#pragma HLS LOOP_TRIPCOUNT min=64 max=800
#pragma HLS PIPELINE II=1
ins >> pix; // AXI4-Stream からの入力
Loop4 : for (int k=0; k<3; k++){
Loop5 : for (int m=0; m<2; m++){
#pragma HLS UNROLL
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];
int32_t y_val = conv_rgb2y(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<16)+(lap_fil_val<<8)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==(x_size-1) && y==(y_size-1)) // フレームの最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
outs << lap; // ストリームへ出力
}
}
LOOP_WAIT_LAST: while(pix.last == 0) { // last が 1 になるまで待つ
#pragma HLS PIPELINE II=1
#pragma HLS LOOP_TRIPCOUNT min=1 max=1 avg=1
ins >> pix;
};
}
void dma_write(hls::stream<ap_axiu<32,0,0,0> >& ins, volatile int32_t *outm,
int32_t x_size, int32_t y_size){
ap_axiu<32,0,0,0> pix;
LOOP_DWY: for(int y=0; y<y_size; y++){
#pragma HLS LOOP_TRIPCOUNT min=48 max=600
LOOP_DWX: for(int x=0; x<x_size; x++){
#pragma HLS LOOP_TRIPCOUNT min=64 max=800
#pragma HLS PIPELINE II=1
ins >> pix;
outm[x_size*y+x] = pix.data;
}
}
}
//extern "C" {
void krnl_lap_filter_dmaw(hls::stream<ap_axiu<32,0,0,0> >& ins, volatile int32_t *outm,
int32_t x_size, int32_t y_size){
#pragma HLS DATAFLOW
#pragma HLS INTERFACE m_axi depth=3072 port=outm offset=slave bundle=gmem
#pragma HLS INTERFACE axis register both port=ins
#pragma HLS INTERFACE s_axilite port=y_size bundle=control
#pragma HLS INTERFACE s_axilite port=x_size bundle=control
#pragma HLS INTERFACE s_axilite port=return bundle=control
hls::stream<ap_axiu<32,0,0,0> > lap_stream;
krnl_lap_filter(ins, lap_stream, x_size, y_size);
dma_write(lap_stream, outm, x_size, y_size);
}
//}
// krnl_lap_filter_dmaw_tb.cpp
// 2020/02/08 by marsee
#include "hls_opencv.h"
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
void krnl_lap_filter_dmaw(hls::stream<ap_axiu<32,0,0,0> >& ins, volatile int32_t *outm,
int32_t x_size, int32_t y_size);
void krnl_lap_filter_soft(hls::stream<ap_axiu<32,0,0,0> >& ins, hls::stream<ap_axiu<32,0,0,0> >& outs,
int32_t x_size, int32_t y_size);
const char INPUT_BMP_FILE[] = "test.bmp";
const char OUTPUT_BMP_FILE[] = "lap.bmp";
int main(){
hls::stream<ap_axiu<32,0,0,0> > ins;
hls::stream<ap_axiu<32,0,0,0> > ins_soft;
hls::stream<ap_axiu<32,0,0,0> > outs_soft;
ap_axiu<32,0,0,0> pix;
ap_axiu<32,0,0,0> vals_soft;
// BMPファイルをMat に読み込む
cv::Mat img = cv::imread(INPUT_BMP_FILE);
// ピクセルを入れる領域の確保
std::vector<int32_t> rd_bmp(sizeof(int32_t)*img.cols*img.rows);
std::vector<int32_t> hw_lap(sizeof(int32_t)*(img.cols)*(img.rows));
std::vector<int32_t> sw_lap(sizeof(int32_t)*(img.cols)*(img.rows));
// rd_bmp にBMPのピクセルを代入
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);
// blue - pixel[0]; green - pixel[1]; red - pixel[2];
}
}
// ins に入力データを用意する
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];
if ((i==img.cols-1) && (j==img.rows-1)) // フレームの最後で last をアサートする
pix.last = 1;
else
pix.last = 0;
ins << pix;
ins_soft << pix;
}
}
krnl_lap_filter_dmaw(ins, hw_lap.data(), img.cols, img.rows); // ハードウェアのソーベルフィルタ
krnl_lap_filter_soft(ins_soft, outs_soft,img.cols, img.rows); // ソフトウェアのソーベルフィルタ
// ハードウェアとソフトウェアのソーベルフィルタの値のチェック
for (int y=0; y<img.rows; y++){
for (int x=0; x<img.cols; x++){
ap_int<32> val = hw_lap[y*img.cols+x];
outs_soft >> vals_soft;
ap_int<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 lap_rows = img.rows;
const int lap_cols = img.cols;
cv::Mat wbmpf(lap_rows, lap_cols, CV_8UC3);
// wbmpf にラプラシアンフィルタ処理後の画像を入力
cv::Mat_<cv::Vec3b> lap_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 = lap_vec3b(y,x);
int32_t rgb = hw_lap[y*wbmpf.cols+x];
pixel[0] = (rgb & 0xff); // blue
pixel[1] = (rgb & 0xff00) >> 8; // green
pixel[2] = (rgb & 0xff0000) >> 16; // red
lap_vec3b(y,x) = pixel;
}
}
// ハードウェアのソーベルフィルタの結果を bmp ファイルへ出力する
cv::imwrite(OUTPUT_BMP_FILE, wbmpf);
return(0);
}
// RGBからYへの変換
// RGBのフォーマットは、{8'd0, R(8bits), G(8bits), B(8bits)}, 1pixel = 32bits
// 輝度信号Yのみに変換する。変換式は、Y = 0.299R + 0.587G + 0.114B
// "YUVフォーマット及び YUV<->RGB変換"を参考にした。http://vision.kuee.kyoto-u.ac.jp/~hiroaki/firewire/yuv.html
// 2013/09/27 : float を止めて、すべてint にした
int32_t conv_rgb2y_soft(int32_t rgb){
int32_t r, g, b, y_f;
int32_t y;
b = rgb & 0xff;
g = (rgb>>8) & 0xff;
r = (rgb>>16) & 0xff;
y_f = 77*r + 150*g + 29*b; //y_f = 0.299*r + 0.587*g + 0.114*b;の係数に256倍した
y = y_f >> 8; // 256で割る
return(y);
}
// ラプラシアンフィルタ
// x0y0 x1y0 x2y0 -1 -1 -1
// x0y1 x1y1 x2y1 -1 8 -1
// x0y2 x1y2 x2y2 -1 -1 -1
int32_t laplacian_fil_soft(int32_t x0y0, int32_t x1y0, int32_t x2y0, int32_t x0y1,
int32_t x1y1, int32_t x2y1, int32_t x0y2, int32_t x1y2, int32_t x2y2)
{
int32_t y;
y = -x0y0 -x1y0 -x2y0 -x0y1 +8*x1y1 -x2y1 -x0y2 -x1y2 -x2y2;
if (y<0)
y = -y;
else if (y>255)
y = 255;
return(y);
}
void krnl_lap_filter_soft(hls::stream<ap_axiu<32,0,0,0> >& ins, hls::stream<ap_axiu<32,0,0,0> >& outs,
int32_t x_size, int32_t y_size){
ap_axiu<32,0,0,0> pix;
ap_axiu<32,0,0,0> lap;
int32_t line_buf[2][1920]; // supported HD resolution
int32_t pix_mat[3][3];
int32_t lap_fil_val;
LOOP_X : for (int y=0; y<y_size; y++){
LOOP_Y : for (int x=0; x<x_size; x++){
ins >> pix; // AXI4-Stream からの入力
Loop4 : for (int k=0; k<3; k++){
Loop5 : 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];
int32_t y_val = conv_rgb2y_soft(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
lap_fil_val = laplacian_fil_soft( pix_mat[0][0], pix_mat[0][1], pix_mat[0][2],
pix_mat[1][0], pix_mat[1][1], pix_mat[1][2],
pix_mat[2][0], pix_mat[2][1], pix_mat[2][2]);
lap.data = (lap_fil_val<<16)+(lap_fil_val<<8)+lap_fil_val; // RGB同じ値を入れる
if (x<2 || y<2) // 最初の2行とその他の行の最初の2列は無効データなので0とする
lap.data = 0;
if (x==(x_size-1) && y==(y_size-1)) // フレームの最後で TLAST をアサートする
lap.last = 1;
else
lap.last = 0;
outs << lap; // ストリームへ出力
}
}
LOOP_WAIT_LAST: while(pix.last == 0) { // last が 1 になるまで待つ
ins >> pix;
};
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 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 | - | - | - | - | - | - |