Halcom 发表于 2022-4-14 13:09:15

MathNet进行多元线性拟合

MathNet进行多元线性拟合            string path = System.Windows.Forms.Application.StartupPath + @"/" + "F18.JPG";
            Mat Image = new Mat(path, ImreadModes.Grayscale);
            int image_Width = Image.Width;
            int image_Height = Image.Height;
            // 二元二次拟合
            //Linear model Poly22: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2
            double[,] XAll = new double;
            double[] YAll = new double;
            int Count = 0;
            for (int i = 0; i < Image.Height; i++)
            {
                for (int j = 0; j < Image.Width; j++)
                {
                  //Ver3b RGB = Image.At<Ver3b>(i, j);
                  //int grayvalue = Convert.ToInt32((RGB + RGB + RGB) / 3.0);
                  int grayvalue = 128;
                  double X = Convert.ToDouble(i);
                  double Y = Convert.ToDouble(j);
                  XAll = 1;
                  XAll = X;
                  XAll = Y;
                  XAll = X * X;
                  XAll = X * Y;
                  XAll = Y * Y;
                  YAll = grayvalue;
                  Count = Count + 1;
                }
            }
            ///////////////////////////MathNet///////////////////////////////
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";
            //// Create matrix "A" with coefficients
            //var matrixA = DenseMatrix.OfArray(XAll);
            //// Create vector "b" with the constant terms.
            //var vectorB = new DenseVector(YAll);
            //// 2. Solve linear equations using QR decomposition
            //var resultX = matrixA.QR().Solve(vectorB);
            //// 5. Verify result. Multiply coefficient matrix "A" by result vector "x"
            //var reconstructVecorB = matrixA * resultX;
            var reconstructVecorB = new double;
            //////////////////////////////////////////////////////////

            Mat src = new Mat(Image.Height, Image.Width, MatType.CV_32FC1, Scalar.Black);
            Count = 0;
            for (int i = 0; i < Image.Height; i++)
            {
                for (int j = 0; j < Image.Width; j++)
                {
                  src.SetArray(i, j, (float)reconstructVecorB);
                  Count = Count + 1;
                }
            }
            double minV = 0.0, maxV = 0.0;
            src.MinMaxLoc(out minV, out maxV);
            Mat srcU8 = new Mat(Image.Height, Image.Width, MatType.CV_8UC1, Scalar.Black);
            src.ConvertTo(srcU8, MatType.CV_8UC1);
            //src.ConvertTo(srcU8, MatType.CV_8UC1, 255.0 / (maxV - minV + 0.00001), -255.0 * minV / (maxV - minV + 0.00001));
            Cv2.ImWrite("1.jpg", srcU8);

            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
页: [1]
查看完整版本: MathNet进行多元线性拟合