GaborConversion(ColorFloatImage image, int x, int y, float[,] matr, float normValue) { ColorFloatPixel tempPixel = image[x, y]; tempPixel.r = 0; tempPixel.g = 0; tempPixel.b = 0; int n = matr.GetLength(0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { tempPixel.r += matr[i, j] * image[((x + i - (n - 1) / 2) < 0 || (x + i - (n - 1) / 2) >= image.Width ? x : (x + i - (n - 1) / 2)), ((y + j - (n - 1) / 2) < 0 || (y + j - (n - 1) / 2) >= image.Height ? y : (y + j - (n - 1) / 2))].r; tempPixel.g += matr[i, j] * image[((x + i - (n - 1) / 2) < 0 || (x + i - (n - 1) / 2) >= image.Width ? x : (x + i - (n - 1) / 2)), ((y + j - (n - 1) / 2) < 0 || (y + j - (n - 1) / 2) >= image.Height ? y : (y + j - (n - 1) / 2))].g; tempPixel.b += matr[i, j] * image[((x + i - (n - 1) / 2) < 0 || (x + i - (n - 1) / 2) >= image.Width ? x : (x + i - (n - 1) / 2)), ((y + j - (n - 1) / 2) < 0 || (y + j - (n - 1) / 2) >= image.Height ? y : (y + j - (n - 1) / 2))].b; } } tempPixel.r = tempPixel.r / normValue; tempPixel.g = tempPixel.g / normValue; tempPixel.b = tempPixel.b / normValue; return(tempPixel); }
public static Bitmap ImageToBitmap(ColorFloatImage image) { Bitmap B = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); LockBitmapInfo lbi = LockBitmap(B); try { for (int j = 0; j < image.Height; j++) { for (int i = 0; i < image.Width; i++) { ColorFloatPixel p = image[i, j]; lbi.data[lbi.linewidth * j + i * 4] = p.b <0.0f ? (byte)0 : p.b> 255.0f ? (byte)255 : (byte)p.b; lbi.data[lbi.linewidth * j + i * 4 + 1] = p.g <0.0f ? (byte)0 : p.g> 255.0f ? (byte)255 : (byte)p.g; lbi.data[lbi.linewidth * j + i * 4 + 2] = p.r <0.0f ? (byte)0 : p.r> 255.0f ? (byte)255 : (byte)p.r; } } } finally { UnlockBitmap(lbi); } return(B); }
public static void Median(ColorFloatImage image, int rad) { float[] med = new float[(rad * 2 + 1) * (rad * 2 + 1)]; ColorFloatImage tempImg = new ColorFloatImage(image.Width, image.Height); int index = 0; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempPixel = image[x, y]; index = 0; for (int i = -rad; i <= rad; i++) { for (int j = -rad; j <= rad; j++) { med[index] = image[((x + i) < 0 || (x + i) >= image.Width ? x : (x + i)), ((y + j) < 0 || (y + j) >= image.Height ? y : (y + j))].g; index++; } } Array.Sort(med); tempPixel.g = med[2 * rad * rad + 2 * rad + 1]; index = 0; for (int i = -rad; i <= rad; i++) { for (int j = -rad; j <= rad; j++) { med[index] = image[((x + i) < 0 || (x + i) >= image.Width ? x : (x + i)), ((y + j) < 0 || (y + j) >= image.Height ? y : (y + j))].r; index++; } } Array.Sort(med); tempPixel.r = med[2 * rad * rad + 2 * rad + 1]; index = 0; for (int i = -rad; i <= rad; i++) { for (int j = -rad; j <= rad; j++) { med[index] = image[((x + i) < 0 || (x + i) >= image.Width ? x : (x + i)), ((y + j) < 0 || (y + j) >= image.Height ? y : (y + j))].b; index++; } } Array.Sort(med); tempPixel.b = med[2 * rad * rad + 2 * rad + 1]; tempImg[x, y] = tempPixel; } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { image[x, y] = tempImg[x, y]; } } }
public static ColorFloatPixel operator +(ColorFloatPixel P1, ColorFloatPixel P2) { ColorFloatPixel tmp = P1; tmp.r += P2.r; tmp.g += P2.g; tmp.b += P2.b; return(tmp); }
public static ColorFloatPixel operator *(ColorFloatPixel P, float n) { ColorFloatPixel tmp = new ColorFloatPixel(); tmp.a = P.a; tmp.r = P.r * n; tmp.g = P.g * n; tmp.b = P.b * n; return(tmp); }
public static void Sobel(ColorFloatImage image, string type = "xy") { ColorFloatImage tempImg = new ColorFloatImage(image.Width + 2, image.Height + 2); ResizeImage(image, tempImg); if (type == "x") { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempP = SobelX(tempImg, x + 1, y + 1); tempP.a += 128; tempP.r += 128; tempP.g += 128; tempP.b += 128; image[x, y] = tempP; } } } else if (type == "y") { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempP = SobelY(tempImg, x + 1, y + 1); tempP.a += 128; tempP.r += 128; tempP.g += 128; tempP.b += 128; image[x, y] = tempP; } } } else { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempPX = SobelX(tempImg, x + 1, y + 1), tempPY = SobelY(tempImg, x + 1, y + 1), tempP; tempP.a = 128 + image[x, y].a; tempP.r = 128 + (float)Math.Sqrt(tempPX.r * tempPX.r + tempPY.r * tempPY.r); tempP.g = 128 + (float)Math.Sqrt(tempPX.g * tempPX.g + tempPY.g * tempPY.g); tempP.b = 128 + (float)Math.Sqrt(tempPX.b * tempPX.b + tempPY.b * tempPY.b); image[x, y] = tempP; } } } }
public static void FlipImageX(ColorFloatImage image) { for (int y = 0; y < image.Height / 2; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel p = image[x, y]; image[x, y] = image[x, image.Height - 1 - y]; image[x, image.Height - 1 - y] = p; } } }
public static void FlipImageY(ColorFloatImage image) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width / 2; x++) { ColorFloatPixel p = image[x, y]; image[x, y] = image[image.Width - 1 - x, y]; image[image.Width - 1 - x, y] = p; } } }
public static void Roberts(ColorFloatImage image, int type = 0) { ColorFloatImage tempImg = new ColorFloatImage(image.Width + 2, image.Height + 2); ResizeImage(image, tempImg); if (type == 1) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempP = Roberts1(tempImg, x + 1, y + 1); tempP.a += 128; tempP.r += 128; tempP.g += 128; tempP.b += 128; image[x, y] = tempP; } } } else if (type == 2) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempP = Roberts2(tempImg, x + 1, y + 1); tempP.a += 128; tempP.r += 128; tempP.g += 128; tempP.b += 128; image[x, y] = tempP; } } } else { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel tempPX = Roberts1(tempImg, x + 1, y + 1), tempPY = Roberts2(tempImg, x + 1, y + 1), tempP; tempP.a = 128 + image[x, y].a; tempP.r = 128 + (float)Math.Sqrt(tempPX.r * tempPX.r + tempPY.r * tempPY.r); tempP.g = 128 + (float)Math.Sqrt(tempPX.g * tempPX.g + tempPY.g * tempPY.g); tempP.b = 128 + (float)Math.Sqrt(tempPX.b * tempPX.b + tempPY.b * tempPY.b); image[x, y] = tempP; } } } }
public static void InvertImage(ColorFloatImage image) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { ColorFloatPixel p = image[x, y]; p.r = 255 - image[x, y].r; p.g = 255 - image[x, y].g; p.b = 255 - image[x, y].b; image[x, y] = p; } } }
public static void downsample(ref ColorFloatImage image, float s, string paramoff = "") { ColorFloatImage tempImg = new ColorFloatImage((int)(image.Width / s), image.Height); if (paramoff != "-off") { ImageProcessing.Gauss(ref image, (float)Math.Sqrt(Math.Abs(s * s - 1))); } float n = (image.Width * s - s) / ((float)image.Width - s); for (int y = 0; y < image.Height; ++y) { tempImg[0, y] = image[0, y]; tempImg[tempImg.Width - 1, y] = image[image.Width - 1, y]; for (int x = 1; x < tempImg.Width - 1; ++x) { ColorFloatPixel p = new ColorFloatPixel(); float x1 = (float)Math.Truncate(x * n), x2 = (int)(x * n) + 1; p.r = (x2 - (float)x * n) * image[(int)x1, y].r + ((float)x * n - x1) * image[(int)x2, y].r; p.g = (x2 - (float)x * n) * image[(int)x1, y].g + ((float)x * n - x1) * image[(int)x2, y].g; p.b = (x2 - (float)x * n) * image[(int)x1, y].b + ((float)x * n - x1) * image[(int)x2, y].b; tempImg[x, y] = p; } } image = tempImg; tempImg = new ColorFloatImage(image.Width, (int)(image.Height / s)); n = (image.Height * s - s) / ((float)image.Height - s); for (int x = 0; x < image.Width; ++x) { tempImg[x, 0] = image[x, 0]; tempImg[x, tempImg.Height - 1] = image[x, image.Height - 1]; for (int y = 1; y < tempImg.Height - 1; ++y) { ColorFloatPixel p = new ColorFloatPixel(); float y1 = (float)Math.Truncate(y * n) > image.Height - 1 ? image.Height - 1 : (float)Math.Truncate(y * n), y2 = (int)(y * n) + 1 > image.Height - 1 ? image.Height - 1 : (int)(y * n) + 1; p.r = (y2 - (float)y * n) * image[x, (int)y1].r + ((float)y * n - y1) * image[x, (int)y2].r; p.g = (y2 - (float)y * n) * image[x, (int)y1].g + ((float)y * n - y1) * image[x, (int)y2].g; p.b = (y2 - (float)y * n) * image[x, (int)y1].b + ((float)y * n - y1) * image[x, (int)y2].b; tempImg[x, y] = p; } } image = tempImg; }
public static void up_bilinear(ref ColorFloatImage image, float s) { ColorFloatImage tempImg = new ColorFloatImage((int)(image.Width * s), image.Height); float n = (image.Width * s - 1) / ((float)image.Width - 1); for (int y = 0; y < image.Height; ++y) { tempImg[0, y] = image[0, y]; tempImg[tempImg.Width - 1, y] = image[image.Width - 1, y]; for (int x = 1; x < tempImg.Width - 1; ++x) { ColorFloatPixel p = new ColorFloatPixel(); float x1 = (float)Math.Truncate(x / n), x2 = (int)(x / n) + 1; p.r = (x2 - (float)x / n) * image[(int)x1, y].r + ((float)x / n - x1) * image[(int)x2, y].r; p.g = (x2 - (float)x / n) * image[(int)x1, y].g + ((float)x / n - x1) * image[(int)x2, y].g; p.b = (x2 - (float)x / n) * image[(int)x1, y].b + ((float)x / n - x1) * image[(int)x2, y].b; tempImg[x, y] = p; } } image = tempImg; tempImg = new ColorFloatImage(image.Width, (int)(image.Height * s)); n = (image.Height * s - 1) / ((float)image.Height - 1); for (int x = 0; x < image.Width; ++x) { tempImg[x, 0] = image[x, 0]; tempImg[x, tempImg.Height - 1] = image[x, image.Height - 1]; for (int y = 1; y < tempImg.Height - 1; ++y) { ColorFloatPixel p = new ColorFloatPixel(); float y1 = (float)Math.Truncate(y / n), y2 = (int)(y / n) + 1; p.r = (y2 - (float)y / n) * image[x, (int)y1].r + ((float)y / n - y1) * image[x, (int)y2].r; p.g = (y2 - (float)y / n) * image[x, (int)y1].g + ((float)y / n - y1) * image[x, (int)y2].g; p.b = (y2 - (float)y / n) * image[x, (int)y1].b + ((float)y / n - y1) * image[x, (int)y2].b; tempImg[x, y] = p; } } image = tempImg; }
public static ColorFloatImage BitmapToColorFloatImage(Bitmap B) { int W = B.Width, H = B.Height; ColorFloatImage res = new ColorFloatImage(W, H); if (B.PixelFormat == PixelFormat.Format8bppIndexed) { Color[] pi = B.Palette.Entries; byte[] pal = new byte[1024]; for (int i = 0; i < pi.Length; i++) { Color C = pi[i]; pal[i * 4] = C.B; pal[i * 4 + 1] = C.G; pal[i * 4 + 2] = C.R; pal[i * 4 + 3] = C.A; } LockBitmapInfo lbi = LockBitmap(B, PixelFormat.Format8bppIndexed, 1); try { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int c = lbi.data[lbi.linewidth * j + i]; int b = pal[c * 4]; int g = pal[c * 4 + 1]; int r = pal[c * 4 + 2]; res[i, j] = new ColorFloatPixel() { b = b, g = g, r = r, a = 0.0f }; } } } finally { UnlockBitmap(lbi); } } else { LockBitmapInfo lbi = LockBitmap(B); try { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int b = lbi.data[lbi.linewidth * j + i * 4]; int g = lbi.data[lbi.linewidth * j + i * 4 + 1]; int r = lbi.data[lbi.linewidth * j + i * 4 + 2]; res[i, j] = new ColorFloatPixel() { b = b, g = g, r = r, a = 0.0f }; } } } finally { UnlockBitmap(lbi); } } return(res); }