xf::Mat Image Container Class

柔情痞子 提交于 2020-01-14 23:02:11

xf :: Mat是一个模板类,用作存储图像数据及其属性的容器。
xf :: Mat图像容器类类似于OpenCV库的cv :: Mat类。

Class Definition

template<int T, int ROWS, int COLS, int NPC>
class Mat {

  public:
    unsigned char allocatedFlag;            // flag to mark memory allocation in this class
    int rows, cols, size;                   // actual image size

#ifdef __SDSVHLS__
    typedef XF_TNAME(T,NPC) DATATYPE;
#else                                       // When not being built for V-HLS
    typedef struct {
        XF_CTUNAME(T,NPC) chnl[XF_NPIXPERCYCLE(NPC)][XF_CHANNELS(T,NPC)];
    } __attribute__ ((packed)) DATATYPE;
#endif

//#if (defined  (__SDSCC__) ) || (defined (__SYNTHESIS__))
#if defined (__SYNTHESIS__) && !defined (__SDA_MEM_MAP__)
    DATATYPE *data __attribute((xcl_array_geometry((ROWS)*(COLS>> (XF_BITSHIFT(NPC))))));//data[ ROWS * ( COLS >> ( XF_BITSHIFT ( NPC ) ) ) ];
#else
    DATATYPE *data;
#endif


    Mat();                                  // default constructor
    Mat(Size _sz);
    Mat(int _rows, int _cols);
    Mat(int _size, int _rows, int _cols);
    Mat(int _rows, int _cols, void *_data);
    Mat(const Mat&);                        // copy constructor

    =Mat();

    Mat& operator= (const Mat&);            // Assignment operator
//  XF_TNAME(T, XF_NPPC1) operator() (unsigned int r, unsigned int c);
//  XF_CTUNAME(T, NPC) operator() (unsigned int r, unsigned int c, unsigned int ch);
    XF_TNAME(T,NPC) read(int index);
    float read_float(int index);
    void write(int index, XF_TNAME(T,NPC) val);
    void write_float(int index, float val);

    void init (int _rows, int _cols, bool allocate=true);
    void copyTo (void* fromData);
    unsigned char* copyFrom ();

    const int type() const;
    const int depth() const;
    const int channels() const;

    template<int DST_T>
    void convertTo (Mat<DST_T, ROWS, COLS, NPC> &dst, int otype, double alpha=1, double beta=0);
};

Parameter Descriptions

Parameter Description
rows The number of rows in the image or height of the image.
cols The number of columns in the image or width of the image.
size The number of words stored in the data member. The value is calculated using rows*cols/(number of pixels packed per word).
allocatedFlag Flag for memory allocation status
*data class parameters and the pointer to the words that store the pixels of the image.

下表列出了成员函数及其描述:

Member Functions Description
Mat() This default constructor initializes the Mat object sizes, using the template parameters ROWS and COLS.
Mat(int _rows, int _cols) This constructor initializes the Mat object using arguments _rows and _cols.
Mat(const xf::Mat &_src) This constructor helps clone a Mat object to another. New memory will be allocated for the newly created constructor.
Mat(int _rows, int _cols, void *_data) This constructor initializes the Mat object using arguments _rows, _cols, and _data.
convertTo(Mat <DST_T,ROWS, COLS, NPC> &dst, int otype, double alpha=1, double beta=0) Refer to xf::convertTo <api-reference.html#xf-convertto>
copyTo(* fromData) Copies the data from Data pointer into physically contiguous memory allocated inside the constructor.
copyFrom() Returns the pointer to the first location of the *data member.
read(int index) Readout a value from a given location and return it as a packed (for multi-pixel/clock) value.
read_float(in t index) Readout a value from a given location and return it as a float value
write(int index, XF_TNAME(T,NP C) val) Writes a packed (for multi-pixel/clock) value into the given location.
write_float(i nt index, float val) Writes a float value into the given location.
type() Returns the type of the image.
depth() Returns the depth of the image
channels() Returns number of channels of the image
=Mat() This is a default destructor of the Mat object.

xf::Mat类的模板参数用于设置像素的深度、图像中的通道数、每个word的像素填充数、图像的最大行数和列数。下表列出了模板参数及其描述:

Table xf::Mat Template Parameter Descriptions
Parameters Description
TYPE Type of the pixel data. For example, XF_8UC1 stands for 8-bit unsigned and one channel pixel. More types can be found in include/common/xf_params.h.
HEIGHT Maximum height of an image.
WIDTH Maximum width of an image.
NPC The number of pixels to be packed per word. For instance, XF_NPPC1 for 1 pixel per word; and XF_NPPC8 for 8 pixels per word.

Pixel-Level Parallelism

在xfOpenCV的函数中要实现的并行性数量保持为可配置参数。 在大多数功能中,有两个用于处理数据的选项。

  • Single-pixel processing
  • Processing eight pixels in parallel

下表描述了可用于指定特定函数所需的并行度级别的选项:

Option Description
XF_NPPC1 Process 1 pixel per clock cycle
XF_NPPC2 Process 2 pixels per clock cycle
XF_NPPC4 Process 4 pixels per clock cycle
XF_NPPC8 Process 8 pixels per clock cycle

Macros to Work With Parallelism

There are two macros that are defined to work with parallelism.

The XF_NPIXPERCYCLE(flags) macro resolves to the number of pixels processed per cycle.

  • XF_NPIXPERCYCLE(XF_NPPC1) resolves to 1
  • XF_NPIXPERCYCLE(XF_NPPC2) resolves to 2
  • XF_NPIXPERCYCLE(XF_NPPC4) resolves to 4
  • XF_NPIXPERCYCLE(XF_NPPC8) resolves to 8

The XF_BITSHIFT(flags) macro resolves to the number of times to shift the image size to right to arrive at the final data transfer size for parallel processing.

  • XF_BITSHIFT(XF_NPPC1) resolves to 0
  • XF_BITSHIFT(XF_NPPC2) resolves to 1
  • XF_BITSHIFT(XF_NPPC4) resolves to 2
  • XF_BITSHIFT(XF_NPPC8) resolves to 3

Data Types
数据类型的不同,取决于像素的深度和图像中通道的数量的组合。参数的通用命名法如下所示。
XF_<_Number of bits per pixel><signed (S) or unsigned (U) or float (F)>C<_number of channels>
在这里插入图片描述

Manipulating Data Type

根据每个时钟周期要处理的像素数和类型参数,可能存在不同的数据类型。xfOpenCV库将这些数据类型用于内部处理和xf::Mat类内部。以下是一些受支持的类型:

  • XF_TNAME(TYPE,NPPC) resolves to the data type of the data member of the xf::Mat object. For instance, XF_TNAME(XF_8UC1,XF_NPPC8) resolves to ap_uint<64>.
  • Word width = pixel depth * number of channels * number of pixels to process per cycle (NPPC).
  • XF_DTUNAME(TYPE,NPPC) resolves to the data type of the pixel. For instance, XF_DTUNAME(XF_32FC1,XF_NPPC1) resolves to float.
  • XF_PTSNAME(TYPE,NPPC) resolves to the ‘C’ data type of the pixel. For instance, XF_PTSNAME (XF_16UC1,XF_NPPC2) resolves to unsigned short.

NOTE
ap_uint<>, ap_int<>, ap_fixed<>, and ap_ufixed<> types belong to the high-level synthesis (HLS) library. For more information, see the: High-Level Synthesis (UG902).

Sample Illustration

下面的代码演示了使用Zynq®UltraScale™平台的SDSoC™工具在图像上构建高斯滤波器所需的配置。
注:在实时应用中,如果视频是流式的,建议帧缓冲区的位置为xf::Mat,使用库函数处理。生成的位置指针被传递给显示ip。

xf::imread

imread函数从指定的文件路径加载图像,复制到xf::Mat并返回。如果无法读取图像(由于缺少文件、不适当的权限、不支持或无效的格式),函数将使用非零返回代码和错误语句退出。
NOTE 在类似于Cosim的HLS单机模式中,使用cv::imread后接copyTo函数,而不是xf::imread。
API Syntax

template<int PTYPE, int ROWS, int COLS, int NPC>
xf::Mat<PTYPE, ROWS, COLS, NPC> imread (char *filename, int type)

Parameter Descriptions
下表描述了模板和函数参数。
Table xf::imread Parameter Description

Parameter Description
PTYPE 输入像素类型。值应该与“type”参数的值一致。
ROWS 要读取的图像的最大高度
COLS 要读取的图像的最大宽度
NPC 每个周期要处理的像素数;可能的选项是XF_NPPC1和XF_NPPC8,分别用于1像素和8像素操作。
filename 要加载的文件的名称
type 描述图像的类型 :‘0’ for gray scale,‘1’ for color image

xf::imwrite

函数xf::imwrite将图像从给定的xf::Mat保存到指定的文件中。图像格式是根据文件扩展名选择的。这个函数内部使用cv::imwrite进行处理。因此,cv::imwrite的所有限制也适用于xf::imwrite。

template <int PTYPE, int ROWS, int COLS, int NPC>
void imwrite(const char *img_name, xf::Mat<PTYPE, ROWS, COLS, NPC> &img)

xf::absDiff

函数xf::absDiff计算xf::Mat和cv::Mat的每个像素之间的绝对差异,并返回cv::Mat中的差异值。

template <int PTYPE, int ROWS, int COLS, int NPC>
void absDiff(cv::Mat &cv_img, xf::Mat<PTYPE, ROWS, COLS, NPC>& xf_img, cv::Mat &diff_img )

xf::convertTo

201/5000
xf :: convertTo函数对给定输入图像的每个像素执行位深度转换。 此方法通过适当的转换将源像素值转换为目标数据类型.
dst(x,y)= cast(α(src(x,y)+β))
注意:输出和输入Mat不能相同。也就是说,转换后的图像不能存储在输入图像的Mat中。

template<int DST_T> void convertTo(xf::Mat<DST_T,ROWS, COLS, NPC> &dst, int ctype, double alpha=1, double beta=0)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!