Hello Mat

 找回密码
 立即注册
查看: 2077|回复: 0

MathNet进行多元线性拟合

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2022-4-14 13:09:15 | 显示全部楼层 |阅读模式
MathNet进行多元线性拟合
  1.             string path = System.Windows.Forms.Application.StartupPath + @"/" + "F18.JPG";
  2.             Mat Image = new Mat(path, ImreadModes.Grayscale);
  3.             int image_Width = Image.Width;
  4.             int image_Height = Image.Height;
  5.             // 二元二次拟合
  6.             //Linear model Poly22: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
  7.             double[,] XAll = new double[image_Width * image_Height, 6];
  8.             double[] YAll = new double[image_Width * image_Height];
  9.             int Count = 0;
  10.             for (int i = 0; i < Image.Height; i++)
  11.             {
  12.                 for (int j = 0; j < Image.Width; j++)
  13.                 {
  14.                     //Ver3b RGB = Image.At<Ver3b>(i, j);
  15.                     //int grayvalue = Convert.ToInt32((RGB[0] + RGB[1] + RGB[2]) / 3.0);
  16.                     int grayvalue = 128;
  17.                     double X = Convert.ToDouble(i);
  18.                     double Y = Convert.ToDouble(j);
  19.                     XAll[Count, 1] = 1;
  20.                     XAll[Count, 2] = X;
  21.                     XAll[Count, 3] = Y;
  22.                     XAll[Count, 4] = X * X;
  23.                     XAll[Count, 5] = X * Y;
  24.                     XAll[Count, 6] = Y * Y;
  25.                     YAll[Count] = grayvalue;
  26.                     Count = Count + 1;
  27.                 }
  28.             }
  29.             ///////////////////////////MathNet///////////////////////////////
  30.             var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
  31.             formatProvider.TextInfo.ListSeparator = " ";
  32.             //// Create matrix "A" with coefficients
  33.             //var matrixA = DenseMatrix.OfArray(XAll);
  34.             //// Create vector "b" with the constant terms.
  35.             //var vectorB = new DenseVector(YAll);
  36.             //// 2. Solve linear equations using QR decomposition
  37.             //var resultX = matrixA.QR().Solve(vectorB);
  38.             //// 5. Verify result. Multiply coefficient matrix "A" by result vector "x"
  39.             //var reconstructVecorB = matrixA * resultX;
  40.             var reconstructVecorB = new double[image_Width * image_Height];
  41.             //////////////////////////////////////////////////////////

  42.             Mat src = new Mat(Image.Height, Image.Width, MatType.CV_32FC1, Scalar.Black);
  43.             Count = 0;
  44.             for (int i = 0; i < Image.Height; i++)
  45.             {
  46.                 for (int j = 0; j < Image.Width; j++)
  47.                 {
  48.                     src.SetArray(i, j, (float)reconstructVecorB[Count]);
  49.                     Count = Count + 1;
  50.                 }
  51.             }
  52.             double minV = 0.0, maxV = 0.0;
  53.             src.MinMaxLoc(out minV, out maxV);
  54.             Mat srcU8 = new Mat(Image.Height, Image.Width, MatType.CV_8UC1, Scalar.Black);
  55.             src.ConvertTo(srcU8, MatType.CV_8UC1);
  56.             //src.ConvertTo(srcU8, MatType.CV_8UC1, 255.0 / (maxV - minV + 0.00001), -255.0 * minV / (maxV - minV + 0.00001));
  57.             Cv2.ImWrite("1.jpg", srcU8);

  58.             this.pictureBox1.Image = C_lmage.MatToBitmap(srcU8);
复制代码

参考:
【1】https://blog.csdn.net/weixin_30687587/article/details/96729032
【2】https://blog.csdn.net/c914620529/article/details/50393223/
【3】https://vimsky.com/examples/deta ... ultiply-method.html
【4】https://stackoverflow.com/questi ... gression-in-c-sharp
【5】【目录】开源Math.NET基础数学类库使用总目录 https://www.cnblogs.com/asxinyu/p/Bolg_Category_For_MathNet.html
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 15:34 , Processed in 0.212139 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表