public static PointD GetCenter(this AffinePos pos) { var xCenter = Abs(pos.LT.X - pos.RT.X) / 2; var yCenter = Abs(pos.LT.Y - pos.RT.Y) / 2; return(new PointD(xCenter, yCenter)); }
public static TrnsData ToTrnsData (this AffinePos srcPos, int w, int h) { //inner w h var innerh = PosL2(srcPos.LB, srcPos.LT); var innerw = PosL2(srcPos.RT, srcPos.LT); var trgPos = srcPos.MoveToCenter(new PointD(w / 2.0, h / 2.0)); // ok var fcenterX = srcPos.GetCenter().X; var fcenterY = srcPos.GetCenter().Y; var lcenterX = trgPos.GetCenter().X; var lcenterY = trgPos.GetCenter().Y; var xshift = lcenterX - fcenterX; var yshift = lcenterY - fcenterY; var dx = srcPos.LT.X - srcPos.RT.X; var dy = srcPos.LT.Y - srcPos.RT.Y; if (dx == 0) { return(default(TrnsData)); } //var angle_degree = 360 - Math.Atan2(dy, dx) * 180 / Math.PI; //double radian; //angle_degree %= 360; //radian = PI / 180.0 * angle_degree; var radian = Math.Atan(dy / dx); var radian2 = Math.Atan2(dy, dx); return(new TrnsData() { H = h, W = w, XShift = (int)xshift, YShift = (int)yshift, Innerw = innerw, Innterh = innerh, XSrcCnter = (int)fcenterX, YSrcCnter = (int)fcenterY, dX = (int)dx, dY = (int)dy, Angle = radian, }); }
public static AffinePos MoveToCenter(this AffinePos pos, PointD center) { var wHalf = Math.Sqrt(Math.Pow((pos.LT.X - pos.RT.X), 2) + Math.Pow((pos.LT.Y - pos.RT.Y), 2)); var hHalf = Math.Sqrt(Math.Pow((pos.LT.X - pos.LB.X), 2) + Math.Pow((pos.LT.Y - pos.LB.Y), 2)); var x1 = center.X - wHalf; var x2 = center.X - wHalf; var x3 = center.X + wHalf; var x4 = center.X + wHalf; var y1 = center.Y - hHalf; var y3 = center.Y + hHalf; var y2 = center.Y + hHalf; var y4 = center.Y - hHalf; return(new AffinePos(x1, y1, x2, y2, x3, y3, x4, y4)); }
public static AffinePos GetCentered(this AffinePos pos, Point center) { var wHalf = Abs(pos.LT.X - pos.RT.X) / 2; var hHalf = Abs(pos.LT.Y - pos.RT.Y) / 2; var x1 = center.X - wHalf; var x2 = center.X - wHalf; var x3 = center.X + wHalf; var x4 = center.X + wHalf; var y1 = center.Y - hHalf; var y3 = center.Y + hHalf; var y2 = center.Y + hHalf; var y4 = center.Y - hHalf; return(new AffinePos(x1, y1, x2, y2, x3, y3, x4, y4)); }
public static int[,] xytransform(int[,] src, AffinePos srcPos, AffinePos trgPos) { var h = src.GetLength(0); var w = src.GetLength(1); var fcenterX = srcPos.GetCenter().X; var fcenterY = srcPos.GetCenter().Y; var lcenterX = trgPos.GetCenter().X; var lcenterY = trgPos.GetCenter().Y; var xshift = (int)(lcenterX - fcenterX); var yshift = (int)(lcenterY - fcenterY); int[,] R = new int[h + Abs(yshift), w + Abs(xshift)]; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { if (i - xshift >= 0 && j - yshift >= 0 && i - xshift < R.GetLength(1) && j - yshift < R.GetLength(0)) { R[j - yshift, i - xshift] = src[j, i]; } } } // now R is shifted /* * var dx = Abs(srcPos.LT.X - srcPos.RT.X); * var dy = Abs(srcPos.LT.Y - srcPos.RT.Y); * if (dx == 0) return null; * var angle_degree = 360 - Math.Atan2(dy, dx)*180/Math.PI; * var rotationResult = rotateGeneral(R, R.GetLength(1), R.GetLength(0), angle_degree); * * return rotationResult; */ return(R); }
private void btnxyaffineStart_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var img = new Img(ofd.FileName); var w = img.Width; var h = img.Height; // -- Set Ratio RatioW = w / cvsOriginal.Width; RatioH = h / cvsOriginal.Height; // -- var data = img.Data.TointArray(); // ------------ imgOriginal.Source = new BitmapImage(new Uri(ofd.FileName)); // ------------ var p1 = new Point(132, 103); var p2 = new Point(100, 101); var srcPos = new AffinePos( 178, 1902, 132, 103, 933, 57, 0, 0 ); var trgPos = new AffinePos( 100, 1900, 100, 101, 1901, 101, 0, 0 ); int[,] ResultArray = xytransform(data, srcPos, trgPos); /// Start To Bitmap //var bmpsource = ResultArray.ToBitmap().ToImageSource(); var bmpsource = ResultArray.ToBitmap_SetPixel().ToImageSource(); imgRotated.Source = bmpsource; // SAve string path = @"C:\Data\Rotated.jpg"; ExtLib.SaveImg(path, bmpsource); } }
private void btnWithPointStart_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var img = new Img(ofd.FileName); var w = img.Width; var h = img.Height; // -- Set Ratio RatioW = w / cvsOriginal.ActualWidth; RatioH = h / cvsOriginal.ActualHeight; // -- var data = img.Data.TointArray(); // ------------ imgOriginal.Source = new BitmapImage(new Uri(ofd.FileName)); // ------------ if (LBPos == null || LTPos == null || RTPos == null) { return; } var srcPos = new AffinePos(LBPos, LTPos, RTPos, RBPos); var trgPos = srcPos.GetCentered(new Point(w / 2, h / 2)); var poslist = new PointD[4] { LBPos, LTPos, RTPos, RBPos }; var afindata = new AffinePos(LBPos, LTPos, RTPos, RBPos); var recipe = afindata.ToTrnsData(w, h); int[,] ResultArray = rotateGeneral(data, recipe.W, recipe.H, recipe.Angle, true); if (ResultArray == null) { return; } /// Start To Bitmap //var bmpsource = ResultArray.ToBitmap().ToImageSource(); var bmpsource = ResultArray.ToBitmap_SetPixel().ToImageSource(); imgRotated.Source = bmpsource; // SAve string path = @"C:\Data\Rotated.jpg"; ExtLib.SaveImg(path, bmpsource); } }