c# Emgucv 图像旋转

 private Bitmap rotate(Bitmap bmp, double alpha, int Dest_w, int Dest_h)
        {
            Image after_rotate = new Image(Dest_w, Dest_h, new Gray(255));///创建一指定大小的的灰度图像 
                   BitmapData Src_Data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                int Src_h = bmp.Height;
              int Src_w = bmp.Width;

            unsafe
            {

                int Src_tride = Src_Data.Stride;
                System.IntPtr Scan0 = Src_Data.Scan0;
                byte* p_Src = (byte*)(void*)Scan0;

              //  int Des_tride = Des_Data.Stride;
            //    System.IntPtr Des = Des_Data.Scan0;
             //   byte* p_Des = (byte*)(void*)Des;
                // byte* p_Des = (byte*)Des_Data.Scan0;

                for (int y = 0; y < Dest_h; y++)
                {
                    for (int x = 0; x < Dest_w; x++)
                    {

                        //       byte Src_pix =p_Src[y * Src_tride + x * 3 + 0]; ///源图像当前像素点
                        //将行列数转换到坐标系下
                        double dest_x = x - 0.5 * Dest_w;
                        double dest_y = 0.5 * Dest_h - y;
                        //获得对应的旋转前的坐标
                        double src_x = dest_x * Math.Cos(alpha) - dest_y * Math.Sin(alpha);
                        double src_y = dest_x * Math.Sin(alpha) + dest_y * Math.Cos(alpha);
                        if ((src_x > -0.5 * bmp.Width + 1) && (src_x < 0.5 * bmp.Width - 1) && (src_y > -0.5 * bmp.Height + 1) && (src_y < 0.5 * Src_h - 1))
                        //如果在原来图像的范围内的话,将旋转前的坐标对应到图像的行和列 ,四舍五入,相当于最邻近插值
                        {

                            // double row= cvRound(0.5*Src_h-src_y);
                            //  double col= cvRound(0.5*Src_w+src_x);

                            double Y = Math.Round(0.5 * Src_h - src_y);
                            double X = Math.Round(0.5 * Src_w + src_x);
                            int q11x = (int)Math.Floor(X);//%返回小于或者等于它的最大整数 3.59 变为3
                            int q11y = (int)Math.Ceiling(Y); //%返回大于等于它的最小整数 3.59 变为4

                            int q21x = (int)Math.Ceiling(X);
                            int q21y = (int)Math.Ceiling(Y);

                            int q22x = (int)Math.Ceiling(X);
                            int q22y = (int)Math.Floor(Y);

                            int q12x = (int)Math.Floor(X);
                            int q12y = (int)Math.Floor(Y);

                            int X2 = (int)Math.Ceiling(X);//% important
                            int X1 = (int)Math.Ceiling(X - 1);
                            int Y2 = (int)Math.Ceiling(Y);
                            int Y1 = (int)Math.Ceiling(Y - 1);//%important
                            double piex12 = p_Src[q12y * Src_tride + q12x * 3 + 0];//((uchar*)source_img->imageData)[q12y * source_img->widthStep + q12x];
                            double piex22 = p_Src[q22y * Src_tride + q22x * 3 + 0];//((uchar*)source_img->imageData)[q22y * source_img->widthStep + q22x];
                            double piex21 = p_Src[q21y * Src_tride + q21x * 3 + 0];//((uchar*)source_img->imageData)[q21y * source_img->widthStep + q21x];
                            double piex11 = p_Src[q11y * Src_tride + q11x * 3 + 0];//((uchar*)source_img->imageData)[q11y * source_img->widthStep + q11x];

                            double R1 = (X2 - x) * piex12 + (x - X1) * piex22;
                            double R2 = (X2 - x) * piex11 + (x - X1) * piex21;
                            double fp = ((Y2 - y) * R1 + (y - Y1) * R2);
                            if (fp > 125)  //操作后有些值可能不是0 也不是255
                                fp = 255;
                            else
                                fp = 0;
                        
                                                 byte a = (byte)Math.Round(fp);

                            after_rotate[y, x] = new Gray(a); //给灰度图像赋值


                                                }
                                     }  //end for
                }  //end for
            } //end unsafe
            bmp.UnlockBits(Src_Data);
         //   after_rotate.UnlockBits(Des_Data);
            return after_rotate.ToBitmap();
        }

你可能感兴趣的:(学习的)