public unsafe ImageU8 ToImageU8() { ImageU8 img = new ImageU8(this.Width, this.Height); Grad * start = this.Start; Grad * end = start + this.Length; float max = 0; // 找最大值 Grad *src = start; while (src != end) { max = Math.Max(max, src->Value); src++; } float coeff = max > 255 ? 255f / max : 0; src = start; Byte *dst = img.Start; while (src != end) { *dst = (Byte)(coeff * src->Value); dst++; src++; } return(img); }
public unsafe ImageGrad CreateGradImage(Boolean computeDirection = true) { int step = this.Step; using (ImageChannel32 gradX = new ImageChannel32(this.Width, this.Height)) using (ImageChannel32 gradY = new ImageChannel32(this.Width, this.Height)) { CopyToIntChannel(this, gradX); CopyToIntChannel(this, gradY); gradX.ApplyConvolution(ConvolutionKernel.SobelX); gradY.ApplyConvolution(ConvolutionKernel.SobelY); ImageGrad grad = new ImageGrad(this.Width, this.Height); Int32 * xStart = (Int32 *)gradX.StartIntPtr; Int32 * yStart = (Int32 *)gradY.StartIntPtr; Grad * g = grad.Start; Grad * gEnd = g + grad.Length; if (computeDirection == true) { while (g != gEnd) { Int32 x = *xStart; Int32 y = *yStart; float f = (float)Math.Sqrt(x * x + y * y); float theta = (float)Math.Atan2(x, y); g->Value = f; g->Theta = theta; xStart++; yStart++; g++; } } else { while (g != gEnd) { Int32 x = *xStart; Int32 y = *yStart; float f = (float)Math.Sqrt(x * x + y * y); g->Value = f; g->Theta = 0; xStart++; yStart++; g++; } } return(grad); } }