OpenCV简化版Mat实现
来源:互联网 发布:spark安装windows 编辑:程序博客网 时间:2024/06/05 23:03
为了理解OpenCV中的Mat结构,自己写了一个简化版本的Mat。
Mat是个结构体,通常用来保存图片信息,比较难理解的一点是图片信息通常是一个矩阵,以数组形式保存,而这个矩阵和Mat的关系是什么呢?
经过深入研究发现,Mat结构体中的data就是指向数组的指针。
#include<stdio.h>#include<stdlib.h>#include<assert.h>#define uchar unsigned chartypedef struct MyMat{ int type; int step; union { uchar* c; int* i; }data; int height; int width;}MyMat;//初始化头MyMat* mycreateMatHeader(int width, int height, int type){ MyMat* myheader=(MyMat*) malloc(sizeof(MyMat)); myheader->height = height; myheader->width = width; myheader->type = type; if (0 == type) { myheader->step = width*sizeof(uchar); }else if(1==type){ myheader->step = width*sizeof(int); } return myheader;}//为data分配内存void createData(int width,int height,MyMat* mat, int type){ assert(0 == type || 1 == type); if (0 == type) { uchar* p = (uchar*)malloc(sizeof(uchar)*width*height); mat->data.c = p; }else if (1 == type) { int* p = (int *)malloc(sizeof(int)*width*height); mat->data.i = p; }}//myCreateMat包括两个函数,mycreateMatHeader和createDataMyMat* myCreateMat(int width,int height,int type){ //0表示*uchar类型,1表示int* MyMat* retMat=mycreateMatHeader(width,height,type); createData(width,height,retMat,type); return retMat;}MyMat* myInitMatHeader(MyMat* mat, int width, int height, int type, void* data){ mat=myCreateMat(width, height, type); if (0 == type) { mat->data.c =(uchar*) data; } else if (1==type) { mat->data.i = (int *)data; } return mat;}int main(){ MyMat testMat; uchar vals[4] = { '1', '2','3', '4' }; MyMat* p=myInitMatHeader(&testMat, 2, 2, 0, vals); printf("width=%d height=%d\n", p->width, p->height); char* ptr = (char*) p->data.i; printf("step=%d\n", p->step); for (int i = 0; i < p->height; i++) { char* rptr = (char*) (ptr + i*(p->step)); for (int j = 0; j< p->width; j++) { printf("%c\n", *(rptr +j)); } printf("\n"); } return 0;}
0 0
- OpenCV简化版Mat实现
- OpenCV Mat的实现
- Mat 版opencv保存图像
- OpenCV Mat
- opencv-Mat
- Mat--opencv
- opencv---mat
- opencv Mat
- opencv mat
- OpenCV Mat
- opencv-mat
- opencv MAT
- opencv-Mat
- OpenCV: Mat
- OpenCV-----Mat
- opencv mat
- 简化版Vector实现
- 学习OpenCV——Surf简化版
- ORACLE常用Script
- GDRetop的退役感想
- Webview处理302跳转
- java基础之字符流(IO流)
- C语言实现链表之双向链表(十二)判断链表是否为空和获取链表长度
- OpenCV简化版Mat实现
- java 采集网页数据 demo
- Oracle中,日期相加减换算日时分秒
- 问题-MyBatis不识别Integer值为0的数据
- Android中Unable to execute dex: Multiple dex files define Lcom/ta/utdid2/android/utils/AESUtils;解决方法
- [Java学习] java文本文件加密解密类
- CentOS个人版 各种软件安装
- C语言实现链表之双向链表(十三)获取数据对应的结点
- Xcode IB编辑工具参考线