Kintex-7 上基于 HLS 2018.3 的 Bayer 转 RGB 代码示例
#include "hls_video.h"
#define WIDTH 640 #define HEIGHT 480
typedef ap_axiu<24,1,1,1> AXI_STREAM;
void bayer2rgb(hls::stream<AXI_STREAM>& stream_in, hls::stream<AXI_STREAM>& stream_out) { #pragma HLS INTERFACE axis port=stream_in #pragma HLS INTERFACE axis port=stream_out
hls::Mat<HEIGHT, WIDTH, HLS_8UC1> bayer(HEIGHT, WIDTH);
hls::Mat<HEIGHT, WIDTH, HLS_8UC3> rgb(HEIGHT, WIDTH);
AXI_STREAM in_data;
AXI_STREAM out_data;
// Convert AXI4 Stream to OpenCV Mat
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
#pragma HLS PIPELINE
stream_in.read(in_data);
bayer.data[i][j] = in_data.data;
}
}
// Bayer to RGB conversion
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
#pragma HLS PIPELINE
if (i % 2 == 0 && j % 2 == 0) {
rgb.data[i][j*3] = bayer.data[i][j]; // R
rgb.data[i][j*3+1] = (bayer.data[i][j] + bayer.data[i][j+1]) / 2; // G
rgb.data[i][j*3+2] = bayer.data[i][j+1]; // B
}
else if (i % 2 == 0 && j % 2 == 1) {
rgb.data[i][j*3] = (bayer.data[i][j-1] + bayer.data[i][j]) / 2; // R
rgb.data[i][j*3+1] = bayer.data[i][j]; // G
rgb.data[i][j*3+2] = (bayer.data[i][j] + bayer.data[i][j+1]) / 2; // B
}
else if (i % 2 == 1 && j % 2 == 0) {
rgb.data[i][j*3] = (bayer.data[i-1][j] + bayer.data[i][j]) / 2; // R
rgb.data[i][j*3+1] = bayer.data[i][j]; // G
rgb.data[i][j*3+2] = (bayer.data[i][j] + bayer.data[i+1][j]) / 2; // B
}
else {
rgb.data[i][j*3] = bayer.data[i][j-1]; // R
rgb.data[i][j*3+1] = (bayer.data[i][j-1] + bayer.data[i][j]) / 2; // G
rgb.data[i][j*3+2] = bayer.data[i][j]; // B
}
}
}
// Convert OpenCV Mat to AXI4 Stream
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
#pragma HLS PIPELINE
out_data.data = rgb.data[i][j*3] + (rgb.data[i][j*3+1] << 8) + (rgb.data[i][j*3+2] << 16);
out_data.keep = 0xFF;
out_data.strb = 0xFF;
out_data.user = 0;
out_data.last = 0;
out_data.id = 0;
out_data.dest = 0;
stream_out.write(out_data);
}
}
}
原文地址: http://www.cveoy.top/t/topic/p3bz 著作权归作者所有。请勿转载和采集!