public static unsafe Mat ZValuesToMat(ushort[] zValues, GocatorContext mContext) { int width = mContext.Width; int height = mContext.Height; int[] size = new int[] { height, width }; fixed(ushort *p = zValues) { return(new Mat(size, DepthType.Cv16U, (IntPtr)p, null)); } }
public static unsafe HImage ZValueToDepthImage(ushort[] zValue, GocatorContext context) { HImage image = new HImage(); fixed(ushort *p = zValue) { try { image.GenImage1("uint2", context.Width, context.Height, (IntPtr)p); } catch (HalconException ee) { throw; } } return(image); }
/// <summary> /// Trans zvalues to colormap /// </summary> /// <returns></returns> public static unsafe Image <Rgb, Byte> ZValueToColorMap(ushort[] zValues, GocatorContext mContext) { Mat mat = ZValuesToMat(zValues, mContext); var maxValue = default(ushort); var minValue = ushort.MaxValue; var nullValue = default(ushort); Parallel.For(0, zValues.Length, (index) => { var tempZ = zValues[index]; if (tempZ != nullValue) { if (tempZ > maxValue) { maxValue = tempZ; } if (tempZ < minValue) { minValue = tempZ; } } }); Color[] colors = new Color[zValues.Length]; Image <Rgb, Byte> image = new Image <Rgb, byte>(mContext.Width, mContext.Height, new Rgb(255, 255, 255)); Parallel.For(0, zValues.Length, (index) => { colors[index] = ToColor(zValues[index], minValue, maxValue, nullValue); }); //Bitmap bitmap = new Bitmap(Width, Height); //BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //IntPtr intPtr = bitmapData.Scan0; //byte[] Pixlemaps = new byte[bitmapData.Stride * bitmapData.Height]; //int offset = bitmapData.Stride - bitmapData.Width * 3; //unsafe //{ // byte* pp = (byte*)(void*)bitmapData.Scan0; // for (int k = 0; k < bitmapData.Height; k++) // { // for (int m = 0; m < bitmapData.Width; m++) // { // pp[0] = (byte)(colors[k * bitmapData.Width + m].R); // pp[1] = (byte)(colors[k * bitmapData.Width + m].G); // pp[2] = (byte)(colors[k * bitmapData.Width + m].B); // pp += 3; // } // pp += bitmapData.Stride - bitmapData.Width * 3; // } //} //bitmap.UnlockBits(bitmapData); // Bitmap aBitmap = bitmap.Clone(new RectangleF(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb); Stopwatch st = Stopwatch.StartNew(); Parallel.For(0, image.Height, (i) => { Parallel.For(0, image.Width, (j) => { Rgb tempColor = new Rgb(colors[j + image.Width * i]); image[i, j] = tempColor; }); }); Debug.WriteLine($"{st.ElapsedMilliseconds}"); return(image); }
/// <summary> /// Trans zvalues to PNG 16bit /// </summary> /// <param name="zValues"></param> /// <param name="mContext"></param> /// <returns></returns> /// public static unsafe Image <Gray, ushort> ZValuesToDepthPng(ushort[] zValues, GocatorContext mContext) { int width = mContext.Width; int height = mContext.Height; int[] size = new int[] { height, width }; fixed(ushort *p = zValues) { Mat depthPng = new Mat(size, DepthType.Cv16U, (IntPtr)p, null); //depthPng.ToImage<Rgb, ushort>().Save(@"C:\colorpng.png"); //Image<Gray, ushort> trh = depthPng.ToImage<Gray, ushort>().ThresholdBinary(new Gray(10000), new Gray(32768)); //trh.Save(@"C:\ trs.png"); return(depthPng.ToImage <Gray, ushort>()); } }