// 2016/04/03 : GRAY_IMAGE を追加
#ifndef __FASTX_H__
#define __FASTX_H__
#include "ap_axi_sdata.h"
#include "hls_video.h"
#define MAX_HEIGHT 600
#define MAX_WIDTH 800
typedef hls::stream<ap_axiu<32,1,1,1> > AXI_STREAM;
typedef hls::Scalar<3, unsigned char> RGB_PIXEL;
typedef hls::Mat<MAX_HEIGHT, MAX_WIDTH, HLS_8UC3> RGB_IMAGE;
typedef hls::Mat<MAX_HEIGHT, MAX_WIDTH, HLS_8UC1> GRAY_IMAGE;
#endif
// fastx.cpp
// 2016/04/02 by marsee
// 2016/04/09 : FAST Corners Detection
#include "fastx.h"
void fastx_corner_det(AXI_STREAM& INPUT_STREAM, AXI_STREAM& OUTPUT_STREAM, int rows, int cols, int threshold) {
#pragma HLS INTERFACE ap_stable port=threshold
#pragma HLS INTERFACE s_axilite port=threshold
#pragma HLS DATAFLOW
#pragma HLS INTERFACE ap_stable port=cols
#pragma HLS INTERFACE ap_stable port=rows
#pragma HLS INTERFACE s_axilite port=return
#pragma HLS INTERFACE axis port=OUTPUT_STREAM
#pragma HLS INTERFACE axis port=INPUT_STREAM
#pragma HLS INTERFACE s_axilite port=cols
#pragma HLS INTERFACE s_axilite port=rows
RGB_IMAGE img_0(rows, cols);
RGB_IMAGE img_1(rows, cols);
RGB_IMAGE img_1_(rows, cols);
#pragma HLS STREAM variable=img_1_.data_stream depth=8192
// FASTX に最大 7 ラインのレイテンシ、Dilate に最大 3 ラインのレイテンシがあるそうだ
// 1ラインのピクセル数X10 ラインのFIFO バッファが必要 800x10 < 8192 (2の13乗)
// http://japan.xilinx.com/support/documentation/application_notes/xapp1167.pdf
// の 10 ページ参照
GRAY_IMAGE img_1g(rows, cols);
GRAY_IMAGE mask(rows, cols);
GRAY_IMAGE dmask(rows, cols);
GRAY_IMAGE img_2g(rows, cols);
RGB_IMAGE img_3(rows, cols);
RGB_PIXEL color(255, 0, 0);
hls::AXIvideo2Mat(INPUT_STREAM, img_0);
hls::Duplicate(img_0, img_1, img_1_);
hls::CvtColor<HLS_BGR2GRAY>(img_1, img_1g);
hls::FASTX(img_1g, mask, threshold, true);
hls::Dilate(mask, dmask);
hls::PaintMask(img_1_, dmask, img_3, color);
hls::Mat2AXIvideo(img_3, OUTPUT_STREAM);
}
// fastx_tb.cpp
// 2016/04/02 by marsee
// OpenCV 2 の Mat を使用したバージョン
// 2016/04/09 : FAST Corners Detection
#include <iostream>
#include "hls_opencv.h"
#include "fastx.h"
using namespace cv;
#define INPUT_IMAGE "test.jpg"
#define OUTPUT_IMAGE "test_result.jpg"
#define OUTPUT_IMAGE_CV "test_result_cv.jpg"
#define THESHOLD_LEVEL 60
void fastx_corner_det(AXI_STREAM& INPUT_STREAM, AXI_STREAM& OUTPUT_STREAM, int rows, int cols, int threshold);
void opencv_fastx_corner_det(Mat& src, Mat& dst, int threshold);
int main (int argc, char** argv) {
// OpenCV で 画像を読み込む
Mat src = imread(INPUT_IMAGE);
AXI_STREAM src_axi, dst_axi;
// Mat フォーマットから AXI4 Stream へ変換
cvMat2AXIvideo(src, src_axi);
// fastx_corner_det() 関数をコール
fastx_corner_det(src_axi, dst_axi, src.rows, src.cols, THESHOLD_LEVEL);
// AXI4 Stream から Mat フォーマットへ変換
// dst は宣言時にサイズとカラー・フォーマットを定義する必要がある
Mat dst(src.rows, src.cols, CV_8UC3);
AXIvideo2cvMat(dst_axi, dst);
// Mat フォーマットからファイルに書き込み
imwrite(OUTPUT_IMAGE, dst);
// opencv_fastx_corner_det() をコール
Mat dst_cv(src.rows, src.cols, CV_8UC3);
opencv_fastx_corner_det(src, dst_cv, THESHOLD_LEVEL);
imwrite(OUTPUT_IMAGE_CV, dst_cv);
// dst と dst_cv が同じ画像かどうか?比較する
for (int y=0; y<src.rows; y++){
Vec3b* dst_ptr = dst.ptr<Vec3b>(y);
Vec3b* dst_cv_ptr = dst_cv.ptr<Vec3b>(y);
for (int x=0; x<src.cols; x++){
Vec3b dst_bgr = dst_ptr[x];
Vec3b dst_cv_bgr = dst_cv_ptr[x];
// bgr のどれかが間違っていたらエラー
if (dst_bgr[0] != dst_cv_bgr[0] || dst_bgr[1] != dst_cv_bgr[1] || dst_bgr[2] != dst_cv_bgr[2]){
printf("x = %d, y = %d, Error dst=%d,%d,%d dst_cv=%d,%d,%d\n", x, y,
dst_bgr[0], dst_bgr[1], dst_bgr[0], dst_cv_bgr[0], dst_cv_bgr[1], dst_cv_bgr[2]);
//return 1;
}
}
}
printf("Test with 0 errors.\n");
return 0;
}
void opencv_fastx_corner_det(Mat& src, Mat& dst, int threshold){
src.copyTo(dst); // 深いコピー
std::vector<Mat> layers;
std::vector<KeyPoint> keypoints;
split(src, layers);
FAST(layers[0], keypoints, threshold, true);
for (int i = 0; i < keypoints.size(); i++) {
rectangle(dst,
Point(keypoints[i].pt.x-1, keypoints[i].pt.y-1),
Point(keypoints[i].pt.x+1, keypoints[i].pt.y+1),
Scalar(255,0), CV_FILLED);
}
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 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 | - | - | - | - | - | - |