opencv armnn_tf 对接

作者: 今宵花吹雪 | 来源:发表于2019-04-11 19:37 被阅读0次

输入armnn的图像以数组的形式存储,但是如果工程中涉及到图像相关的内容,一般会配合opencv使用,所以需要做到opencv中Mat数据类型和armnn需要的数组类型之间能够相互转换

Mat数据结构详解
参考自https://blog.csdn.net/yang_xian521/article/details/7107786

armnn中数据输入输出形式为一维的数组,维度顺序为NHWC

找到博客 https://blog.csdn.net/guyuealian/article/details/80253066
完美解决问题

vector<_Tp> convertMat2Vector(const cv::Mat &mat)
{
    return (vector<_Tp>)(mat.reshape(1, 1)); //通道数不变,按行转为一行
}

template <typename _Tp>
cv::Mat convertVector2Mat(vector<_Tp> v, int channels, int rows)
{
    cv::Mat mat = cv::Mat(v);                           //将vector变成单列的mat
    cv::Mat dest = mat.reshape(channels, rows).clone(); //PS:必须clone()一份,否则返回出错
    return dest;
}

主要思想是用一维的图像作为中间媒介,mat2vector 先将图像resize成单通道单行,经过处理Mat.data 在内存上连续,可以直接用来构造vector;vector2mat用vector构造mat,然后resize成需要的通道数和size

唯一存在的问题就是我们神经网络的输出图像可能通道数不为1(灰度图像),3(GRB)或者4(GRBA),而cv::resize 中assert了通道数必须为这三种,所以如果通道数不为上述三种情况的话,目前想到的办法就是先对输出vector做一个单通道的提取,保存多个vector,然后分别进行转换。

vector<vector<_Tp> > split_channels(vector<_Tp> vec, int channels){
    vector<vector<_Tp> > result;
    int HxW = vec.size() / channels;
    for(int i = 0; i < channels; i++){
        vector<_Tp> tmp(HxW, 0);
        for(int j = 0; j < HxW; j++){
            int index = j * channels + i;
            tmp[j] = vec[index];
        }
        result.push_back(tmp);
    }
    return result;
}  

相关文章

网友评论

    本文标题:opencv armnn_tf 对接

    本文链接:https://www.haomeiwen.com/subject/rwhoiqtx.html