高斯判别算法GDA(吴恩达机器学习c#实践,高斯模板上同心椭圆马氏距离画法,三)

这段代码展示了如何使用C#进行椭圆的绘制和旋转操作。首先创建了一个11x11的灰度图像,然后通过ZoomNormal函数放大到44x44,并使用Bitmap进行24位颜色显示。接着,定义了一个旋转椭圆的函数,通过计算和矩阵变换实现了椭圆的旋转。该函数接受中心点坐标、椭圆半径和旋转角度作为参数,以红色显示旋转后的椭圆轨迹。
摘要由CSDN通过智能技术生成

先画一个水平椭圆吧!(参考计算机图形学基础)

以下是画旋转椭圆的C#代码:

  //因为是11*11,不妥,X12,所以改为44*44*3*3,最终放大了16倍
            byte[] gaos = new byte[11 * 11];

            for (int j = 0; j < 11; j++)
            {
                for (int i = 0; i < 11; i = i + 1)
                {
                    int nn = j * 11 + i;
                    int temp3 = pdKernal_2[nn]==4.0?255:(int)(pdKernal_2[nn] * 10);//白色主档边界
                   // int temp3 = (int)(pdKernal_2[nn] * 10);
                    gaos[nn] = temp3 > 255 ? (byte)255 : (byte)temp3;//此处是24位灰度图像(黑白)
                    //此处glob_rgbValues[nn * 3+0]=glob_rgbValues[nn * 3+1]=glob_rgbValues[nn * 3+2]

                }
            }
            int ww = 132 + 44;
            int hh = 132 + 44;
            //int ww = 132 ;
            //int hh = 132 ;
            byte[] glob_buffer8 = new byte[ww * hh];
            //ZoomNormal(gaos, ref glob_buffer8, 11, 11,
            //           ref  ww, ref hh, 12, 12);
            ZoomNormal(gaos, ref glob_buffer8, 11, 11,
                        ref  ww, ref hh, 16, 16);//这个函数前面已经出现

/

   ///测试24位显示
            Bitmap cutPic88 = new Bitmap(ww, hh, PixelFormat.Format24bppRgb);
           
            BitmapData _cutPic8 = cutPic88.LockBits(new Rectangle(0, 0, ww, hh), ImageLockMode.ReadWrite,
                                                 cutPic88.PixelFormat);
            IntPtr ptr00 = _cutPic8.Scan0;//得到首地址 
            byte[] buffer24=new byte[ww*hh*3];
            for (int j = 0; j < hh; j++)
            for (int i = 0; i < ww;i++ )              
                {
                    int tt = j * ww + i;
                buffer24[3*tt]=glob_buffer8[tt];
                buffer24[3 * tt+1] = glob_buffer8[tt];
                buffer24[3 * tt+2] = glob_buffer8[tt];
            }
                //把cutvalues数组给ptr
            System.Runtime.InteropServices.Marshal.Copy(buffer24, 0, ptr00, ww * hh*3);
            cutPic88.UnlockBits(_cutPic8);

            旋转椭圆(16 * 5, 16 * 5, 3.8 * 16 / 2, 2.8 * 16 / 2, 3.1416 / 4, ref  cutPic88);
            旋转椭圆(16 * 5, 16 * 5, 3.8 * 16, 2.8 * 16, 3.1416 / 4, ref  cutPic88);
            旋转椭圆(16 * 5, 16 * 5, 3.8 * 21, 2.8 * 21, 3.1416 / 4, ref  cutPic88);

///

   void 旋转椭圆(int 中心x, int 中心y, double la1, double la2, double 弧度, ref Bitmap curbmp)
        {
            //int x0 = 200;
            //int y0 = 100;
            //int iamuda1 = 120;//a
            //int iamuda2 = 60;//b
            //int x0 = 16 * 5;
            //int y0 = 16 * 5;
            int x0 = 中心x;
            int y0 = 中心y;
            //double iamuda1 = 3.8 * 16 / 2;//a
            //double iamuda2 = 2.8 * 16 / 2;//b
            double iamuda1 = la1;//a
            double iamuda2 = la2;//b
            int x = 0;
            int y = (int)(iamuda2 + 0.5);
            double d1 = iamuda2 * iamuda2 + iamuda1 * iamuda1 * (-iamuda2 + 0.25);
            double d2 = 0;
            //cutPic88.SetPixel(x+x0, y+y0, Color.Red);
            //cutPic88.SetPixel(-x + x0, -y + y0, Color.Red);
            //cutPic88.SetPixel(-x + x0, y + y0, Color.Red);
            //cutPic88.SetPixel(x + x0, -y + y0, Color.Red);
            int xxx = x + x0; int yyy = y + y0;
            点旋转(ref xxx, ref yyy, 弧度, (float)x0, (float)y0);
            curbmp.SetPixel(xxx, yyy, Color.Red);
            xxx = -x + x0; yyy = -y + y0; 点旋转(ref xxx, ref yyy, 弧度, (float)x0, (float)y0);
            curbmp.SetPixel(xxx, yyy, Color.Red);
            xxx = -x + x0; yyy = y + y0; 点旋转(ref xxx, ref yyy, 弧度, (float)x0, (float)y0);
            curbmp.SetPixel(xxx, yyy, Color.Red);
            xxx = x + x0; yyy = -y + y0; 点旋转(ref xxx, ref yyy, 弧度, (float)x0, (float)y0);
            curbmp.SetPixel(xxx, yyy, Color.Red);

            while (iamuda2 * iamuda2 * (x + 1) < iamuda1 * iamuda1 * (y - 0.5))
            {
                if (d1 <= 0)
                { d1 += iamuda2 * iamuda2 * (2 * x + 1 + 2); x++; }
                else { d1 += iamuda2 * iamuda2 * (2 * x + 1 + 2) + iamuda1 * iamuda1 * (-2 * y + 2); x++; y--; }
                int xx = x + x0; int yy = y + y0;
                点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = -x + x0; yy = -y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = -x + x0; yy = y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = x + x0; yy = -y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
            }
            d2 = iamuda2 * iamuda2 * (x + 0.5) * (x + 0.5) + iamuda1 * iamuda1 * (y - 1) * (y - 1) - iamuda2 * iamuda2 * iamuda1 * iamuda1;
            while (y > 0)
            {
                if (d2 <= 0)
                { d2 += iamuda2 * iamuda2 * (2 * x + 2) + iamuda1 * iamuda1 * (-2 * y + 3); x++; y--; }
                else { d2 += iamuda1 * iamuda1 * (-2 * y + 3); y--; }

                //cutPic88.SetPixel(x + x0, y + y0, Color.Red);
                //cutPic88.SetPixel(-x + x0, -y + y0, Color.Red);
                //cutPic88.SetPixel(-x + x0, y + y0, Color.Red);
                //cutPic88.SetPixel(x + x0, -y + y0, Color.Red);
                //3.1416 / 4
                int xx = x + x0; int yy = y + y0;
                点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = -x + x0; yy = -y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = -x + x0; yy = y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
                xx = x + x0; yy = -y + y0; 点旋转(ref xx, ref yy, 弧度, (float)x0, (float)y0);
                curbmp.SetPixel(xx, yy, Color.Red);
            }
        }
        void 点旋转(ref int xx, ref int yy, double 弧度, float 中心x, float 中心y)
        {
            矩阵变换 temp = new 矩阵变换();
            PointF tempPt = new PointF(-中心x, -中心y);
            PointF 指定点 = new PointF(中心x, 中心y);
            PointF 旋转点 = new PointF(xx, yy);
            temp.点平移(指定点, ref 旋转点);
            temp.基于原点旋转(弧度, ref 旋转点);
            temp.点平移(tempPt, ref 旋转点);
            xx = (int)旋转点.X;
            yy = (int)旋转点.Y;
        }

//以下是矩阵变换类

  public class 矩阵变换//这个类我们前面直线旋转也是用它
    {
        public double[,] 单位矩阵 = new double[3, 3];//单位矩阵
        public 矩阵变换()
        {
            int i, j;
            for (i = 0; i < 3; i++)
                for (j = 0; j < 3; j++)
                {
                    if (i == j) 单位矩阵[i, j] = 1;
                    else 单位矩阵[i, j] = 0;
                }
        }

public void 点平移(PointF 指定点 ,ref PointF sp)

{

矩阵变换 M = new 矩阵变换();

 M. 单位矩阵[2, 0] = -指定点.X;
 M. 单位矩阵[2, 1] = -指定点.Y;

  double x, y;
            x = M.单位矩阵[0, 0] * sp.X + M.单位矩阵[1, 0] * sp.Y + M.单位矩阵[2, 0];
            y = M.单位矩阵[0, 1] * sp.X + M.单位矩阵[1, 1] * sp.Y + M.单位矩阵[2, 1];
            sp.X = (float)x;
            sp.Y = (float)y;

}

public void 基于原点旋转(double 弧度 ,ref PointF sp)

{          矩阵变换 M = new 矩阵变换();
            M.单位矩阵[0, 0] = M.单位矩阵[1, 1] = Math.Cos(弧度);//Math.Cos
            M.单位矩阵[0, 1] = Math.Sin(弧度);//Math.Sin
            M.单位矩阵[1, 0] = -M.单位矩阵[0, 1];

double x, y;
            x = M.单位矩阵[0, 0] * sp.X + M.单位矩阵[1, 0] * sp.Y + M.单位矩阵[2, 0];
            y = M.单位矩阵[0, 1] * sp.X + M.单位矩阵[1, 1] * sp.Y + M.单位矩阵[2, 1];
            sp.X = (float)x;
           sp.Y = (float)y;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值