OpenCV输出均值、标准差、协方差
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
1、概述
案例:使用OpenCV输出图片的均值和标准差以及协方差矩阵
相关定义:
1.均值:平均值,指讲一个数据集合中的值相加再除以数据集中数据的个数得出的值
2.方差:数据集中每个样本值与全体样本值的平均数之差的平方值的平均数
3.标准差:标准差是方差的算数平方根(ps:定义参考方差)
4.协方差:在概率论和统计学中用于衡量两个变量的总体误差
函数定义:
均值及标准差:
void meanStdDev(InputArray src, OutputArray mean, OutputArray stddev,
InputArray mask=noArray());
1.src:输入图像
2.mean:输出参数--->均值
3.stddev:输出参数--->标准差
协方差矩阵、均值矩阵<br>void calcCovarMatrix( InputArray samples, OutputArray covar,
InputOutputArray mean, int flags, int ctype = CV_64F);<br>samples:输入数据,一般channgle==1<br>covar:输出参数表示协方差矩阵<br>mean:输出参数表示,均值矩阵<br>flags:操作标志:CV_COVAR_SCRAMBLED,CV_COVAR_NORMAL,CV_COVAR_USE_AVG,CV_COVAR_SCALE,CV_COVAR_ROWS,CV_COVAR_COLS.
2、代码演示
Face_Means_Covar::Face_Means_Covar(QWidget *parent)
: MyGraphicsView{parent}
{
this->setWindowTitle("计算Mat均值、标准差、及协方差");
QLabel *labelTitle = new QLabel(this);
labelTitle->setText("输出结果如下:");
//均值
QLabel *labelMeansTitle = new QLabel(this);
labelMeansTitle->setText("均值:");
labelMeansTitle->move(0,labelTitle->y()+labelTitle->height()+10);
labelMeansValue = new QLabel(this);
labelMeansValue->setFixedWidth(150);
labelMeansValue->move(labelMeansTitle->x()+labelMeansTitle->width()+10,labelMeansTitle->y());
//标准差
QLabel *labelStddevTitle = new QLabel(this);
labelStddevTitle->setText("标准差:");
labelStddevTitle->move(0,labelMeansTitle->y()+labelMeansTitle->height()+10);
labelStddevValue = new QLabel(this);
labelStddevValue->setFixedWidth(150);
labelStddevValue->move(labelStddevTitle->x()+labelStddevTitle->width()+10,labelStddevTitle->y());
//协方差
QLabel *labelCovarTitle = new QLabel(this);
labelCovarTitle->setText("协方差:");
labelCovarTitle->move(0,labelStddevTitle->y()+labelStddevTitle->height()+10);
labelCovarValue = new QLabel(this);
labelCovarValue->setFixedWidth(150);
labelCovarValue->move(labelCovarTitle->x()+labelCovarTitle->width()+10,labelCovarTitle->y());
}
void Face_Means_Covar::dropEvent(QDropEvent *event){
path = event->mimeData()->urls().at(0).toLocalFile();
qDebug()<<"文件路径:"<<path;
showMeansAndCovar(path.toStdString().c_str());
}
void Face_Means_Covar::showMeansAndCovar(const char *filePath){
Mat src = imread(filePath);
if(src.empty()){
qDebug()<<"图片文件为空";
return;
}
imshow("src",src);
//计算图像均值以及标准差
Mat means,stddev;
meanStdDev(src,means,stddev,noArray());
//输出均值矩阵,图像有多少通道means就有多少个值。例如:三通道图像,则有第一通道的均值,第二通道的均值,第三通道的均值
cout << means<<endl;
//输出标准差矩阵,图像有多少个通道stddev就有多少个值。例如:单通道图像,则0的位置就是通道标准差
cout<<stddev<<endl;
//计算所有通道的平均值
double resultMeans = 0.0;
for(int i=0;i<means.rows;i++){
resultMeans+=means.at<double>(i);
qDebug()<<"均值:"<< means.at<double>(i);
}
QString rMeans=QString("%1").arg(resultMeans/3);
labelMeansValue->setText(rMeans);
//计算所有通道的标准差
double resultStddev=0.0;
for(int i=0;i<stddev.rows;i++){
resultStddev+=stddev.at<double>(i);
qDebug()<<"标准差:"<<stddev.at<double>(i);
}
QString rStddev=QString("%1").arg(resultStddev/3);
labelStddevValue->setText(rStddev);
//计算协方差
Mat covar,means2;
cvtColor(src,src,COLOR_BGR2GRAY);
//这里的输入需要是单通道的channgle==1
calcCovarMatrix(src,covar,means2,COVAR_NORMAL|cv::COVAR_ROWS);
qDebug()<<"以下是协方差矩阵";
//一定要搞一个小一点的图测试
cout << covar<<endl;
cout << means2<<endl;
}
3、示例图片
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓
GitHub 加速计划 / opencv31 / opencv
156
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
4d26e16a
Speed up and reduce memory consumption for findContours 2 天前
1db98278
Add jxl (JPEG XL) codec support #26379
### Pull Request Readiness Checklist
Related CI and Docker changes:
- https://github.com/opencv/ci-gha-workflow/pull/190
- https://github.com/opencv-infrastructure/opencv-gha-dockerfile/pull/44
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work https://github.com/opencv/opencv/issues/20178
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2 天前
更多推荐
已为社区贡献16条内容
所有评论(0)