opencv学习_矩阵运算和绘制图像
Posted Leslie X徐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv学习_矩阵运算和绘制图像相关的知识,希望对你有一定的参考价值。
opencv 矩阵运算和绘制图像
5.矩阵运算
矩阵的创建和加减
void MainWindow::test()
{
double Anum[3][3] = {{1,1,1},
{2,2,2},
{3,3,3}};
double Bnum[3][3] = {{1,1,1},
{2,2,2},
{3,3,3}};
double Cnum[3][3] = {{1,0,0},
{0,1,0},
{0,0,1}};
Mat Amat(3,3,CV_64F,Anum);
Mat Bmat(3,3,CV_64F,Bnum);
Mat Cmat(3,3,CV_64F,Cnum);
Mat Dmat(3,3,CV_64F);
Dmat = Amat + Bmat;
std::cout<<Dmat<<std::endl;
Dmat = Amat * Cmat;
std::cout<<Dmat<<std::endl;
double Enum[5]={1,2,3,4,5};
Mat Emat(1,5,CV_64F,Enum);
std::cout<<Emat.t()*Emat<<std::endl;
std::cout<<Emat*Emat.t()<<std::endl;
}
/*输出
[2, 2, 2;
4, 4, 4;
6, 6, 6]
[1, 1, 1;
2, 2, 2;
3, 3, 3]
[1, 2, 3, 4, 5;
2, 4, 6, 8, 10;
3, 6, 9, 12, 15;
4, 8, 12, 16, 20;
5, 10, 15, 20, 25]
[55]
*/
2维向量类型的加减
void MainWindow::test()
{
Point2i a(1,1),b(1,1);
Point2i pt;
pt = (a+b)*10;
std::cout<<pt<<std::endl;
int c = a.dot(b); //点乘
std::cout<<c<<std::endl;
}
/*
输出
[20, 20]
2
*/
特殊矩阵
Mat Amat;
Amat = Mat::eye(5,5,CV_64F)*3;
std::cout<<Amat<<std::endl;
/*
输出
[3, 0, 0, 0, 0;
0, 3, 0, 0, 0;
0, 0, 3, 0, 0;
0, 0, 0, 3, 0;
0, 0, 0, 0, 3]
*/
6.绘制图形
创建图形函数:
cv::ellipse(Mat,Point,Size,double angle,startAngle,endAngle,Scalar,thickness,lineType);
cv::circle(Mat,Point center,double radius,Scalar,thickness,lineType);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
w=200;
imgInit();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::imgInit()
{
Mat imgData = Mat::zeros(w,w,CV_8UC3);
cvtColor(imgData,imgData,COLOR_BGR2RGB);
myImg=imgData;
myQImg = QImage((const uchar*)(imgData.data),
imgData.cols,imgData.rows,
QImage::Format_RGB888);
imgShow();
}
void MainWindow::imgShow()
{
ui->label->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->label->size())));
}
void MainWindow::MyEllipse(Mat img, double angle)
{
int thickness = 1;
int lineType = 8;
//创建椭圆函数
cv::ellipse(img,Point(w/2,w/2),
Size(w/4,w/16),angle,0,360,
Scalar(0,0,255),thickness,lineType);
}
void MainWindow::MyFilledCircle(Mat img, Point center)
{
int thickness = -1; //填充
int lineType = 8;
//创建圆函数
cv::circle(img,center,w/32,
Scalar(255,0,0),thickness,lineType);
}
void MainWindow::on_btnDraw_clicked()
{
/// 1.a. 创建椭圆
MyEllipse( myImg, 90 );
MyEllipse( myImg, 0 );
MyEllipse( myImg, 45 );
MyEllipse( myImg, -45 );
/// 1.b. 创建圆
MyFilledCircle( myImg, Point( w/2.0, w/2.0) );
myQImg = QImage((const uchar*)(myImg.data),
myImg.cols,myImg.rows,
QImage::Format_RGB888);
imgShow();
}
以上是关于opencv学习_矩阵运算和绘制图像的主要内容,如果未能解决你的问题,请参考以下文章