// sobel_axis_RGB24.cpp
// 2022/03/20 by marsee
// Up to HD resolution
// 2022/03/23 : Added keep and strb
//
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
#include "sobel_axis_RGB24.h"
ap_int<32> sobel_fil(ap_int<32> h_or_v, ap_int<32> x0y0, ap_int<32> x1y0, ap_int<32> x2y0, ap_int<32> x0y1,
ap_int<32> x1y1, ap_int<32> x2y1, ap_int<32> x0y2, ap_int<32> x1y2, ap_int<32> x2y2);
ap_int<32> conv_rbg2y(ap_int<32> rbg);
ap_int<32> square_root8(ap_int<32> val);
int sobel_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> sobel;
ap_int<32> sobel_val, sobel_h_val, sobel_v_val;
ap_int<32> line_buf[2][1920]; // Up to HD resolution
#pragma HLS array_partition variable=line_buf block factor=2 dim=1
ap_int<32> pix_mat[3][3];
#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;
} while(pix.user == 0);
LOOP_Y: for(int y=0; y<row_size; y++){
LOOP_X: for(int x=0; x<col_size; x++){
#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];
ap_int<32> y_val = conv_rbg2y(pix.data);
pix_mat[2][2] = y_val;
line_buf[0][x] = line_buf[1][x]; // 行の入れ替え
line_buf[1][x] = y_val;
sobel_h_val = sobel_fil(HORIZONTAL, 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]);
sobel_v_val = sobel_fil(VERTICAL, 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]);
sobel_val = square_root8(sobel_h_val*sobel_h_val + sobel_v_val*sobel_v_val);
sobel.data = (sobel_val<<16)+(sobel_val<<8)+sobel_val;
if(x<2 || y<2)
sobel.data = 0;
if(x==0 && y==0) // 最初のピクセル
sobel.user = 1;
else
sobel.user = 0;
if(x == (col_size-1)) // 行の最後
sobel.last = 1;
else
sobel.last = 0;
sobel.keep = 0x7;
sobel.strb = 0x7;
if(function == SOBEL_FILTER)
outs << sobel;
else
outs << pix;
}
}
return(0);
}
// RBGからYへの変換
// RBGのフォーマットは、{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 にした
ap_int<32> conv_rbg2y(ap_int<32> rbg){
ap_int<32> r, g, b, y_f;
ap_int<32> y;
b = rbg & 0xff;
g = (rbg>>8) & 0xff;
r = (rbg>>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);
}
// sobel filter
// HORZONTAL
// x0y0 x1y0 x2y0 1 2 1
// x0y1 x1y1 x2y1 0 0 0
// x0y2 x1y2 x2y2 -1 -2 -1
// VERTICAL
// x0y0 x1y0 x2y0 1 0 -1
// x0y1 x1y1 x2y1 2 0 -2
// x0y2 x1y2 x2y2 1 0 -1
ap_int<32> sobel_fil(ap_int<32> h_or_v, ap_int<32> x0y0, ap_int<32> x1y0, ap_int<32> x2y0, ap_int<32> x0y1,
ap_int<32> x1y1, ap_int<32> x2y1, ap_int<32> x0y2, ap_int<32> x1y2, ap_int<32> x2y2){
ap_int<32> y;
if(h_or_v == HORIZONTAL){
y = x0y0 + 2*x1y0 + x2y0 - x0y2 - 2*x1y2 - x2y2;
} else {
y = x0y0 - x2y0 + 2*x0y1 - 2*x2y1 + x0y2 - x2y2;
}
if(y<0)
y = -y;
//y = 0;
else if(y>255) // 8 bits
y = 255;
return(y);
}
// square_root8
// 8 bit幅のsquare_rootを求める
ap_int<32> square_root8(ap_int<32> val){
ap_int<32> temp = 0;
ap_int<32> square;
for(int i=7; i>=0; --i){
temp += (1 << i);
square = temp * temp;
if(square > val){
temp -= (1 << i);
}
}
return(temp);
}
// sobel_axis_RGB24_tb.cpp
// 2022/03/20 by marsee
// 2022/03/23 : Added keep and strb
//
#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 "sobel_axis_RGB24.h"
int sobel_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 sobel_filter_soft(int32_t *cam_fb, int32_t *sobel_fb,
int32_t x_size, int32_t y_size);
int32_t square_root8_soft(int32_t val);
const char INPUT_JPG_FILE[] = "test2.jpg";
const char OUTPUT_JPG_FILE[] = "sobel.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;
// 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_sobel(sizeof(int32_t)*(img.cols)*(img.rows));
std::vector<int32_t> sw_sobel(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];
}
}
// 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;
}
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 (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;
pix.keep = 0x7;
pix.strb = 0x7;
ins << pix;
ins2 << pix;
}
}
sobel_axis_RGB24(ins, outs, SOBEL_FILTER, img.rows, img.cols); // ハードウェアのソーベルフィルタ
sobel_filter_soft(rd_bmp.data(), sw_sobel.data(), img.cols, img.rows); // ソフトウェアのソーベルフィルタ
// ハードウェアとソフトウェアのソーベルフィルタの値のチェック
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_sobel[y*img.cols+x] = (int32_t)val;
if (val != sw_sobel[y*img.cols+x]){
printf("ERROR HW and SW results mismatch x = %ld, y = %ld, HW = %x, SW = %x\n",
x, y, val, sw_sobel[y*(img.cols-2)+x]);
return(1);
}
}
}
printf("Success HW and SW results match\n");
const int sobel_row = img.rows;
const int sobel_cols = img.cols;
cv::Mat wbmpf(sobel_row, sobel_cols, CV_8UC3);
// wbmpf にsobel フィルタ処理後の画像を入力
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_sobel[y*wbmpf.cols+x];
pixel[0] = ((rbg >> 8) & 0xff); // blue
pixel[1] = (rbg & 0xff); // green
pixel[2] = ((rbg >> 16) & 0xff); // red
sob_vec3b(y,x) = pixel;
}
}
// ハードウェアのソーベルフィルタの結果を jpg ファイルへ出力する
cv::imwrite(OUTPUT_JPG_FILE, wbmpf);
sobel_axis_RGB24(ins2, outs2, ORIGINAL_IMAGE, img.rows, img.cols); // 元画像出力
cv::Mat wbmpf2(sobel_row, sobel_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);
}
int32_t sobel_fil_soft(int32_t h_or_v, 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 conv_rbg2y_soft(int32_t rbg);
int sobel_filter_soft(int32_t *cam_fb, int32_t *sobel_fb,
int32_t x_size, int32_t y_size){
int32_t sobel_val, sobel_h_val, sobel_v_val;
int32_t pix[3][3];
for(int y=0; y<y_size; y++){
for(int x=0; x<x_size; x++){
for(int i=2; i>=0; --i){
for(int j=2; j>=0; --j){
if(x>=2 && y>=2)
pix[i][j] = conv_rbg2y_soft(cam_fb[(y-i)*x_size+(x-j)]);
else
pix[i][j] = 0;
}
}
sobel_h_val = sobel_fil_soft(HORIZONTAL,pix[0][0], pix[0][1], pix[0][2],
pix[1][0], pix[1][1], pix[1][2],
pix[2][0], pix[2][1], pix[2][2]);
sobel_v_val = sobel_fil_soft(VERTICAL, pix[0][0], pix[0][1], pix[0][2],
pix[1][0], pix[1][1], pix[1][2],
pix[2][0], pix[2][1], pix[2][2]);
sobel_val = square_root8_soft(sobel_h_val*sobel_h_val + sobel_v_val*sobel_v_val);
if(x<2 || y<2)
sobel_val = 0;
sobel_fb[y*x_size+x] = (sobel_val<<16)+(sobel_val<<8)+sobel_val;
}
}
return(0);
}
// RBGからYへの変換
// RBGのフォーマットは、{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_rbg2y_soft(int32_t rbg){
int32_t r, g, b, y_f;
int32_t y;
b = rbg & 0xff;
g = (rbg>>8) & 0xff;
r = (rbg>>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);
}
// sobel filter
// HORZONTAL
// x0y0 x1y0 x2y0 1 2 1
// x0y1 x1y1 x2y1 0 0 0
// x0y2 x1y2 x2y2 -1 -2 -1
// VERTICAL
// x0y0 x1y0 x2y0 1 0 -1
// x0y1 x1y1 x2y1 2 0 -2
// x0y2 x1y2 x2y2 1 0 -1
int32_t sobel_fil_soft(int32_t h_or_v, 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;
if(h_or_v == HORIZONTAL){
y = x0y0 + 2*x1y0 + x2y0 - x0y2 - 2*x1y2 - x2y2;
} else {
y = x0y0 - x2y0 + 2*x0y1 - 2*x2y1 + x0y2 - x2y2;
}
if(y<0)
y = -y;
//y = 0;
else if(y>255)
y = 255;
return(y);
}
// square_root8_soft
// 8 bit幅のsquare_rootを求める
int32_t square_root8_soft(int32_t val){
int32_t temp = 0;
int32_t square;
for(int i=7; i>=0; --i){
temp += (1 << i);
square = temp * temp;
if(square > val){
temp -= (1 << i);
}
}
return(temp);
}
-I/usr/local/include
-L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 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 |