ひでみのアイデア帳

くだらないことなんだけど、忘れないために・・・

実験アプリのソースコード

opencv_sample.cpp

純粋にCPUで処理されるソースコードの部分。

/*
 * OpenCV フィルタサンプル - ラプラシアン
g++ -o sobel_sample01 sobel_sample01.cpp -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc
 */
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

extern int wrapper_conv_data(unsigned char* img_in, unsigned char* img_out, int width, int height);

int main( int argc, char** argv )
{
  printf("exec: OpenCV_Sample01\n");

  // Read lena-chan with Grayscale
  printf("read: lena.png\n");
  cv::Mat src = cv::imread("lena.png", 0);

  printf("exec: laplacian\n");
  cv::Mat laplacian, tmp;
  cv::Laplacian(src, tmp, CV_32F, 3);
  cv::convertScaleAbs(tmp, laplacian, 1, 0);

  cv::Mat convdata;
  convdata = src.clone();

  unsigned char* img_in  = (unsigned char*)&laplacian.data[0];
  unsigned char* img_out = (unsigned char*)&convdata.data[0];

  printf("exec: conv_data in\n");
  wrapper_conv_data(img_in, img_out, src.cols, src.rows);
  printf("exec: conv_data out\n");

  // Write for laplacian not kuracian
  printf("write: lena_laplacian.png\n");
  cv::imwrite("lena_laplacian1.png", laplacian);
  cv::imwrite("lena_laplacian2.png", convdata);

  printf("finish: OpenCV_Sample01\n");

  return 0;
}

fpga_func_wrapper.c

FPGA化した関数を呼び出すソースコード。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sds_lib.h>

extern int conv_data(unsigned char img_in[1024], unsigned char img_out[1024], int width);

/*
 * Function Wrapper
 */
int wrapper_conv_data(unsigned char* img_in, unsigned char* img_out, int width, int height)
{
    int rslt = 0;

    unsigned char *buf_in, *buf_out;

    if(width > 256){
        rslt = -1;
        return rslt;
    }

    buf_in  = (unsigned char *)sds_alloc(width);
    buf_out = (unsigned char *)sds_alloc(width);

    for(int y = 0; y < height; y++){
//      rslt += conv_data(&img_in[y * width], &img_out[y * width], width);
        printf("line: %d\n", y);
        memcpy(buf_in, &img_in[y * width], width);
        rslt += conv_data(buf_in, buf_out, width); // ここの行がSDSoCによって関数が置き換えられる
        memcpy(&img_out[y * width], buf_out, width);
    }

    sds_free(buf_in);
    sds_free(buf_out);

    return rslt;
}

fpga_func.c

FPGA化する関数ね。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int conv_data(unsigned char img_in[1024], unsigned char img_out[1024], int width)
{
    for(int x = 0; x < width; x++){
        if(img_in[x] < 128){
            img_out[x] = img_in[x] + 128;
        }else{
            img_out[x] = 255;
        }
    }

    return 0;
}

こんな風に3つに分けたのさ。

なぜかは、たぶん、sds_allocするとわかるよ。