public static void ImageToFile(ColorFloatImage image, string filename) { using (Bitmap B = ImageToBitmap(image)) B.Save(filename); }
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); }
static GrayscaleFloatImage canny(ColorFloatImage image, int thr_high, int thr_low) { int MAX = 255; GrayscaleFloatImage temp_img = nonmax(image); float max_value = 0; for (int y = 0; y < temp_img.Height; y++) { for (int x = 0; x < temp_img.Width; x++) { if (temp_img[x, y] > max_value) { max_value = temp_img[x, y]; } } } float high_value = max_value * thr_high / 1000; float low_value = max_value * thr_low / 1000; GrayscaleFloatImage out_img = new GrayscaleFloatImage(image.Width, image.Height); bool[,] visited_pixels = new bool[image.Width, image.Height]; for (int y = 0; y < temp_img.Height; y++) { for (int x = 0; x < temp_img.Width; x++) { if (temp_img[x, y] < low_value) { visited_pixels[x, y] = true; out_img[x, y] = 0; } else if (temp_img[x, y] > high_value) { visited_pixels[x, y] = true; out_img[x, y] = MAX; } } } GrayscaleFloatImage dir_img = dir(image); List <Tuple <int, int> > pixels_list = new List <Tuple <int, int> >(); for (int coord_y = 0; coord_y < temp_img.Height; coord_y++) { for (int coord_x = 0; coord_x < temp_img.Width; coord_x++) { bool flag = false; flag = recursive(coord_x, coord_y, visited_pixels, pixels_list, dir_img, temp_img, out_img, ref flag); if (flag) { foreach (var a in pixels_list) { out_img[a.Item1, a.Item2] = 0; } pixels_list = new List <Tuple <int, int> >(); } else { foreach (var a in pixels_list) { out_img[a.Item1, a.Item2] = MAX; } pixels_list = new List <Tuple <int, int> >(); } } } return(out_img); }
static GrayscaleFloatImage nonmax(ColorFloatImage image) { int offset = 2; string mode = "even"; ColorFloatImage temp_image = img_expansion(image, mode, offset); temp_image = gradient(image, mode); temp_image = img_expansion(temp_image, mode, offset); GrayscaleFloatImage gray_grad = temp_image.ToGrayscaleFloatImage(); GrayscaleFloatImage dir_img = dir(image); GrayscaleFloatImage out_img = new GrayscaleFloatImage(image.Width, image.Height); float max_value = 0; for (int y = 0; y < out_img.Height; y++) { for (int x = 0; x < out_img.Width; x++) { float M = gray_grad[x + offset, y + offset]; if (M > max_value) { max_value = M; } switch (dir_img[x, y]) { case 0: // o //break; case 64: // -> if (M < gray_grad[x + offset + 1, y + offset] || M < gray_grad[x + offset - 1, y + offset]) { out_img[x, y] = 0; } else { out_img[x, y] = M; } break; case 128: // ^ if (M < gray_grad[x + offset, y + offset + 1] || M < gray_grad[x + offset, y + offset - 1]) { out_img[x, y] = 0; } else { out_img[x, y] = M; } break; case 192: // / if (M < gray_grad[x + offset + 1, y + offset + 1] || M < gray_grad[x + offset - 1, y + offset - 1]) { out_img[x, y] = 0; } else { out_img[x, y] = M; } break; case 255: // \ if (M < gray_grad[x + offset - 1, y + offset + 1] || M < gray_grad[x + offset + 1, y + offset - 1]) { out_img[x, y] = 0; } else { out_img[x, y] = M; } break; } } } float mult = 255 / max_value; for (int y = 0; y < out_img.Height; y++) { for (int x = 0; x < out_img.Width; x++) { out_img[x, y] *= mult; } } return(out_img); }
static ColorFloatImage gradient(ColorFloatImage image, String mode) { if (mode != "rep" && mode != "odd" && mode != "even") { Console.WriteLine("Wrong edge mode"); return(image); } ColorFloatImage temp_image = img_expansion(image, mode, 1); ColorFloatImage temp_image_x = new ColorFloatImage(image.Width, image.Height); ColorFloatImage temp_image_y = new ColorFloatImage(image.Width, image.Height); for (int y = 0; y < temp_image_x.Height; y++) { for (int x = 0; x < temp_image_x.Width; x++) { temp_image_x[x, y] = temp_image[x + 1, y] + (-1) * temp_image[x, y]; } } for (int y = 0; y < temp_image_y.Height; y++) { for (int x = 0; x < temp_image_y.Width; x++) { temp_image_y[x, y] = temp_image[x, y + 1] + (-1) * temp_image[x, y]; } } ColorFloatImage out_img = new ColorFloatImage(image.Width, image.Height); float max_color_r = 0; float max_color_g = 0; float max_color_b = 0; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { double r_x = temp_image_x[x, y].r, r_y = temp_image_y[x, y].r; double g_x = temp_image_x[x, y].g, g_y = temp_image_y[x, y].g; double b_x = temp_image_x[x, y].b, b_y = temp_image_y[x, y].b; ColorFloatPixel temp_pixel; temp_pixel.r = (float)(Math.Sqrt(r_x * r_x + r_y * r_y)); if (temp_pixel.r > max_color_r) { max_color_r = temp_pixel.r; } temp_pixel.g = (float)(Math.Sqrt(g_x * g_x + g_y * g_y)); if (temp_pixel.g > max_color_g) { max_color_g = temp_pixel.g; } temp_pixel.b = (float)(Math.Sqrt(b_x * b_x + b_y * b_y)); if (temp_pixel.r > max_color_b) { max_color_b = temp_pixel.b; } temp_pixel.a = 0; out_img[x, y] = temp_pixel; } } //contrast increasing block double multiplier_r = 255 / max_color_r; double multiplier_g = 255 / max_color_g; double multiplier_b = 255 / max_color_b; float mul = 1; if ((multiplier_r <= multiplier_g) && (multiplier_r <= multiplier_b)) { mul = (float)multiplier_r; } else if (multiplier_g <= multiplier_r && multiplier_g <= multiplier_b) { mul = (float)multiplier_g; } else if (multiplier_b <= multiplier_r && multiplier_b <= multiplier_g) { mul = (float)multiplier_b; } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { out_img[x, y] = out_img[x, y] * mul; } } return(out_img); }
static ColorFloatImage img_expansion(ColorFloatImage inp_img, String mode, int radius) { ColorFloatImage out_img = new ColorFloatImage(inp_img.Width + 2 * radius, inp_img.Height + 2 * radius); for (int y = radius; y < out_img.Height - radius; y++) //centre part pf image { for (int x = 0; x < out_img.Width; x++) { if (mode == "rep")//replicate { if (x < radius) { out_img[x, y] = inp_img[0, y - radius]; } else if (x >= radius + inp_img.Width) { out_img[x, y] = inp_img[inp_img.Width - 1, y - radius]; } else { out_img[x, y] = inp_img[x - radius, y - radius]; } } else if (mode == "odd")//odd { if (x < radius) { out_img[x, y] = 2 * inp_img[0, y - radius] + (-1) * inp_img[radius - x - 1, y - radius]; } else if (x >= radius + inp_img.Width) { out_img[x, y] = 2 * inp_img[inp_img.Width - 1, y - radius] + (-1) * inp_img[radius + 2 * inp_img.Width - x - 1, y - radius]; } else { out_img[x, y] = inp_img[x - radius, y - radius]; } } else if (mode == "even")//even { if (x < radius) { out_img[x, y] = inp_img[radius - x - 1, y - radius]; } else if (x >= radius + inp_img.Width) { out_img[x, y] = inp_img[radius + 2 * inp_img.Width - x - 1, y - radius]; } else { out_img[x, y] = inp_img[x - radius, y - radius]; } } } } for (int y = 0; y < radius; y++) //upper part of image { for (int x = 0; x < out_img.Width; x++) { if (mode == "rep")//replicate { out_img[x, y] = out_img[x, radius]; } else if (mode == "odd") // odd { out_img[x, y] = 2 * out_img[x, radius] + (-1) * out_img[x, 2 * radius - y - 1]; } else if (mode == "even") // even { out_img[x, y] = out_img[x, 2 * radius - y - 1]; } } } for (int y = inp_img.Height + radius; y < out_img.Height; y++) //lower part of image { for (int x = 0; x < out_img.Width; x++) { if (mode == "rep")//replicate { out_img[x, y] = out_img[x, out_img.Height - radius - 1]; } else if (mode == "odd")//odd { out_img[x, y] = 2 * out_img[x, out_img.Height - radius - 1] + (-1) * out_img[x, 2 * (out_img.Height - radius) - y - 1]; } else if (mode == "even")//even { out_img[x, y] = out_img[x, 2 * (out_img.Height - radius) - y - 1]; } } } return(out_img); }