#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <string.h>
#include <opencv/cv.h>
#include <stdio.h>
#include "opencv2/photo/photo.hpp"
#include <unistd.h>
using namespace cv;
#define specimenNum 7
#define cir 5
char inputWindow[20] = "input";
int normalWidth=400, normalHeight=400;
int specimenAddr[specimenNum][2] = {{50, 150}, {150, 50}, {120, 320},{150, 150}, {300,10}, {70, 320}, {120, 370}};
double mat_Num[2] = {0, 0};
double mat_X[specimenNum][2] = {{1, 5}, {1, 15}, {1, 12},{1, 15}, {1,30}, {1, 7}, {1, 12}};
double mat_Y[specimenNum] = {15, 5, 32, 15, 1, 32, 37};
Mat mat0, mat1, mat2, mat3, mat4, mat5, mat6, mat7, mat8;
IplImage src1, src2, src3, src4, src5, src6, src7, src8;
double k0, k1;
/*******************************************
*********样本点初始化显示*******************
*******************************************/
void specimenInit(void){
int i;
mat0 = Mat(normalWidth, normalHeight, CV_8UC3, cv::Scalar(0, 0, 0));
for(i=0; i<specimenNum; i++){
Point center = Point(specimenAddr[i][0], specimenAddr[i][1]);
circle(mat0, center, cir, Scalar(128,128,255), -1);
}
imshow(inputWindow, mat0);
}
/*******************************************
*********键值处理,按下q则退出**************
*******************************************/
void mykey(void){
while(1){
char c;
c = waitKey(0);
if(c == 'q'){
break;
}
}
}
/******************************************
*********画出当前学习曲线******************
*******************************************/
void drawmachinaline(void){
double i, j, m, n;
Mat matTmp;
mat1.copyTo(matTmp);
for(i=0; i< normalWidth; i++){
n = (double)i;
m = k0 + k1 * n;
if(m <= normalHeight){
Point center = Point((int)n, (int)m);
circle(mat0, center, 2, Scalar(255, 255, 255), -1);
}
}
imshow(inputWindow, mat0);
waitKey(10);
}
/*******************************************
***********显示矩阵数据*********************
*******************************************/
void showMatdate(Mat tmpMat){
int i, j;
CvScalar s1;
int Width = tmpMat.rows;
int Height = tmpMat.cols;
IplImage tmp;
tmp = tmpMat;
for(i=0; i< Width; i++){
for(j=0; j<Height; j++){
s1 = cvGet2D(&tmp, i, j);
printf("%lf ", s1.val[0]);
}
printf("\n");
}
printf("\n");
}
void getMachineData(void){
CvScalar s1;
int Width = mat8.rows;
int Height = mat8.cols;
IplImage tmp;
tmp = mat8;
s1 = cvGet2D(&tmp, 0, 0);
k0 = s1.val[0];
s1 = cvGet2D(&tmp, 1, 0);
k1 = s1.val[0];
printf("k0:%lf, k1:%lf\n", k0, k1);
}
/*******************************************
*********计算最佳函数参数*******************
*******************************************/
void getfuncNum(void){
int i;
mat1 = Mat(2, specimenNum, CV_64FC1, mat_X);
mat2 = Mat(specimenNum, 1, CV_64FC1, mat_Y);
mat3 = Mat(specimenNum, 2, CV_64FC1);
mat4 = Mat(specimenNum, specimenNum, CV_64FC1);
mat5 = Mat(specimenNum, specimenNum, CV_64FC1);
mat6 = Mat(specimenNum, 2, CV_64FC1);
mat7 = Mat(2, specimenNum, CV_64FC1);
mat8 = Mat(2, 1, CV_64FC1);
src1 = mat1;
src2 = mat2;
src3 = mat3;
src4 = mat4;
src5 = mat5;
src6 = mat6;
src7 = mat7;
src8 = mat8;
printf("mat1:\n");
showMatdate(mat1);
printf("mat2:\n");
showMatdate(mat2);
/***********mat1的转置矩阵**************************/
cvTranspose(&src1, &src3);
printf("mat3:\n");
showMatdate(mat3);
/**************矩阵乘法*****************************/
cvMatMulAdd(&src3, &src1, 0, &src4);
printf("mat4:\n");
showMatdate(mat4);
/**************矩阵的逆*****************************/
cvInvert(&src4, &src5, CV_SVD_SYM);
printf("mat5:\n");
showMatdate(mat5);
cvMatMulAdd(&src5, &src3, 0, &src6);
printf("mat6:\n");
showMatdate(mat6);
cvTranspose(&src6, &src7);
printf("mat7:\n");
showMatdate(mat7);
cvMatMulAdd(&src7, &src2, 0, &src8);
printf("mat8:\n");
showMatdate(mat8);
getMachineData();
drawmachinaline();
}
int main(int argc, char *argv[]){
specimenInit();
getfuncNum();
mykey();
return 0;
}