xilinx xfopencv例程学习笔记

考虑到有别的网友会检索到这个笔记,说明一下,笔记中PL/PS定义在硬件加速函数那块可能不大准确,应该说是在硬件内存中的‘不是SDSCC编译器’与‘是SDSCC编译器’。。。。。。。。。凑合理解。。。

xfopencv--examples

第一个例程学习accumulate

调用sd卡图片到PL中

int main(int argc, char** argv)
{

	if (argc != 3)
	{
		fprintf(stderr,"Invalid Number of Arguments!\nUsage:\n");
		fprintf(stderr,"<Executable Name> <input image path1> <input image path2> \n");
		return -1;
	}

	cv::Mat in_img, in_img1, out_img;
	cv::Mat in_gray, in_gray1, diff;
	in_gray  = cv::imread(argv[1], 0);  // reading image
	in_gray1 = cv::imread(argv[2], 0);  // reading image

	if (in_gray.data == NULL)
	{
		fprintf(stderr,"Cannot open image %s\n", argv[1]);
		return 0;
	}
	if (in_gray1.data == NULL)
	{
		fprintf(stderr,"Cannot open image %s\n", argv[2]);
		return 0;
	}

	cv::Mat inout_gray(in_gray.rows, in_gray.cols, CV_16U, 1);
	cv::Mat inout_gray1(in_gray.rows, in_gray.cols, CV_32FC1, 1);

调用PL图片到PS中

static xf::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1> imgInput1(in_gray1.rows,in_gray1.cols);
static xf::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1> imgInput2(inout_gray.rows,inout_gray.cols);
static xf::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1> imgOutput(out_gray.rows,out_gray.cols);

imgInput1.copyTo(in_gray.data);
imgInput2.copyTo(in_gray1.data);

把图片从PS中提出到PL中

out_gray.data = imgOutput.copyFrom();

把图片从PL保存到sd卡中

imwrite("out_hls.jpg", out_gray);

用sdscc加速硬件时所用的计时手段

headers.h文件
#ifndef _XF_HEADERS_H_
#define _XF_HEADERS_H_

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



#if __SDSCC__
#undef __ARM_NEON__
#undef __ARM_NEON
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#define __ARM_NEON__
#define __ARM_NEON
#else
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#endif


#if __SDSCC__
#include "sds_lib.h"
#define TIME_STAMP_INIT  unsigned int clock_start, clock_end;  clock_start = sds_clock_counter();
#define TIME_STAMP  { clock_end = sds_clock_counter(); printf("elapsed time %lu \n", clock_end-clock_start); clock_start = sds_clock_counter();  }
#endif

#include "common/xf_sw_utils.h"

#endif//_XF_HEADERS_H_

硬件加速函数前后调用时:

    #include "xf_headers.h"

	#if __SDSCC__
		perf_counter hw_ctr;
		 hw_ctr.start();
	#endif
	
	accumulate_accel(imgInput1,imgInput2,imgOutput);

	#if __SDSCC__
	hw_ctr.stop();
	uint64_t hw_cycles = hw_ctr.avg_cpu_cycles();
	#endif

	

硬件函数中可以通过改参数设置每个始终有几个像素并行


#ifndef _XF_ACCUMULATE_CONFIG_H_
#define _XF_ACCUMULATE_CONFIG_H_

#include "hls_stream.h"
#include <ap_int.h>
#include "xf_config_params.h"
#include "common/xf_common.h"
#include "common/xf_utility.h"

#include "imgproc/xf_accumulate_image.hpp"

// Set the image height and width
#define HEIGHT 2160
#define WIDTH  3840

#if NO
#define NPC1 XF_NPPC1
#endif
#if RO
#define NPC1 XF_NPPC8
#endif

#define IN_TYPE XF_8UC1
#define OUT_TYPE XF_16UC1

void accumulate_accel(xf::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1> &imgInput1,xf::Mat<IN_TYPE, HEIGHT, WIDTH, NPC1> &imgInput2,xf::Mat<OUT_TYPE, HEIGHT, WIDTH, NPC1> &imgOutput);

#endif//_XF_ACCUMULATE_CONFIG_H_

样例中多设置一个.h文件来确定参数

/*  set the optimisation type  */
#define NO  1  // Normal Operation
#define RO  0 // Resource Optimized

第二个是canny

硬件加速函数里面提到PS中复制内存,以及PL中复制图片的方式


#include "xf_canny_config.h"

void canny_accel(xf::Mat<XF_8UC1, HEIGHT, WIDTH, INTYPE> &_src,xf::Mat<XF_2UC1, HEIGHT, WIDTH, OUTTYPE> &_dst,xf::Mat<XF_2UC1, HEIGHT, WIDTH, XF_NPPC32> &_dst1,xf::Mat<XF_8UC1, HEIGHT, WIDTH, XF_NPPC8> &_dst2,unsigned char low_threshold,unsigned char high_threshold)
{

#pragma SDS async(1)

	xf::Canny<FILTER_WIDTH,NORM_TYPE,XF_8UC1,XF_2UC1,HEIGHT, WIDTH,INTYPE,OUTTYPE,XF_USE_URAM>(_src,_dst,low_threshold,high_threshold);

#pragma SDS wait(1)


#if __SDSCC__
	_dst1.data = (ap_uint<64>*)_dst.data;#PS复制
#else
	_dst1.copyTo(_dst.data);#PL复制
#endif


	xf::EdgeTracing<XF_2UC1,XF_8UC1,HEIGHT, WIDTH, XF_NPPC32,XF_NPPC8,XF_USE_URAM>(_dst1,_dst2);
}

         第一个xf_canny可能是并行进去的,等待全部完后才能后,再进行下一个硬件加速程序

在canny中有一种txt文件的写法

FILE *fp = fopen("nmsvalues.txt","w");
	int cnt = 0;
	for (int i=0; i<diff.rows-0; i++)
	{
		for(int j=0; j<diff.cols-0; j++)
		{
			uchar v = diff.at<uchar>(i,j);
			fprintf(fp,"%d ",v);
			if (v>0)//(v>0 && v!=127 && v!=128)
				cnt++;
			if (minval > v)
				minval = v;
			if (maxval < v)
				maxval = v;
		}
		fprintf(fp,"\n");
	}

	fclose(fp);
float err_per = 100.0 * (float)cnt / (diff.rows * diff.cols);

	fprintf(stderr,"Minimum error in intensity = %f\n Maximum error in intensity = %f\n Percentage of pixels above error threshold = %f\nNo of Pixels with Error = %d\n",minval,maxval,err_per, cnt);

网上查了

fprintf 是输出到文件,当然,这个文件也可能是虚拟的文件。多一个文件指针FILE*。
printf 是直接输出到标准显示设备,就是屏幕的终端中。

示例:  

  char name[20] = "Mary";
  FILE *out;
  out = fopen( "output.txt", "w" );
  if( out != NULL )
  fprintf( out, "Hello %s\n", name );

不大明白canny里面为啥fclose了,还能fprintf,估计这时相当于printf了

深究可以看https://www.cnblogs.com/zhangyabin---acm/p/3203745.html这篇文章

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值