OpenCV中Mat数据结构使用举例
#include "stdafx.h"
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
//创建一个用1+3j填充的 7 x 7 复矩阵-----1
Mat M(7, 7, CV_32FC2, Scalar(1,3));
//现在将 M转换为100 x 60的CV_8UC(15)的矩阵,旧内容将会被释放
M.create(100, 60, CV_8UC(15));//不能为矩阵设置初值
//第 5行,乘以 3,加到第 3 行,
M.row(3) = M.row(3) + M.row (5) * 3;
//现在将第7列复制到第1列, M.col(1) = M.col(7);//这个不能实现,对列操作时要新建一个Mat
Mat M1 = M.col(1);
M.col(7).copyTo(M1);
//创建一种新的 320 x 240 图像-----2
Mat img(Size(320,240), CV_8UC3, Scalar::all(255));
string strWindowName = "ShowImage";
namedWindow(strWindowName, WINDOW_AUTOSIZE);
imshow(strWindowName, img);
waitKey(0);
//选择ROI(region of interest)
Mat roi(img, Rect(10, 10, 100, 100));
//填充 (0,255,0) 的ROI (这是RGB 空间中的绿色),320 x 240 原始图像将被修改
roi = Scalar(0, 255, 0) ;
imshow(strWindowName, img);
waitKey(0);
//获取数组中的子块-----3
Mat A = Mat::eye(10, 10, CV_32S);
//提取 A 的1 (含)到 3 (不包含)列
Mat B = A(Range::all(), Range(1, 3));
//提取 B 的5 (含)到 9 (不包含)行,即 C ~ A(Range(5,9),Range (1,3))
Mat C = B(Range(5, 9), Range::all());
Size size;
Point ofs;
C.locateROI(size, ofs);//使用locateROI() 计算子数组在主容器数组中的相对的位置
cout<<size.width<<" "<<size.height<<" "<<ofs.x<<" "<<ofs.y<<endl;
//快速初始化小矩阵-----4
double m[3][3] = {{1, 2, 3}, {1, 2, 5}, {3, 4, 6}};
Mat M2 = Mat(3, 3, CV_64F, m);//.inv();
Mat E = Mat::eye(4, 4, CV_64F);
cout<<"E = "<<endl<<" "<<E<<endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout<<"O = "<<endl<<" "<<O<<endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout<<"Z = "<<endl<<" "<<Z<<endl;
//IplImage、Mat、CvMat互转-----5
IplImage *img1 = cvLoadImage("aa.jpg", 2 | 4);
Mat mtx(img1);//IplImage *-> Mat,新的Mat类型与原来的IplImage类型共享图像数据,转换只是创建一个Mat矩阵头// or : Mat mtx = img1;
CvMat oldmat = mtx;//Mat-> CvMat //只是创建矩阵头,而没有复制数据,oldmat不用手动释放
CV_Assert((oldmat.cols == img1->width) && (oldmat.rows == img1->height) && (oldmat.data.ptr == (uchar *)img1->imageData) && (oldmat.step == img1->widthStep));
imshow(strWindowName, mtx);
waitKey(0);
cvNamedWindow(strWindowName.c_str(), 0);
cvShowImage(strWindowName.c_str(), &oldmat);
cvWaitKey(0);
IplImage img2 = mtx;//Mat->IplImage //只是创建图像头,而没有复制数据,img2不用手动释放
cvShowImage(strWindowName.c_str(), &img2);
cvWaitKey(0);
Mat mat3(&oldmat);//CvMat->Mat
imshow(strWindowName, mat3);
waitKey(0);
cvDestroyWindow(strWindowName.c_str());
cvReleaseImage(&img1);
//创建 3 x 3 双精度恒等矩阵-----6
Mat M3 = (Mat_ <double>(3,3) <<1,0,0, 0,1,0, 0,0,1);
//访问数组元素-----7
M2.at<double>(0, 0) += 10.f;
double sum = 0;//计算元素和,方法一
for (int i=0; i<M2.rows; i++)
{
const double *Mi = M2.ptr<double>(i) ;
for (int j=0; j<M2.cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//计算元素和,方法二
int cols =M2.cols, rows = M2.rows ;
if (M2.isContinuous())
{
cols *= rows;
rows = 1 ;
}
for (int i=0; i<rows; i++)
{
const double *Mi = M2.ptr <double>(i);
for (int j=0; j<cols; j++)
{
sum += std::max(Mi[j], 0.);
}
}
cout<<sum<<endl;
sum = 0;//计算元素和,方法三
MatConstIterator_<double> it = M2.begin<double>(), it_end = M2.end<double>();
for(; it != it_end; ++it)
{
sum += std::max(*it, 0.);
}
cout<<sum<<endl;
return 0;
}
参考文献:
1、 http://blog.csdn.net/giantchen547792075/article/details/7061391
2、 http://blog.csdn.net/giantchen547792075/article/details/7169255
3、 http://blog.csdn.net/giantchen547792075/article/details/7318415
4、 http://blog.sina.com.cn/s/blog_534497fd01015k7z.html
5、 http://blog.csdn.net/wobuaishangdiao/article/details/7754580
6、 http://blog.csdn.net/yang_xian521/article/details/7107786
7、 http://blog.csdn.net/yang_xian521/article/details/7161335
8、http://blog.csdn.net/caiye917015406/article/details/7791815