Opencv Mat的操作

cout << mat 有错误的原因

You are using OpenCV built with VS10. The ostream operator << in the DLL is using the operator from VC 10 Runtime. While you are calling the ostream operator << from VC 11 Runtime. The DLLs are conflicting

template T& Mat::at(int i) const 必须在编译期确定参数T

#include <opencv2/opencv.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv;

void print(Mat mat, int prec)
{
	cout << "[" << endl;
	for (int i = 0; i < mat.size().height; i++)
	{
		for (int j = 0; j < mat.size().width; j++)
		{
			cout << setprecision(prec) << mat.at<float>(i, j);
			if (j != mat.size().width - 1)
				cout << ", ";
			else
				cout << endl;
				
		}
	}
	cout << "]" << endl;
}

int main()
{
	float array[] = { 1,2,3 };
	float array1[] = { 2,3,1 };
	//用数组初始化Mat
	
	Mat mat = Mat(1, 3, CV_32F, array);
	Mat mat1 = Mat(1, 3, CV_32F, array1);

	Mat tempmat;
	//对mat拷贝
	mat.copyTo(tempmat);
	print(tempmat,5);
	//选择roi,rect的四个参数分别是(x,y)坐标,第三个参数是宽度,第四个参数是高度
	Mat roi(mat, Rect(0, 0, 2, 1));
	print(roi,5);
	//mat-mat1的1范数
	cout << norm(mat, mat1, CV_L1) << endl;
	//mat-mat1的2范数
	cout << norm(mat, mat1, CV_L2) << endl;
	//打印mat的内容
	print(mat ,5);
	//创建对角为1的矩阵
	Mat eyemat = Mat::eye(4, 4, CV_32F);
	print(eyemat,5 );
	//提取eyemat的0-1行,2-3列
	Mat submat = eyemat(Range(0, 2), Range(2, 4));
	print(submat,5);
	//abs(),max(),min(),+,-,*,/等操作很简单,就不写了
	float a[2][2] = { 2,3,1,2 };
	float b[2][2] = { 2,1,0,-1 };
	Mat amat(2, 2, CV_32F, a);
	Mat bmat(2, 2, CV_32F, b);
	print( amat ,5);
	print( bmat,5);
	//求amat的逆
	print( amat.inv(),5);
	//两矩阵相乘
	print( amat.mul(bmat),5 );
	print(amat.mul(6), 5);
	//生成一个值为0的矩阵
	print( Mat::zeros(3, 3, CV_32F),5 );
	//生成一个值为1的矩阵
	print( Mat::ones(3, 3, CV_32F) ,5);
	system("pause");
}
posted @ 2015-10-21 17:23  逍遥客33  阅读(301)  评论(0编辑  收藏  举报