static public BGR[,] Img2BGR(Bitmap Source) { int Width = Source.Width, Height = Source.Height; BitmapData SourceData = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BGR[,] Result = new BGR[Height, Width]; byte *SourcePointer = (byte *)SourceData.Scan0.ToPointer(); int X, Y, T; for (Y = 0; Y < Height; ++Y) { for (X = 0; X < Width; ++X) { Result[Y, X] = new BGR(); for (T = 0; T < 3; ++T) { Result[Y, X][T] = SourcePointer[0]; ++SourcePointer; } ++SourcePointer; } } Source.UnlockBits(SourceData); return(Result); }
static public HSIComplexImg Img2HSIComplexImg(Bitmap Source) { int Width = Source.Width, Height = Source.Height; BitmapData SourceData = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); HSIComplexImg Result = new HSIComplexImg(Width, Height); BGR BGRTmp = new BGR(); HSI HSITmp; byte * SourcePointer = (byte *)SourceData.Scan0.ToPointer(); int X, Y, T; for (Y = 0; Y < Height; ++Y) { for (X = 0; X < Width; ++X) { for (T = 0; T < 3; ++T) { BGRTmp[T] = SourcePointer[0]; ++SourcePointer; } ++SourcePointer; HSITmp = BGRTmp.ToHSI(); Result[0][Y, X] = new AMComplex(HSITmp.H, 0); Result[1][Y, X] = new AMComplex(HSITmp.S, 0); Result[2][Y, X] = new AMComplex(HSITmp.I, 0); } } Source.UnlockBits(SourceData); return(Result); }
static public Bitmap Spectrum2Img(BGRComplexImg Source, Spectrum Type) { BGR[,] Result = new BGR[Source.Height, Source.Width]; double Scale = Math.Sqrt(Source.Height * Source.Width); int X, Y, T; for (Y = 0; Y < Source.Height; ++Y) { for (X = 0; X < Source.Width; ++X) { Result[Y, X] = new BGR(); for (T = 0; T < 3; ++T) { if (Type == Spectrum.Magnitude) { Result[Y, X][T] = Source[T][Y, X].Magnitude; } else if (Type == Spectrum.Phase) { Result[Y, X][T] = Source[T][Y, X].Phase; } Result[Y, X][T] = DoubleToByte(Math.Log(Result[Y, X][T] + 1) * Scale); } } } return(BGR2Img(Result)); }
static public BGR[,] HSI2BGR(HSI[,] Source) { int Height = Source.GetLength(0), Width = Source.GetLength(1); BGR[,] Result = new BGR[Height, Width]; int X, Y; for (Y = 0; Y < Height; ++Y) { for (X = 0; X < Width; ++X) { Result[Y, X] = Source[Y, X].ToBGR(); } } return(Result); }
static private Bitmap SimpleSpatialFilter(Bitmap Source, int Range, Func <byte[], byte> Type) { if (Range % 2 == 0) { throw new Exception("範圍必須為奇數"); } int Height = Source.Height, Width = Source.Width; BGR[,] BGRSource = Img2BGR(Source); BGR[,] Result = new BGR[Height, Width]; int X, Y, T, A, B; List <byte> BGRList = new List <byte>(); for (Y = 0; Y < Height; ++Y) { for (X = 0; X < Width; ++X) { Result[Y, X] = new BGR(); for (T = 0; T < 3; ++T) { BGRList.Clear(); for (A = Y - Range / 2; A <= Y + Range / 2; ++A) { if (A >= 0 && A < Height) { for (B = X - Range / 2; B <= X + Range / 2; ++B) { if (B >= 0 && B < Width) { BGRList.Add(DoubleToByte(BGRSource[A, B][T])); } } } } Result[Y, X][T] = Type(BGRList.ToArray()); } } } return(BGR2Img(Result)); }
static public HSI BGR2HSI(BGR Source) { double R = (double)Source.R / 255; double G = (double)Source.G / 255; double B = (double)Source.B / 255; double Max = Math.Max(R, Math.Max(G, B)); double Min = Math.Min(R, Math.Min(G, B)); double I = (R + G + B) / 3f; double H, S; if (Max == Min) { H = 0f; S = 0f; } else { double C = Max - Min; if (Max == R) { H = (G - B) / C; } else if (Max == G) { H = (B - R) / C + 2f; } else { H = (R - G) / C + 4f; } H *= 60f; if (H < 0f) { H += 360f; } S = 1 - Min / I; } return(new HSI(H, S, I)); }
static public BGRComplexImg HSI2BGR(HSIComplexImg Source) { BGRComplexImg Result = new BGRComplexImg(Source.Width, Source.Height); HSI TmpHSI = new HSI(); BGR TmpBGR = null; int X, Y; for (Y = 0; Y < Source.Height; ++Y) { for (X = 0; X < Source.Width; ++X) { TmpHSI.H = Source.H[Y, X].Re; TmpHSI.S = Source.S[Y, X].Re; TmpHSI.I = Source.I[Y, X].Re; TmpBGR = TmpHSI.ToBGR(); Result.R[Y, X].Re = TmpBGR.R; Result.G[Y, X].Re = TmpBGR.G; Result.B[Y, X].Re = TmpBGR.B; } } return(Result); }
static public HSIComplexImg BGR2HSI(BGRComplexImg Source) { HSIComplexImg Result = new HSIComplexImg(Source.Width, Source.Height); BGR TmpBGR = new BGR(); HSI TmpHSI = null; int X, Y; for (Y = 0; Y < Source.Height; ++Y) { for (X = 0; X < Source.Width; ++X) { TmpBGR.R = DoubleToByte(Source.R[Y, X].Re); TmpBGR.B = DoubleToByte(Source.B[Y, X].Re); TmpBGR.G = DoubleToByte(Source.G[Y, X].Re); TmpHSI = TmpBGR.ToHSI(); Result.H[Y, X].Re = TmpHSI.H; Result.S[Y, X].Re = TmpHSI.S; Result.I[Y, X].Re = TmpHSI.I; } } return(Result); }
static public HSI FromBGR(BGR Source) { return(ImgF.BGR2HSI(Source)); }