from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pynq import allocate, Overlay
sobel_filter = Overlay("./sobel.bit")
dma = sobel_filter.axi_dma_0
sobel = sobel_filter.sobel_axis_RGB24_0
image_path = "./test2.jpg"
original_image = Image.open(image_path)
canvas = plt.gcf()
size = canvas.get_size_inches()
canvas.set_size_inches(size*2)
width, height = original_image.size
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(original_image)
in_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
out_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
in_buffer[:] = np.array(original_image)
def run_kernel():
dma.sendchannel.transfer(in_buffer)
dma.recvchannel.transfer(out_buffer)
sobel.write(0x00,0x01) # start
dma.sendchannel.wait()
dma.recvchannel.wait()
print(height)
print(width)
sobel.register_map.row_size = height
sobel.register_map.col_size = width
sobel.register_map.function_r = 3 # SOBELwAxiDma
run_kernel()
sobel_image = Image.fromarray(out_buffer)
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(sobel_image)
del in_buffer
del out_buffer
をsobel.register_map.function_r = 3 # SOBELwAxiDma
に変更して、動作させてみたところ、元画像が表示できた。これも成功だ。sobel.register_map.function_r = 2 # ORG_IMGwAxiDma
// sobel_axis_RGB24.h
// 2022/03/20 by marsee
// 2022/03/26 : axi_vdma と axi dma 用に分けた
//
#ifndef __SOBEL_AXIS_RGB24_H__
#define __SOBEL_AXIS_RGB24_H__
#define HORIZONTAL 0
#define VERTICAL 1
#define ORG_IMGwAxiVdma 0
#define SOBELwAxiVdma 1
#define ORG_IMGwAxiDma 2
#define SOBELwAxiDma 3
#endif
// sobel_axis_RGB24.cpp
// 2022/03/20 by marsee
// Up to HD resolution
// 2022/03/23 : Added keep and strb
// 2022/03/26 : axi_vdma と axi dma 用に分けた
//
#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;
if(function==ORG_IMGwAxiDma || function==SOBELwAxiDma)
break;
} while(pix.user == 0);
LOOP_Y: for(int y=0; y<row_size; y++){
#pragma HLS LOOP_TRIPCOUNT avg=600 max=600 min=600
LOOP_X: for(int x=0; x<col_size; x++){
#pragma HLS LOOP_TRIPCOUNT avg=800 max=800 min=800
#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(function==ORG_IMGwAxiVdma || function == SOBELwAxiVdma){
if(x==0 && y==0) // 最初のピクセル
sobel.user = 1;
else
sobel.user = 0;
if(x == (col_size-1)) // 行の最後
sobel.last = 1;
else
sobel.last = 0;
}else{
sobel.user = 0;
sobel.last = pix.last;
}
sobel.keep = 0x7;
sobel.strb = 0x7;
if(function==SOBELwAxiVdma || function==SOBELwAxiDma)
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
// 2022/03/26 : 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 "sobel_axis_RGB24.h"
#define SOBELwXilinxVideoStandard
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];
}
}
#ifdef SOBELwXilinxVideoStandard
// 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 = (ap_int<32>)rd_bmp[(j*img.cols)+i];
#ifdef SOBELwXilinxVideoStandard
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;
}
}
#ifdef SOBELwXilinxVideoStandard
sobel_axis_RGB24(ins, outs, SOBELwAxiVdma, img.rows, img.cols); // ハードウェアのソーベルフィルタ
#else
sobel_axis_RGB24(ins, outs, SOBELwAxiDma, img.rows, img.cols); // ハードウェアのソーベルフィルタ
#endif
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);
#ifdef SOBELwXilinxVideoStandard
sobel_axis_RGB24(ins2, outs2, ORG_IMGwAxiVdma, img.rows, img.cols); // 元画像出力
#else
sobel_axis_RGB24(ins2, outs2, ORG_IMGwAxiDma, img.rows, img.cols); // 元画像出力
#endif
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
tuser は使用しない。
データ転送の最後のデータの時に tlast を 1 にする。
tkeep, tstrb はオール1にする。
で止まっていた。dma.recvchannel.wait()
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pynq import allocate, Overlay
sobel_filter = Overlay("./sobel.bit")
dma = sobel_filter.axi_dma_0
sobel = sobel_filter.sobel_axis_RGB24_0
image_path = "./test2.jpg"
original_image = Image.open(image_path)
canvas = plt.gcf()
size = canvas.get_size_inches()
canvas.set_size_inches(size*2)
width, height = original_image.size
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(original_image)
in_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
out_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
in_buffer[:] = np.array(original_image)
def run_kernel():
dma.sendchannel.transfer(in_buffer)
dma.recvchannel.transfer(out_buffer)
sobel.write(0x00,0x01) # start
dma.sendchannel.wait()
dma.recvchannel.wait()
print(height)
print(width)
sobel.register_map.row_size = height
sobel.register_map.col_size = width
sobel.register_map.function_r = 1
run_kernel()
sobel_image = Image.fromarray(out_buffer)
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(sobel_image)
del in_buffer
del out_buffer
// 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
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pynq import allocate, Overlay
check_RGBd = Overlay("./check_RGB.bit")
dma = check_RGBd.axi_dma_0
chRGB = check_RGBd.choose_RGB_0
image_path = "./RGB_test.png"
original_image = Image.open(image_path)
canvas = plt.gcf()
size = canvas.get_size_inches()
canvas.set_size_inches(size*2)
width, height = original_image.size
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(original_image)
in_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
out_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
in_buffer[:] = np.array(original_image)
def run_kernel():
dma.sendchannel.transfer(in_buffer)
dma.recvchannel.transfer(out_buffer)
chRGB.write(0x00,0x01) # start
dma.sendchannel.wait()
dma.recvchannel.wait()
print(height)
print(width)
chRGB.register_map.row_size = height
chRGB.register_map.col_size = width
chRGB.register_map.choose_rgb_val = 0
run_kernel()
chRGB_image = Image.fromarray(out_buffer)
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(chRGB_image)
del in_buffer
del out_buffer
の場合。chRGB.register_map.choose_rgb_val = 0
の場合。chRGB.register_map.choose_rgb_val = 1
の場合。chRGB.register_map.choose_rgb_val = 2
の場合の実際の波形を見てみよう。chRGB.register_map.choose_rgb_val = 2
// choose_RGB.cpp
// 2021/03/10 by marsee
// 2021/03/16 : Changed the bit width of AXI4-Stream from 32 bits to 24 bits.
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
int choose_RGB(hls::stream<ap_axiu<24,1,1,1> >& ins, hls::stream<ap_axiu<24,1,1,1> >& outs,
int32_t row_size, int32_t col_size, int32_t choose_rgb_val){
#pragma HLS INTERFACE mode=s_axilite port=choose_rgb_val
#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=return
#pragma HLS INTERFACE mode=axis register_mode=both port=outs register
#pragma HLS INTERFACE mode=axis register_mode=both port=ins register
ap_axiu<24,1,1,1> dt;
for(int row=0; row < row_size; row++){
#pragma HLS LOOP_TRIPCOUNT avg=4 max=4 min=4
for(int col=0; col < col_size; col++){
#pragma HLS LOOP_TRIPCOUNT avg=32 max=32 min=32
ins >> dt;
switch(choose_rgb_val){
case 0:
dt.data &= (ap_uint<24>)0xff;
break;
case 1:
dt.data &= (ap_uint<24>)0xff00;
break;
default: // 2
dt.data &= (ap_uint<24>)0xff0000;
break;
}
outs << dt;
}
}
return(0);
}
// choose_RGB_tb.cpp
// 2022/03/10 by marsee
// 2021/03/16 : Changed the bit width of AXI4-Stream from 32 bits to 24 bits.
#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"
int choose_RGB(hls::stream<ap_axiu<24,1,1,1> >& ins, hls::stream<ap_axiu<24,1,1,1> >& outs,
int32_t row_size, int32_t col_size, int32_t choose_rgb_val);
const char INPUT_IMG_FILE[] = "RGB_test.bmp";
const char OUTPUT_IMG_FILE[] = "output.bmp";
const int32_t choose_rgb_val = 2;
int main(){
hls::stream<ap_axiu<24,1,1,1> > ins;
hls::stream<ap_axiu<24,1,1,1> > outs;
ap_axiu<24,1,1,1> pix;
ap_axiu<24,1,1,1> vals;
// JPG ファイルをMat に読み込む
cv::Mat img = cv::imread(INPUT_IMG_FILE);
// ピクセルを入れる領域の確保
std::vector<ap_uint<24>> rd_buf(sizeof(uint32_t)*img.cols*img.rows);
std::vector<ap_uint<24>> out_buf(sizeof(uint32_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_buf[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];
}
}
for(int j=0; j < img.rows; j++){
for(int i=0; i < img.cols; i++){
pix.data = rd_buf[(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 = 0xf;
pix.strb = 0xf;
ins << pix;
}
}
choose_RGB(ins, outs, img.rows, img.cols, choose_rgb_val);
// out_buf に結果を代入
for(int j=0; j<img.rows; j++){
for(int i=0; i<img.cols; i++){
outs >> pix;
out_buf[j*img.cols+i] = pix.data;
}
}
// wbmpf に結果を代入
cv::Mat wbmpf(img.rows, img.cols, CV_8UC3);
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);
uint32_t rbg = (int32_t)out_buf[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;
}
}
// 結果を画像ファイルへ出力する
cv::imwrite(OUTPUT_IMG_FILE, wbmpf);
}
まで実行した。check_RGBd = Overlay("./check_RGB.bit")
まで実行したところトリガーがかかった。run_kernel()
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from pynq import allocate, Overlay
check_RGBd = Overlay("./check_RGB.bit")
dma = check_RGBd.axi_dma_0
chRGB = check_RGBd.choose_RGB_0
image_path = "./RGB_test.png"
original_image = Image.open(image_path)
canvas = plt.gcf()
size = canvas.get_size_inches()
canvas.set_size_inches(size*2)
width, height = original_image.size
print("Image size: {}x{} pixels.".format(width, height))
plt.figure(figsize=(6, 5));
_ = plt.imshow(original_image)
in_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
out_buffer = allocate(shape=(height, width, 3),
dtype=np.uint8, cacheable=1)
in_buffer[:] = np.array(original_image)
def run_kernel():
dma.sendchannel.transfer(in_buffer)
dma.recvchannel.transfer(out_buffer)
chRGB.write(0x00,0x01) # start
dma.sendchannel.wait()
dma.recvchannel.wait()
print(height)
print(width)
chRGB.register_map.row_size = height
chRGB.register_map.col_size = width
chRGB.register_map.choose_rgb_val = 0
run_kernel()
chRGB_image = Image.fromarray(out_buffer)
del in_buffer
del out_buffer
で shape が違うと言われてしまった。。。in_buffer[:] = np.array(original_image)
をimage_path = "./RGB_test.png"
に変更した。image_path = "./RGB_test.bmp"
に行ったきりで終了しなかった。run_kernel()
const int32_t choose_rgb_val = 0;
const int32_t choose_rgb_val = 1;
const int32_t choose_rgb_val = 2;
const int32_t choose_rgb_val = 3;
// choose_RGB.cpp
// 2021/03/10 by marsee
//
#include <stdint.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
int choose_RGB(hls::stream<ap_axiu<32,1,1,1> >& ins, hls::stream<ap_axiu<32,1,1,1> >& outs,
int32_t row_size, int32_t col_size, int32_t choose_rgb_val){
#pragma HLS INTERFACE mode=s_axilite port=choose_rgb_val
#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=return
#pragma HLS INTERFACE mode=axis register_mode=both port=outs register
#pragma HLS INTERFACE mode=axis register_mode=both port=ins register
ap_axiu<32,1,1,1> dt;
for(int row=0; row < row_size; row++){
#pragma HLS LOOP_TRIPCOUNT avg=4 max=4 min=4
for(int col=0; col < col_size; col++){
#pragma HLS LOOP_TRIPCOUNT avg=32 max=32 min=32
ins >> dt;
switch(choose_rgb_val){
case 0:
dt.data &= 0xff;
break;
case 1:
dt.data &= 0xff00;
break;
case 2:
dt.data &= 0xff0000;
break;
default:
dt.data &= 0xff000000;
}
outs << dt;
}
}
return(0);
}
// choose_RGB_tb.cpp
// 2022/03/10 by marsee
//
#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"
int choose_RGB(hls::stream<ap_axiu<32,1,1,1> >& ins, hls::stream<ap_axiu<32,1,1,1> >& outs,
int32_t row_size, int32_t col_size, int32_t choose_rgb_val);
const char INPUT_IMG_FILE[] = "RGB_test.png";
const char OUTPUT_IMG_FILE[] = "output.png";
const int32_t choose_rgb_val = 1;
int main(){
hls::stream<ap_axiu<32,1,1,1> > ins;
hls::stream<ap_axiu<32,1,1,1> > outs;
ap_axiu<32,1,1,1> pix;
ap_axiu<32,1,1,1> vals;
// JPG ファイルをMat に読み込む
cv::Mat img = cv::imread(INPUT_IMG_FILE);
// ピクセルを入れる領域の確保
std::vector<uint32_t> rd_buf(sizeof(uint32_t)*img.cols*img.rows);
std::vector<uint32_t> out_buf(sizeof(uint32_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_buf[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];
}
}
for(int j=0; j < img.rows; j++){
for(int i=0; i < img.cols; i++){
pix.data = (int32_t)rd_buf[(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 = 0xf;
pix.strb = 0xf;
ins << pix;
}
}
choose_RGB(ins, outs, img.rows, img.cols, choose_rgb_val);
// out_buf に結果を代入
for(int j=0; j<img.rows; j++){
for(int i=0; i<img.cols; i++){
outs >> pix;
out_buf[j*img.cols+i] = pix.data;
}
}
// wbmpf に結果を代入
cv::Mat wbmpf(img.rows, img.cols, CV_8UC3);
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);
uint32_t rbg = out_buf[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;
}
}
// 結果を画像ファイルへ出力する
cv::imwrite(OUTPUT_IMG_FILE, wbmpf);
}
-I/usr/local/include
-L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
from pynq import allocate, Overlay
DMA_pow2_axis_i = Overlay("./DMA_pow2.bit")
dma = DMA_pow2_axis_i.axi_dma_0
pow2 = DMA_pow2_axis_i.DMA_pow2_axis_0
import numpy as np
data = allocate(shape=(10), dtype=np.uint32)
result = allocate(shape=(10), dtype=np.uint32)
for i in range(10):
data[i] = i
print(data)
print(result)
def run_kernel():
dma.sendchannel.transfer(data)
dma.recvchannel.transfer(result)
pow2.write(0x00,0x01) # start
dma.sendchannel.wait()
dma.recvchannel.wait()
run_kernel()
print(result)
del data
del result
// DMA_pow2_axis.cpp
// 2022/02/22 by marsee
// 2022/02/26 : Added tuser and tlast.
//
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
int DMA_pow2_axis(hls::stream<ap_axis<32,1,1,1> >& data,
hls::stream<ap_axis<32,1,1,1> >& result){
#pragma HLS INTERFACE mode=s_axilite port=return
#pragma HLS INTERFACE mode=axis register_mode=both port=result register
#pragma HLS INTERFACE mode=axis register_mode=both port=data register
ap_axis<32,1,1,1> dt, rlt;
for(int i=0; i<10; i++){
data >> dt;
rlt.data = dt.data * dt.data;
rlt.last = dt.last;
rlt.user = dt.user;
rlt.keep = dt.keep;
rlt.strb = dt.strb;
result << rlt;
}
return(0);
}
// DMA_pow2_axis_tb.cpp
// 2022/02/22 by marsee
// 2022/02/26 : Added tuser and tlast.
//
#include <ap_int.h>
#include <hls_stream.h>
#include <ap_axi_sdata.h>
int DMA_pow2_axis(hls::stream<ap_axis<32,1,1,1> >& data,
hls::stream<ap_axis<32,1,1,1> >& result);
int main(){
using namespace std;
hls::stream<ap_axis<32,1,1,1> > data;
hls::stream<ap_axis<32,1,1,1> > result;
ap_axis<32,1,1,1> dt;
ap_axis<32,1,1,1> rlt;
for(int i=0; i<10; i++){
dt.data = i;
if(i == 0)
dt.user = 1;
else
dt.user = 0;
if(i == 9)
dt.last = 1;
else
dt.last = 0;
dt.keep = 0xf;
dt.strb = 0xf;
data << dt;
}
DMA_pow2_axis(data, result);
cout << endl;
for(int i=0; i<10; i++){
result >> rlt;
cout << "data = " << i << " result = " << rlt.data << " user = " << rlt.user
<< " last = " << rlt.last << " keep = " << rlt.keep
<< " strb = " << rlt.strb << endl;
}
cout << endl;
return(0);
}
TKEEP : ザイリンクス IP では、 ヌル バイ ト の利用をパケット化されたストリームの末尾にある残りのバイ トのエンコードに限定しています。
ザイリンクスのエンドポイン ト IP では、 ストリーム中の先頭または途中のヌル バイ ト を示す目的で TKEEP を使用しません。
デフォルト: 1
TSTRB : 変更な し
通常、 TSTRB はスパース ストリーム のエンコードに使用 し ます。TSTRB は、 残りのパケットのエンコード だけを目的として使用すべきではありません。
デフォルト: TKEEP と同じ、 それ以外の場合は 1
from pynq import allocate, Overlay
DMApow2i = Overlay("DMApow2.bit")
DMA_pow2 = DMApow2i.DMA_pow2_0
import numpy as np
data = allocate(shape=(10), dtype=np.uint32, cacheable=1)
result = allocate(shape=(10), dtype=np.uint32, cacheable=1)
data[:] = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(data)
DMA_pow2.write(0x18,data.physical_address) # in_r
DMA_pow2.write(0x20,result.physical_address) # out_r
DMA_pow2.write(0x00,0x01) # start
while True:
status = DMA_pow2.read(0x00)
if status == 0xe:
break
print(result)
del data
del result
// DMA_pow2.cpp
// 2021/11/17 by marsee
#include <stdint.h>
int DMA_pow2(int32_t *in, int32_t *out){
#pragma HLS INTERFACE mode=m_axi depth=10 port=out offset=slave
#pragma HLS INTERFACE mode=m_axi depth=10 port=in offset=slave
#pragma HLS INTERFACE mode=s_axilite port=return
for(int i=0; i<10; i++){
out[i] = in[i] * in[i];
}
return(0);
}
// DMA_pow2_tb.cpp
// 2021/11/17 by marsee
//
#include <iostream>
#include <stdint.h>
int DMA_pow2(int32_t *in, int32_t *out);
int main(){
int32_t data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int32_t result[10];
DMA_pow2(data, result);
for(int i=0; i<10; i++){
std::cout << "data[" << i << "]= " << data[i] <<
", result[" << i << "] = " <<
result[i] << std::endl;
}
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | 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 | - | - |