public static __ftype__ Distance(this __type__ r1, __type__ r2) { //# var cast = isDouble ? "" : "(float)"; var phi = Fun.Abs(r2.Angle - r1.Angle) % __cast__Constant.PiTimesTwo; return((phi > __cast__Constant.Pi) ? __cast__Constant.PiTimesTwo - phi : phi); }
public static void DistanceTest() { TrafoTesting.GenericTest(rnd => { var delta = (rnd.UniformDouble() - 0.5) * 2.0 * Constant.Pi; var r1 = TrafoTesting.GetRandomRot2(rnd); var r2 = new Rot2d(r1.Angle + (delta + Constant.PiTimesTwo * rnd.UniformInt(10))); var dist = r1.Distance(r2); TrafoTesting.AreEqual(dist, Fun.Abs(delta)); }); }
private double[] ComputeRelativeError(Func <__rot3t__, __rot3t__, __rtype__> f) { double[] error = new double[count]; for (int i = 0; i < count; i++) { var v = (double)angles[i]; var va = (double)f(A[i], B[i]); error[i] = Fun.Abs(v - va) / Fun.Abs(v); } return(error); }
public static void Difference(string dir) { var idir = Path.Combine(dir, "in"); var odir = Path.Combine(dir, "out"); var hiliteImg = new PixImage <float>(Path.Combine(idir, "ref_hilite.png")); var luxImg = new PixImage <float>(Path.Combine(idir, "ref_lux.png")); var diffImg = new PixImage <float>(Col.Format.RGB, hiliteImg.Size); diffImg.Volume.SetMap2(hiliteImg.Volume, luxImg.Volume, (a, b) => Fun.Abs(b - a)); var outImg = diffImg.ToPixImage <ushort>(); outImg.SaveAsImage(Path.Combine(odir, "difference.tiff")); }
/// <summary> /// Perform weighted least squares plane fitting /// </summary> /// <param name="points">Data points</param> /// <param name="maxIter">Maximum number of iterations</param> /// <param name="thres">The algorithm terminates if the change in mean squared error /// is below the given threshold</param> /// <param name="mse">Mean squared error</param> /// <returns>The found plane</returns> public static Plane3d FitPlane3dWeightedLeastSquares(this V3d[] points, int maxIter, double thres, out double mse) { if (points == null) { throw new ArgumentNullException(nameof(points)); } if (points.Length <= 0) { throw new InvalidOperationException(); } if (maxIter <= 0) { throw new InvalidOperationException(); } Plane3d wlsPlane = new Plane3d(); var weights = new double[points.Length].Set(1.0); var sqDists = new double[weights.Length]; double meanSquaredErrorNew = 0.0; double ch = double.MaxValue; for (int iter = 0; iter < maxIter && ch > thres; iter++) { double meanSquaredErrorCurrent = meanSquaredErrorNew; wlsPlane = PerformOneStep(points, weights); sqDists.SetByIndex(i => Fun.Square(wlsPlane.Height(points[i]))); meanSquaredErrorNew = sqDists.InnerProduct(weights, (s, w) => s * w, 0.0, (s, m) => s + m) / weights.Aggregate((sum, x) => sum += x); weights.SetByIndex(i => 1.0 / (sqDists[i] + 1.0)); ch = Fun.Abs(meanSquaredErrorNew - meanSquaredErrorCurrent); } ; mse = meanSquaredErrorNew; return(wlsPlane); }
internal void Init(TPoint p0, TPoint p1, double[] vec) { var dim = vec.Length; var scale = new double[dim].Set(0.0); var invDelta = new double[dim]; for (int d = 0; d < dim; d++) { invDelta[d] = Fun.IsTiny(vec[d]) ? 0.0 : 1.0 / vec[d]; } // Compute the scaling factors due to the projection of the // cylinder around the line onto the coorindate planes. for (int d0 = 0; d0 < dim - 1; d0++) { var dx0 = vec[d0]; for (int d1 = d0 + 1; d1 < dim; d1++) { var dx1 = vec[d1]; var s = Fun.Sqrt(dx0 * dx0 + dx1 * dx1); var s0 = Fun.IsTiny(dx1) ? double.MaxValue : s / Fun.Abs(dx1); if (s0 > scale[d0]) { scale[d0] = s0; } var s1 = Fun.IsTiny(dx0) ? double.MaxValue : s / Fun.Abs(dx0); if (s1 > scale[d1]) { scale[d1] = s1; } } } MinP = p0; MaxP = p1; P0 = p0; P1 = p1; InvDelta = invDelta; Scale = scale; }
public static void VariousDemos(string dir) { bool alternative = true; // WriteLinearRampImage(); // Interpolation(); Report.BeginTimed("various PixImage demos"); // NOTE: in the following comments a byte image is an image that uses a // byte for each channel of each pixel, a float image is an image that uses // a float for each channel of each pixel. var colorImage = CreateHowManyColorsIllusion(1024); // scaling an image var scaledColorImage = new PixImage <byte>(1280, 800, 3); scaledColorImage.GetMatrix <C3b>().SetScaledCubic(colorImage.GetMatrix <C3b>()); scaledColorImage.SaveAsImage(Path.Combine(dir, "v-scaled-image.png")); // For shrinking images, interpoation of image values is not the optimal // resampling filter. Here BSpline3 or BSpline5 approximation can be used // which are 3rd order or 5th order approximations of a Gauss filter. var shrunkColorImage = new PixImage <byte>(512, 512, 3); shrunkColorImage.GetMatrix <C3b>().SetScaledBSpline5(colorImage.GetMatrix <C3b>()); shrunkColorImage.SaveAsImage(Path.Combine(dir, "v-shrunk-image.png")); var largeColorImage = new PixImage <byte>(4096, 4096, 3); largeColorImage.GetMatrix <C3b>().SetScaledLanczos(colorImage.GetMatrix <C3b>()); largeColorImage.SaveAsImage(Path.Combine(dir, "v-large-lanczos-image.png")); var scaledColorImage2 = new PixImage <byte>(1280, 800, 3); scaledColorImage2.GetMatrix <C3b>().SetScaledLanczos(colorImage.GetMatrix <C3b>()); scaledColorImage.SaveAsImage(Path.Combine(dir, "v-scaled-lanczos-image.png")); var smallColorImage = CreateHowManyColorsIllusion(256); var nearestScaledImage = new PixImage <byte>(1024, 768, 3); nearestScaledImage.GetMatrix <C3b>().SetScaledNearest(smallColorImage.GetMatrix <C3b>()); nearestScaledImage.SaveAsImage(Path.Combine(dir, "v-scaled-nearest-image.png")); // writing a color png image colorImage.SaveAsImage(Path.Combine(dir, "v-color-image.png")); var grayImage = colorImage.ToGrayscalePixImage(); // scaling a grayscale image var scaledGrayImage = new PixImage <byte>(1280, 800, 1); scaledGrayImage.Matrix.SetScaledLanczos(grayImage.Matrix); scaledGrayImage.SaveAsImage(Path.Combine(dir, "v-scaled-gray-image.png")); // for grayscale and black/white images the Matrix property works grayImage.Matrix.SetLineY(16, 0, 100, 0); // writing a grayscale png image grayImage.SaveAsImage(Path.Combine(dir, "v-gray-image.png")); var gray2colorImage = grayImage.ToPixImage <byte>(Col.Format.BGR); // writing grayxcal image as a color image gray2colorImage.SaveAsImage(Path.Combine(dir, "v-gray2color-image.png")); // loading a 8-bit per channel color image var byteImg = new PixImage <byte>(Path.Combine(dir, "v-color-image.png")); //var byteImg2 = byteImg.Scaled(0.5); //byteImg2.SaveAsImage(Path.Combine(dir, "v-color-2.png")); //var byteImg4 = byteImg2.Scaled(0.5); //byteImg4.SaveAsImage(Path.Combine(dir, "v-color-4.png")); //var byteImg8 = byteImg4.Scaled(0.5); //byteImg8.SaveAsImage(Path.Combine(dir, "r-color-8.png")); // retrieving channel matrices from an rgb image var rc = byteImg.GetChannel(Col.Channel.Red); var gc = byteImg.GetChannel(Col.Channel.Green); var bc = byteImg.GetChannel(Col.Channel.Blue); // convert 8bit/channel rgb image to 16bit/channel image // var ushortImage = byteImg.ToPixImage<ushort>(); // ushortImage.Rotated(30 * Constant.RadiansPerDegree, false) // .SaveAsImage(Path.Combine(odir, "rotated_30_rgb16.png")); // save 16bit/channel rgb image. // ushortImage.SaveAsImage(Path.Combine(odir, "rgb8_to_rgb16.tif")); // load 16bit/channel rgb image // var ushortImage2 = new PixImage<ushort>(Path.Combine(odir, "rgb8_to_rgb16.tif")); // save again as 8bit/channel rgb image // ushortImage2.ToPixImage<byte>().SaveAsImage(Path.Combine(odir, "rgb8_to_rgb16_to_rgb8.tif")); // building a new rgb image from channel matrices var newImg = new PixImage <byte>(rc, gc, bc); // writing an 8-bit per channel png image newImg.SaveAsImage(Path.Combine(dir, "v-recombined-color.png"), PixFileFormat.Png, options: PixSaveOptions.Default | PixSaveOptions.UseStorageService | PixSaveOptions.UseChunkedStream); //byteImg.Rotated(60.0 * Constant.RadiansPerDegree) // .SaveAsImage(Path.Combine(dir, "v-rotated-60-resized.png")); //byteImg.Rotated(90.0 * Constant.RadiansPerDegree) // .SaveAsImage(Path.Combine(dir, "v-rotated-90-resized.png")); //byteImg.Volume.Transformed(ImageTrafo.Rot90).ToPixImage() // .SaveAsImage(Path.Combine(odir, "rotated_90_csharp_rgb8.png")); //byteImg.Rotated(180.0 * Constant.RadiansPerDegree) // .SaveAsImage(Path.Combine(dir, "v-rotated-180-resized.png")); //byteImg.Volume.Transformed(ImageTrafo.Rot180).ToPixImage() // .SaveAsImage(Path.Combine(odir, "rotated_180_csharp_rgb8.png")); //byteImg.Rotated(270.0 * Constant.RadiansPerDegree) // .SaveAsImage(Path.Combine(dir, "v-rotated-270-resized.png")); //byteImg.Volume.Transformed(ImageTrafo.Rot270).ToPixImage() // .SaveAsImage(Path.Combine(odir, "rotated_270_csharp_rgb8.png")); // loading an 8-bit per channel rgb image directly as a float image var floatImg = new PixImage <float>(Path.Combine(dir, "v-color-image.png")); // converting a float image to a byte image var floatToByteImg = floatImg.ToPixImage <byte>(); // saving the converted image in png format floatToByteImg.SaveAsImage(Path.Combine(dir, "v-byte2float2byte-color.png")); // color conversion to linear response var linearFloatImg = floatImg.Copy <C3f>(Col.LinearSRGBFromSRGB); // converting the linear float image to a byte image and saving it in png format linearFloatImg.ToPixImage <byte>().SaveAsImage(Path.Combine(dir, "v-linear-color.png")); // loading a byte image var bImg = new PixImage <byte>(Path.Combine(dir, "v-color-image.png")); byte threshold = 2; var isSame = byteImg.Volume.InnerProduct( bImg.Volume, (b1, b2) => Fun.Abs(b2 - b1) < threshold, true, (equal, pixEqual) => equal && pixEqual, equal => !equal); // replacing a border of 50 pixels by replicating the 1-pixel frame inside // the border outwards bImg.Volume.ReplicateBorder(new Border2l(50)); // acessing pixels of a byte image as C3b's var c3bmatrix = bImg.GetMatrix <C3b>(); // var copiedMatrix = c3bmatrix.Copy(); var newC3fImage = c3bmatrix.ToPixImage <float>(); // setting a region in the matrix c3bmatrix.SetRectangleFilled(48, 48, 52, 52, C3b.Black); // min x, min y, max x, max y if (alternative) { // this is equivalent to: c3bmatrix.SubMatrix(48, 48, 5, 5).Set(C3b.Black); // start x, start y, size x, size y } // accessing a single pixel of the matrix c3bmatrix[50, 50] = C3b.VRVisGreen; var size = c3bmatrix.Size; // draw a bresenham line c3bmatrix.SetLine(size.X - 50, 50, 50, size.Y - 50, C3b.Blue); // draw a bresenham circle c3bmatrix.SetCircle(size.X / 2, size.Y / 2, 50, C3b.Yellow); c3bmatrix.SetCircleFilled((size.X * 3) / 4, (size.Y * 3) / 4, 50, C3b.Yellow); c3bmatrix.SetCircleFilled(25, 25, 75, C3b.Yellow); var cx = size.X / 2; var cy = size.Y / 2; for (int i = 0; i < 36; i++) { var alpha = i * 2 * Constant.Pi / 36; var dx = Fun.Cos(alpha); var dy = Fun.Sin(alpha); c3bmatrix.SetLineAllTouchedRaw(cx + 64 * dx, cy + 64 * dy, cx + 128 * dx, cy + 128 * dy, C3b.Yellow); } // writing the image with the replicated border as a png bImg.SaveAsImage(Path.Combine(dir, "v-border-drawing.png")); Report.End(); }
public static float Distance(this Rot2f r1, Rot2f r2) { var phi = Fun.Abs(r2.Angle - r1.Angle) % (float)Constant.PiTimesTwo; return((phi > (float)Constant.Pi) ? (float)Constant.PiTimesTwo - phi : phi); }