|
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[image_Width * image_Height, 6];
- double[] YAll = new double[image_Width * image_Height];
- 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[0] + RGB[1] + RGB[2]) / 3.0);
- int grayvalue = 128;
- double X = Convert.ToDouble(i);
- double Y = Convert.ToDouble(j);
- XAll[Count, 1] = 1;
- XAll[Count, 2] = X;
- XAll[Count, 3] = Y;
- XAll[Count, 4] = X * X;
- XAll[Count, 5] = X * Y;
- XAll[Count, 6] = Y * Y;
- YAll[Count] = 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[image_Width * image_Height];
- //////////////////////////////////////////////////////////
- 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 = 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
|
|