OpenCV中的Mat数据与Matlab中的数据的相互转换
来源:互联网 发布:qq刷人气软件 编辑:程序博客网 时间:2024/05/22 02:08
一、概述
OpenCV中的图像数据格式为Mat类型,该数据结构中的数据存储为行优先,而Matlab中的图像数据存储时是列优先。而很多文献的开源代码是基于matlab完成,当我们需要将其转换为C/C++时,可能会牵涉到数据格式的匹配问题,因此初步实现了一个类,完成两者之间数据转换。
二、代码实现
1.头文件
//Mat2Cell.h#ifndef __MAT2CELL_H__#define __MAT2CELL_H__#include <opencv2/opencv.hpp>using namespace cv;class Mat2Cell{public:Mat2Cell();Mat2Cell(int _width, int _height, int _channel = 1);Mat2Cell(const Mat &src);Mat2Cell(const Mat2Cell &src);Mat2Cell& operator=(const Mat2Cell &other);~Mat2Cell();void fromcvMat(const Mat &src);Mat tocvMat() const;uchar * chan(int k = 0);uchar at(int row, int col, int channel);void crop(int r1, int r2, int c1, int c2);void create(int _width, int _height, int _channel = 1);void release();int total() const;private:void copyFromMatRawData(const uchar * pMat);void copyToMatRawData(uchar * output) const;public:int width;int height;int channel;uchar *pData;};inline int Mat2Cell::total() const{return width*height*channel;}inline uchar * Mat2Cell::chan(int k /* = 0 */){return pData + k*width*height;}inline uchar Mat2Cell::at(int row, int col, int channel){return pData[channel*width*height + col*height + row];}#endif2.源文件
//Mat2Cell.cpp#include "Mat2Cell.h"Mat2Cell::Mat2Cell(){width= 0;height= 0;channel = 0;pData= NULL;}Mat2Cell::Mat2Cell(int _width, int _height, int _channel /* = 1 */){pData = NULL;create(_width, _height, _channel);}Mat2Cell::Mat2Cell(const Mat &src){pData = NULL;create(src.cols, src.rows, src.channels());copyFromMatRawData(src.data);}Mat2Cell::Mat2Cell(const Mat2Cell &src){create(src.width, src.height, src.channel);memcpy(pData, src.pData, total());}Mat2Cell& Mat2Cell::operator=(const Mat2Cell &other){if (total() != other.total()){delete[]pData;pData = new uchar[other.total()];memset(pData, 0, other.total());}width = other.width;height = other.height;channel = other.channel;if (other.pData == NULL){pData = NULL;}else{memcpy(pData, other.pData, total());}return *this;}void Mat2Cell::fromcvMat(const Mat &src){create(src.cols, src.rows, src.channels());copyFromMatRawData(src.data);}Mat Mat2Cell::tocvMat() const{Mat result;result.create(height, width, CV_8UC(channel));copyToMatRawData(result.data);return result;}void Mat2Cell::crop(int r1, int r2, int c1, int c2){int tr = height, tc = width;height= r2 - r1;width= c2 - c1;uchar *u = new uchar[height*width*channel];for (int k = 0; k < channel; k++){uchar *p = u + k*width*height;for (int w = 0; w < width; w++){memcpy(p, pData + k*tr*tc + (c1 + w)*tr + r1, height);p += height;}}delete[]pData;pData = u;}void Mat2Cell::create(int _width, int _height, int _channel /* = 1 */){release();width= _width;height= _height;channel = _channel;pData = new uchar[total()];memset(pData, 0, total());}Mat2Cell::~Mat2Cell(){release();}void Mat2Cell::release(){if (pData == NULL) return;delete[]pData;width = 0; height = 0; channel = 0;pData = NULL;}void Mat2Cell::copyFromMatRawData(const uchar* pMat){for (int k = 0; k < channel; k++){for (int w = 0; w < width; w++){for (int h = 0; h < height; h++){pData[k*width*height + w*height + h] = pMat[h*channel*width + w*channel + k];}}}}void Mat2Cell::copyToMatRawData(uchar * output) const{for (int h = 0; h < height; h++){for (int w = 0; w < width; w++){for (int k = 0; k < channel; k++){output[h*channel*width + w*channel + k] = pData[k*width*height + w*height + h];}}}}
3.主函数
#include <iostream>#include <ctime>#include <cstdlib>#include <opencv2/opencv.hpp>#include "Mat2Cell.h"using namespace std;using namespace cv;int main(){//用随机数构造一个Matvector<uchar> matData;srand((unsigned int)time(NULL));for (int i = 0; i < 48; i++){uchar temp = rand() % 255;matData.push_back(temp);}Mat src = Mat(4, 4, CV_8UC3, matData.data());Mat2Cell mat2cell(src); //数据转换//验证结果int width= mat2cell.width;int height= mat2cell.height;int channel = mat2cell.channel;uchar *pData = mat2cell.pData;for (int k = 0; k < channel; k++){cout << "+++++++++++++++++++++++++++++++++++" << endl;for (int w = 0; w < width; w++){for (int h = 0; h < height; h++){cout << (int)pData[k*width*height + w*height + h] << "\t";}cout << endl;}}system("pause");return 0;}
三、结果验证
1.随机数构造的Mat
2.打印结果
阅读全文
0 0
- OpenCV中的Mat数据与Matlab中的数据的相互转换
- openCV中的Mat、IplImage数据的转换
- 将MATLAB中的mat数据转换为OpenCV能够读取的xml数据
- 访问opencv的Mat中的图像数据
- .mat,.txt,.csv 数据转换为weka中的arff格式及matlab和Weka之间相互转换格式
- FFmpeg中数据与OpenCV中Mat数据的转换
- Opencv Mat与Iplimage的相互转换
- 【OpenCV】IplImage与Mat的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- Opencv Mat与Iplimage的相互转换
- TXT数据转OpenCV中的Mat数据
- 【OpenCV】中的数据型别(Mat数据的读取)
- Qt 中的image 和 Opencv 中的 Mat 之间的相互转换
- R语言读取Matlab中的mat数据
- QImage与OpenCV中的MAT图像格式转换
- 第8周 项目4-稀疏矩阵的三元组表示的实现及应用(2)
- 管道通信
- [知了堂学习笔记]_JavaScript之DOM事件(许愿墙)
- Android 内存优化简介
- 杂项1
- OpenCV中的Mat数据与Matlab中的数据的相互转换
- c++练习-类对象对比大小
- 无状态和有状态详解 以及多线程单线程详解
- appium关于定位元素
- linux设置环境变量动态库搜索路径
- vue网络请求加loading引起的小想
- unity之shader
- Android App优化之ANR详解
- (一)原理