static GrayscaleFloatImage RD(float[,] matr) { int h = matr.GetUpperBound(1) + 1; int w = matr.GetUpperBound(0) + 1; GrayscaleFloatImage tmp_img = new GrayscaleFloatImage(w, h); float max_one = 0, min_one = 255; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { max_one = Math.Max(max_one, matr[x, y]); min_one = Math.Min(min_one, matr[x, y]); } } for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { /*if (matr[x, y] >= 2) * tmp_img[x, y] = 255; * else * tmp_img[x, y] = 0; */ tmp_img[x, y] = (matr[x, y] - min_one) * 255 / (max_one - min_one); } } return(tmp_img); }
public static GrayscaleFloatImage Bilinear(GrayscaleFloatImage image, double n) { var width = (int)(image.Width * n); var height = (int)(image.Height * n); var result = new GrayscaleFloatImage(width, height); // for (var y = 0; y < height; y++) { var dy = y / n; for (var x = 0; x < width; x++) { var dx = x / n; if ((Math.Abs(dx % 1) <= 0) && (Math.Abs(dy % 1) <= 0)) { result[x, y] = image[(int)dx, (int)dy]; } else { var x1 = (int)dx; var y1 = (int)dy; result[x, y] = (float)((dy - y1) * ((x1 + 1 - dx) * image[x1, y1 + 1 >= image.Height ? image.Height - 1 : y1 + 1] + (dx - x1) * image[x1 + 1 >= image.Width ? image.Width - 1 : x1 + 1, y1 + 1 >= image.Height ? image.Height - 1 : y1 + 1]) + (y1 + 1 - dy) * ((x1 + 1 - dx) * image[x1, y1] + (dx - x1) * image[x1 + 1 >= image.Width ? image.Width - 1 : x1 + 1, y1])); } } } return(result); }
public static GrayscaleFloatImage Process(GrayscaleFloatImage Image, double SigmaD, double SigmaR) { var width = Image.Width; var height = Image.Height; var windowSize = 15; var result = Image; windowSize = windowSize % 2 == 0 ? windowSize - 1 : windowSize; var n = (windowSize - 1) / 2; for (var y = 0; y < height; y++) for (var x = 0; x < width; x++) { double ch = 0; double zn = 0; for (var j = 0; j < windowSize; j++) for (var i = 0; i < windowSize; i++) { var w = Math.Exp((-(i - n) * (i - n) - (j - n) * (j - n)) / (2 * SigmaD * SigmaD) - (Image[x, y] - Image[x + i - n < 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n]) * (Image[x, y] - Image[x + i - n < 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n]) / (2 * SigmaR * SigmaR)); ch += Image[x + i - n <= 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n] * w; zn += w; } result[x, y] = (float)(ch / zn); } return result; }
public static GrayscaleFloatImage Vessels(GrayscaleFloatImage image, int size, double sigma) { var angle = 15; var images = new GrayscaleFloatImage[6]; for (var i = 0; i < 6; i++) { images[i] = Gabor(image, Filters.Gabor(6, 2, 3, angle, 1, 0), 6, 6); angle += 30; } var result = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { result[x, y] = 0; for (var i = 0; i < 6; i++) { if (images[i][x, y] < result[x, y]) { result[x, y] = images[i][x, y]; } } result[x, y] = Math.Abs(result[x, y]); } } return(result); }
public static GrayscaleFloatImage Process(GrayscaleFloatImage image, float sigma, float th, float tl) { var canny = new Canny(image, sigma, th, tl); var result = canny.Image; return(result); }
public static Bitmap ImageToBitmap(GrayscaleFloatImage 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++) { byte c = image[i, j] < 0.0f ? (byte)0 : image[i, j] > 255.0f ? (byte)255 : (byte)image[i, j]; lbi.data[lbi.linewidth * j + i * 4] = c; lbi.data[lbi.linewidth * j + i * 4 + 1] = c; lbi.data[lbi.linewidth * j + i * 4 + 2] = c; } } } finally { UnlockBitmap(lbi); } return(B); }
public static GrayscaleFloatImage Process(GrayscaleFloatImage Image, double SigmaD, double SigmaR) { var width = Image.Width; var height = Image.Height; var windowSize = 15; var result = Image; windowSize = windowSize % 2 == 0 ? windowSize - 1 : windowSize; var n = (windowSize - 1) / 2; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { double ch = 0; double zn = 0; for (var j = 0; j < windowSize; j++) { for (var i = 0; i < windowSize; i++) { var w = Math.Exp((-(i - n) * (i - n) - (j - n) * (j - n)) / (2 * SigmaD * SigmaD) - (Image[x, y] - Image[x + i - n < 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n]) * (Image[x, y] - Image[x + i - n < 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n]) / (2 * SigmaR * SigmaR)); ch += Image[x + i - n <= 0 ? 0 : x + i - n >= width ? width - 1 : x + i - n, y + j - n < 0 ? 0 : y + j - n >= height ? height - 1 : y + j - n] * w; zn += w; } } result[x, y] = (float)(ch / zn); } } return(result); }
public static GrayscaleFloatImage Bilinear(GrayscaleFloatImage image, double n) { var width = (int)(image.Width * n); var height = (int)(image.Height * n); var result = new GrayscaleFloatImage(width, height); // for (var y = 0; y < height; y++) { var dy = y / n; for (var x = 0; x < width; x++) { var dx = x / n; if ((Math.Abs(dx % 1) <= 0) && (Math.Abs(dy % 1) <= 0)) { result[x, y] = image[(int)dx, (int)dy]; } else { var x1 = (int)dx; var y1 = (int)dy; result[x, y] = (float)((dy - y1) * ((x1 + 1 - dx) * image[x1, y1 + 1 >= image.Height ? image.Height - 1 : y1 + 1] + (dx - x1) * image[x1 + 1 >= image.Width ? image.Width - 1 : x1 + 1, y1 + 1 >= image.Height ? image.Height - 1 : y1 + 1]) + (y1 + 1 - dy) * ((x1 + 1 - dx) * image[x1, y1] + (dx - x1) * image[x1 + 1 >= image.Width ? image.Width - 1 : x1 + 1, y1])); } } } return result; }
static float[, ][,] matrix_der2(GrayscaleFloatImage image, float sigma) { float[, ][,] out_matr = new float[image.Width, image.Height][, ]; float[,] Gxx = g_kernel(sigma, "xx"); float[,] Gxy = g_kernel(sigma, "xy"); float[,] Gyy = g_kernel(sigma, "yy"); int rad = (int)Math.Round(3 * sigma); GrayscaleFloatImage temp_image = img_expansion(image.ToColorFloatImage(), "odd", rad).ToGrayscaleFloatImage(); for (int y = rad; y < image.Height + rad; ++y) { for (int x = rad; x < image.Width + rad; ++x) { float[,] q = new float[2, 2]; for (int m = -rad; m <= rad; ++m) { for (int n = -rad; n <= rad; ++n) { q[0, 0] += temp_image[x + n, y + m] * Gxx[rad + n, rad + m]; q[0, 1] += temp_image[x + n, y + m] * Gxy[rad + n, rad + m]; q[1, 1] += temp_image[x + n, y + m] * Gyy[rad + n, rad + m]; } } q[1, 0] = q[0, 1]; out_matr[x - rad, y - rad] = q; } } return(out_matr); }
public static GrayscaleFloatImage MedianFilter(GrayscaleFloatImage image, int kernelSize) { var tmpImage = new GrayscaleFloatImage(image.Width, image.Height); var kernel = new float[kernelSize * kernelSize]; for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { for (var j = 0; j < kernelSize; j++) for (var i = 0; i < kernelSize; i++) { var dx = x + i - kernelSize / 2; var dy = y + j - kernelSize / 2; if (dx < 0) dx = 0; if (dy < 0) dy = 0; if (dx >= image.Width) dx = image.Width - 1; if (dy >= image.Height) dy = image.Height - 1; kernel[i + j * kernelSize] = image[dx, dy]; } kernel = kernel.OrderBy(e => e).ToArray(); tmpImage[x, y] = kernel[kernel.Length / 2 + 1]; } return tmpImage; }
static void Main(string[] args) { GrayscaleFloatImage input_image = ImageIO.FileToGrayscaleFloatImage(args[0]); input_image = ExtendedRD(input_image, 4); //input_image = temp_func(input_image, 2); ImageIO.ImageToFile(input_image, args[2]); }
public static void InversionProcess(GrayscaleFloatImage image) { for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { image[x, y] = 255 - image[x, y]; } }
public static GrayscaleFloatImage ClockWiseRotate(GrayscaleFloatImage image, int angle) { GrayscaleFloatImage resultImage; switch (angle) { case 90: resultImage = new GrayscaleFloatImage(image.Height, image.Width); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { resultImage[image.Height - 1 - y, x] = image[x, y]; } break; case 180: resultImage = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { resultImage[image.Width - 1 - x, image.Height - 1 - y] = image[x, y]; } break; case 270: resultImage = new GrayscaleFloatImage(image.Height, image.Width); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { resultImage[y, image.Width - 1 - x] = image[x, y]; } break; default: var cos = Math.Cos(angle * 0.0174533); var sin = Math.Sin(angle * 0.0174533); var width = (int)Math.Floor(cos * image.Height + sin * image.Width); var height = (int)Math.Floor(sin * image.Height + cos * image.Width); resultImage = new GrayscaleFloatImage(width, height); for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var b = sin * image.Height; var hx = (x - b) * cos + y * sin; var hy = -(x - b) * sin + y * cos; float c = 0; if (hx >= 1 && hy >= 1 && hx < image.Width - 1 && hy < image.Height - 1) { var x1 = (int)hx; var y1 = (int)hy; c = (float)((hy - y1) * ((x1 + 1 - hx) * image[x1, y1 + 1] + (hx - x1) * image[x1 + 1, y1 + 1]) + (y1 + 1 - hy) * ((x1 + 1 - hx) * image[x1, y1] + (hx - x1) * image[x1 + 1, y1])); } resultImage[x, y] = c; } } break; } return resultImage; }
public static double Psnr(GrayscaleFloatImage image1, GrayscaleFloatImage image2) { var mse = Mse(image1, image2); if (Math.Abs(mse) <= 0) { return(0); } return(10 * Math.Log10(255 * 255 / mse)); }
public static void InversionProcess(GrayscaleFloatImage image) { for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { image[x, y] = 255 - image[x, y]; } } }
public static void FlipVertical(GrayscaleFloatImage image) { for (var y = 0; y < image.Height / 2; y++) for (var x = 0; x < image.Width; x++) { var p = image[x, y]; image[x, y] = image[x, image.Height - 1 - y]; image[x, image.Height - 1 - y] = p; } }
public static void FlipHorizontal(GrayscaleFloatImage image) { for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width / 2; x++) { var p = image[x, y]; image[x, y] = image[image.Width - 1 - x, y]; image[image.Width - 1 - x, y] = p; } }
private static void NewGradient(GrayscaleFloatImage image, int r, float sig) { for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { ImageX[x, y] = ImageY[x, y] = 0; } } float s = 0, koef = 1 / (float)Math.Sqrt(2 * Math.PI * sig * sig); for (var j = -r; j <= r; j++) { for (var i = -r; i <= r; i++) { s += Math.Abs((float)(koef * (-2) * i / (sig * sig) * Math.Exp(-((i * i + j * j) / (2 * sig * sig))))); } } for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { for (var j = -r; j <= r; j++) { for (var i = -r; i <= r; i++) { ImageX[x, y] += (float)(koef * (-2) * i / (sig * sig) * Math.Exp(-((i * i + j * j) / (2 * sig * sig)))) * image[x + i < 0 ? 0 : x + i >= image.Width ? image.Width - 1 : x + i, y + j < 0 ? 0 : y + j >= image.Height ? image.Height - 1 : y + j]; } } ImageX[x, y] = ImageX[x, y] / s; } } for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { for (var j = -r; j <= r; j++) { for (var i = -r; i <= r; i++) { ImageY[x, y] += (float)(koef * (-2) * j / (sig * sig) * Math.Exp(-((i * i + j * j) / (2 * sig * sig)))) * image[x + i < 0 ? 0 : x + i >= image.Width ? image.Width - 1 : x + i, y + j < 0 ? 0 : y + j >= image.Height ? image.Height - 1 : y + j]; } } ImageY[x, y] = ImageY[x, y] / s; } } for (var x = 0; x < image.Width; x++) { for (var y = 0; y < image.Height; y++) { ImageGrad[x, y] = (float)Math.Sqrt(ImageY[x, y] * ImageY[x, y] + ImageX[x, y] * ImageX[x, y]); } } }
public static GrayscaleFloatImage Bicubic(GrayscaleFloatImage image, double n) { int oldWidth = image.Width, oldHeight = image.Height; var newWidth = (int)Math.Round(image.Width * n); var newHeight = (int)Math.Round(image.Height * n); var result = new GrayscaleFloatImage(newWidth, newHeight); // var arr = new float[4]; var temp = new float[4][]; for (var i = 0; i < 4; i++) { temp[i] = new float[4]; } // for (var y = 0; y < newHeight; y++) { for (var x = 0; x < newWidth; x++) { var x0 = (int)(x / n) - 1; var y0 = (int)(y / n) - 1; var x1 = (int)(x / n); var y1 = (int)(y / n); var x2 = (int)(x / n) + 1; var y2 = (int)(y / n) + 1; var x3 = (int)(x / n) + 2; var y3 = (int)(y / n) + 2; // temp[0][0] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[0][1] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[0][2] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[0][3] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[1][0] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[1][1] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[1][2] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[1][3] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[2][0] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[2][1] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[2][2] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[2][3] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[3][0] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[3][1] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[3][2] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[3][3] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; // arr[0] = (float)(temp[0][1] + 0.5 * (float)(y / n - y1) * (temp[0][2] - temp[0][0] + (float)(y / n - y1) * (2.0 * temp[0][0] - 5.0 * temp[0][1] + 4.0 * temp[0][2] - temp[0][3] + (float)(y / n - y1) * (3.0 * (temp[0][1] - temp[0][2]) + temp[0][3] - temp[0][0])))); arr[1] = (float)(temp[1][1] + 0.5 * (float)(y / n - y1) * (temp[1][2] - temp[1][0] + (float)(y / n - y1) * (2.0 * temp[1][0] - 5.0 * temp[1][1] + 4.0 * temp[1][2] - temp[1][3] + (float)(y / n - y1) * (3.0 * (temp[1][1] - temp[1][2]) + temp[1][3] - temp[1][0])))); arr[2] = (float)(temp[2][1] + 0.5 * (float)(y / n - y1) * (temp[2][2] - temp[2][0] + (float)(y / n - y1) * (2.0 * temp[2][0] - 5.0 * temp[2][1] + 4.0 * temp[2][2] - temp[2][3] + (float)(y / n - y1) * (3.0 * (temp[2][1] - temp[2][2]) + temp[2][3] - temp[2][0])))); arr[3] = (float)(temp[3][1] + 0.5 * (float)(y / n - y1) * (temp[3][2] - temp[3][0] + (float)(y / n - y1) * (2.0 * temp[3][0] - 5.0 * temp[3][1] + 4.0 * temp[3][2] - temp[3][3] + (float)(y / n - y1) * (3.0 * (temp[3][1] - temp[3][2]) + temp[3][3] - temp[3][0])))); result[x, y] = (float)(arr[1] + 0.5 * (float)(x / n - x1) * (arr[2] - arr[0] + (float)(x / n - x1) * (2.0 * arr[0] - 5.0 * arr[1] + 4.0 * arr[2] - arr[3] + (float)(x / n - x1) * (3.0 * (arr[1] - arr[2]) + arr[3] - arr[0])))); } } return(result); }
public static void FlipHorizontal(GrayscaleFloatImage image) { for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width / 2; x++) { var p = image[x, y]; image[x, y] = image[image.Width - 1 - x, y]; image[image.Width - 1 - x, y] = p; } } }
public static GrayscaleFloatImage Process(GrayscaleFloatImage image, double sigma) { var harris = new Harris(image, sigma); BuildGradient(); //compute image grayscale gradient BuildOrder2MomentMatrix(); //compute order 2 moment matrix; BuildHarrisResponse(); //compute harris reponse for Corners ApplyNonMaxSupression(); //apply non-maximum supression algorithm to eliminate the weak Corners var result = WriteResultImage(image); return(result); }
public static double Mse(GrayscaleFloatImage image1, GrayscaleFloatImage image2) { double sum = 0; var height = image1.Height; var width = image1.Width; for (var j = 0; j < height; j++) for (var i = 0; i < width; i++) { sum += (image1.rawdata[i + j * width] - image2.rawdata[i + j * width]) * (image1.rawdata[i + j * width] - image2.rawdata[i + j * width]); } return sum / (height * width); }
public static void FlipVertical(GrayscaleFloatImage image) { for (var y = 0; y < image.Height / 2; y++) { for (var x = 0; x < image.Width; x++) { var p = image[x, y]; image[x, y] = image[x, image.Height - 1 - y]; image[x, image.Height - 1 - y] = p; } } }
public static GrayscaleFloatImage FileToGrayscaleFloatImage(string filename) { if (CheckPGM(filename)) { return(ReadPGM(filename)); } Bitmap B = new Bitmap(filename); GrayscaleFloatImage res = BitmapToGrayscaleFloatImage(B); B.Dispose(); return(res); }
public static GrayscaleFloatImage Bicubic(GrayscaleFloatImage image, double n) { int oldWidth = image.Width, oldHeight = image.Height; var newWidth = (int)Math.Round(image.Width * n); var newHeight = (int)Math.Round(image.Height * n); var result = new GrayscaleFloatImage(newWidth, newHeight); // var arr = new float[4]; var temp = new float[4][]; for (var i = 0; i < 4; i++) temp[i] = new float[4]; // for (var y = 0; y < newHeight; y++) for (var x = 0; x < newWidth; x++) { var x0 = (int)(x / n) - 1; var y0 = (int)(y / n) - 1; var x1 = (int)(x / n); var y1 = (int)(y / n); var x2 = (int)(x / n) + 1; var y2 = (int)(y / n) + 1; var x3 = (int)(x / n) + 2; var y3 = (int)(y / n) + 2; // temp[0][0] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[0][1] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[0][2] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[0][3] = image[x0 < 0 ? 0 : x0 >= oldWidth ? oldWidth - 1 : x0, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[1][0] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[1][1] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[1][2] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[1][3] = image[x1 < 0 ? 0 : x1 >= oldWidth ? oldWidth - 1 : x1, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[2][0] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[2][1] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[2][2] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[2][3] = image[x2 < 0 ? 0 : x2 >= oldWidth ? oldWidth - 1 : x2, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; temp[3][0] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y0 < 0 ? 0 : y0 >= oldHeight ? oldHeight - 1 : y0]; temp[3][1] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y1 < 0 ? 0 : y1 >= oldHeight ? oldHeight - 1 : y1]; temp[3][2] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y2 < 0 ? 0 : y2 >= oldHeight ? oldHeight - 1 : y2]; temp[3][3] = image[x3 < 0 ? 0 : x3 >= oldWidth ? oldWidth - 1 : x3, y3 < 0 ? 0 : y3 >= oldHeight ? oldHeight - 1 : y3]; // arr[0] = (float)(temp[0][1] + 0.5 * (float)(y / n - y1) * (temp[0][2] - temp[0][0] + (float)(y / n - y1) * (2.0 * temp[0][0] - 5.0 * temp[0][1] + 4.0 * temp[0][2] - temp[0][3] + (float)(y / n - y1) * (3.0 * (temp[0][1] - temp[0][2]) + temp[0][3] - temp[0][0])))); arr[1] = (float)(temp[1][1] + 0.5 * (float)(y / n - y1) * (temp[1][2] - temp[1][0] + (float)(y / n - y1) * (2.0 * temp[1][0] - 5.0 * temp[1][1] + 4.0 * temp[1][2] - temp[1][3] + (float)(y / n - y1) * (3.0 * (temp[1][1] - temp[1][2]) + temp[1][3] - temp[1][0])))); arr[2] = (float)(temp[2][1] + 0.5 * (float)(y / n - y1) * (temp[2][2] - temp[2][0] + (float)(y / n - y1) * (2.0 * temp[2][0] - 5.0 * temp[2][1] + 4.0 * temp[2][2] - temp[2][3] + (float)(y / n - y1) * (3.0 * (temp[2][1] - temp[2][2]) + temp[2][3] - temp[2][0])))); arr[3] = (float)(temp[3][1] + 0.5 * (float)(y / n - y1) * (temp[3][2] - temp[3][0] + (float)(y / n - y1) * (2.0 * temp[3][0] - 5.0 * temp[3][1] + 4.0 * temp[3][2] - temp[3][3] + (float)(y / n - y1) * (3.0 * (temp[3][1] - temp[3][2]) + temp[3][3] - temp[3][0])))); result[x, y] = (float)(arr[1] + 0.5 * (float)(x / n - x1) * (arr[2] - arr[0] + (float)(x / n - x1) * (2.0 * arr[0] - 5.0 * arr[1] + 4.0 * arr[2] - arr[3] + (float)(x / n - x1) * (3.0 * (arr[1] - arr[2]) + arr[3] - arr[0])))); } return result; }
public static GrayscaleFloatImage Gabor(GrayscaleFloatImage image, float[] kernel, int kernelWidth, int kernelHeight, int mode = 0) { var tmpImage = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { float sum = 0; for (var j = 0; j < kernelHeight; j++) { for (var i = 0; i < kernelWidth; i++) { var dx = x + i - kernelWidth / 2; var dy = y + j - kernelHeight / 2; if (dx < 0) { dx = 0; } if (dy < 0) { dy = 0; } if (dx >= image.Width) { dx = image.Width - 1; } if (dy >= image.Height) { dy = image.Height - 1; } sum += image[dx, dy] * kernel[i + j * kernelWidth]; } } switch (mode) { case 1: sum += 128; break; case 2: sum = Math.Abs(sum); break; } tmpImage[x, y] = sum; } } return(tmpImage); }
public static double Mse(GrayscaleFloatImage image1, GrayscaleFloatImage image2) { double sum = 0; var height = image1.Height; var width = image1.Width; for (var j = 0; j < height; j++) { for (var i = 0; i < width; i++) { sum += (image1.rawdata[i + j * width] - image2.rawdata[i + j * width]) * (image1.rawdata[i + j * width] - image2.rawdata[i + j * width]); } } return(sum / (height * width)); }
public Harris(GrayscaleFloatImage image, double sigma) { Width = image.Width; Height = image.Height; Corners = new int[Width * Height]; K = 0.05; CornerTreshold = 9000000; NmSupressWindowSize = 11; Gray = new float[Width * Height]; Gray = image.rawdata; GradX = new float[Width * Height]; GradY = new float[Width * Height]; Mx = new int[Width * Height]; My = new int[Width * Height]; Mxy = new int[Width * Height]; }
public static GrayscaleFloatImage WriteResultImage(GrayscaleFloatImage image) { for (var i = 2 * Width + 2; i < Width * Height - 2 * Width - 2; i++) { if ((byte)Corners[i] != 0) { for (var ii = -2; ii <= 2; ii++) { for (var jj = -2; jj <= 2; jj++) { image.rawdata[(i + ii * Width + jj)] = 1; } } } } return(image); }
public void InitializePath(string pathDir) { imgDir = Directory.GetFiles(pathDir).Where(a => a.Split("/").Last()[0] != '.').ToArray(); int count = imgDir.Count(); images = new List <float[]>(); for (int i = 0; i < count; i++) { Bitmap img = new Bitmap(imgDir[i]); GrayscaleFloatImage a = ImageIO.BitmapToGrayscaleFloatImage(img); images.Add(new float[a.rawdata.Length]); a.rawdata.CopyTo(images[i], 0); } threads = new Thread[numProcs]; jobs = new ConcurrentQueue <float[]>(images); }
public static GrayscaleFloatImage DownBilinear(GrayscaleFloatImage image, double n) { var width = (int)(image.Width / n); var height = (int)(image.Height / n); var result = new GrayscaleFloatImage(width, height); // var sigma = Math.Sqrt(n * n - 1); var data = new double[image.rawdata.Length]; var template = new double[image.rawdata.Length]; for (var i = 0; i < image.rawdata.Length; i++) { data[i] = Convert.ToDouble(image.rawdata[i]); } // ConvolutionGauss.GaussProcess(data, image.Width, image.Height, sigma: sigma, windowSize: (int)(sigma * 3), temp: template, dest: data); // for (var i = 0; i < image.rawdata.Length; i++) { image.rawdata[i] = Convert.ToSingle(data[i]); } for (var y = 0; y < height; y++) { var dy = y * n; for (var x = 0; x < width; x++) { var dx = x * n; if ((Math.Abs(dx % 1) <= 0) || (Math.Abs(dy % 1) <= 0)) { result[x, y] = image[(int)dx, (int)dy]; } else { var x1 = (int)dx; var y1 = (int)dy; result[x, y] = (float)((dy - y1) * ((x1 + 1 - dx) * image[x1, y1 + 1] + (dx - x1) * image[x1 + 1, y1 + 1]) + (y1 + 1 - dy) * ((x1 + 1 - dx) * image[x1, y1] + (dx - x1) * image[x1 + 1, y1])); } } } return(result); }
public static GrayscaleFloatImage MedianFilter(GrayscaleFloatImage image, int kernelSize) { var tmpImage = new GrayscaleFloatImage(image.Width, image.Height); var kernel = new float[kernelSize * kernelSize]; for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { for (var j = 0; j < kernelSize; j++) { for (var i = 0; i < kernelSize; i++) { var dx = x + i - kernelSize / 2; var dy = y + j - kernelSize / 2; if (dx < 0) { dx = 0; } if (dy < 0) { dy = 0; } if (dx >= image.Width) { dx = image.Width - 1; } if (dy >= image.Height) { dy = image.Height - 1; } kernel[i + j * kernelSize] = image[dx, dy]; } } kernel = kernel.OrderBy(e => e).ToArray(); tmpImage[x, y] = kernel[kernel.Length / 2 + 1]; } } return(tmpImage); }
public static GrayscaleFloatImage Gabor(GrayscaleFloatImage image, float[] kernel, int kernelWidth, int kernelHeight, int mode = 0) { var tmpImage = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { float sum = 0; for (var j = 0; j < kernelHeight; j++) for (var i = 0; i < kernelWidth; i++) { var dx = x + i - kernelWidth / 2; var dy = y + j - kernelHeight / 2; if (dx < 0) dx = 0; if (dy < 0) dy = 0; if (dx >= image.Width) dx = image.Width - 1; if (dy >= image.Height) dy = image.Height - 1; sum += image[dx, dy] * kernel[i + j * kernelWidth]; } switch (mode) { case 1: sum += 128; break; case 2: sum = Math.Abs(sum); break; } tmpImage[x, y] = sum; } return tmpImage; }
static GrayscaleFloatImage dir(ColorFloatImage image, float sigma) { string mode = "even"; int rad = (int)Math.Round(3 * sigma); ColorFloatImage temp_img = img_expansion(image, mode, rad); /* * for (int y = 1; y < temp_image_x.Height + 1; y++) * for (int x = 1; x < temp_image_x.Width + 1; x++) * temp_image_x[x - 1, y - 1] = temp_image[x + 1, y] + (-1) * temp_image[x, y]; * for (int y = 1; y < temp_image_y.Height + 1; y++) * for (int x = 1; x < temp_image_y.Width + 1; x++) * temp_image_y[x - 1, y - 1] = temp_image[x, y + 1] + (-1) * temp_image[x, y]; */ GrayscaleFloatImage temp_image = temp_img.ToGrayscaleFloatImage(); GrayscaleFloatImage out_img = new GrayscaleFloatImage(image.Width, image.Height); float[,] Gx = g_kernel_deriv(sigma, "x"); float[,] Gy = g_kernel_deriv(sigma, "y"); for (int y = rad; y < image.Height + rad; y++) { for (int x = rad; x < image.Width + rad; x++) { float val_x = 0, val_y = 0; int m = 0; for (int j = y - rad; j < y + rad + 1; j++, m++) { int n = 0; for (int k = x - rad; k < x + rad + 1; k++, n++) { val_x = val_x + Gx[n, m] * temp_image[k, j]; val_y = val_y + Gy[n, m] * temp_image[k, j]; } } if (val_x == 0 && val_y != 0) { out_img[x - rad, y - rad] = 64; continue; } double theta = Math.Atan2(val_y - rad, val_x - rad) * (180 / Math.PI); if (theta <= 22.5 && theta > -22.5 || theta <= -157.5 && theta > 157.5) { out_img[x - rad, y - rad] = 64; // -> } else if (theta <= 67.5 && theta > 22.5 || theta >= -157.5 && theta < -112.5) { out_img[x - rad, y - rad] = 192; // / } else if (theta > 67.5 && theta <= 112.5 || theta >= -112.5 && theta < -67.5) { out_img[x - rad, y - rad] = 128; // ^ } else if (theta > 112.5 && theta <= 157.5 || theta >= -67.5 && theta < -22.5) { out_img[x - rad, y - rad] = 255; // \ } } } return(out_img); }
public static GrayscaleFloatImage DownBilinear(GrayscaleFloatImage image, double n) { var width = (int)(image.Width / n); var height = (int)(image.Height / n); var result = new GrayscaleFloatImage(width, height); // var sigma = Math.Sqrt(n * n - 1); var data = new double[image.rawdata.Length]; var template = new double[image.rawdata.Length]; for (var i = 0; i < image.rawdata.Length; i++) { data[i] = Convert.ToDouble(image.rawdata[i]); } // ConvolutionGauss.GaussProcess(data, image.Width, image.Height, sigma: sigma, windowSize: (int)(sigma * 3), temp: template, dest: data); // for (var i = 0; i < image.rawdata.Length; i++) { image.rawdata[i] = Convert.ToSingle(data[i]); } for (var y = 0; y < height; y++) { var dy = y * n; for (var x = 0; x < width; x++) { var dx = x * n; if ((Math.Abs(dx % 1) <= 0) || (Math.Abs(dy % 1) <= 0)) { result[x, y] = image[(int)dx, (int)dy]; } else { var x1 = (int)dx; var y1 = (int)dy; result[x, y] = (float)((dy - y1) * ((x1 + 1 - dx) * image[x1, y1 + 1] + (dx - x1) * image[x1 + 1, y1 + 1]) + (y1 + 1 - dy) * ((x1 + 1 - dx) * image[x1, y1] + (dx - x1) * image[x1 + 1, y1])); } } } return result; }
public static GrayscaleFloatImage Process(GrayscaleFloatImage image, float sigma, float th, float tl) { var canny = new Canny(image, sigma, th, tl); var result = canny.Image; return result; }
public Canny(GrayscaleFloatImage image, float sigma, float th, float tl) { Image = image; ImageX = new GrayscaleFloatImage(image.Width, image.Height); ImageY = new GrayscaleFloatImage(image.Width, image.Height); ImageGrad = new GrayscaleFloatImage(image.Width, image.Height); MaxHysteresisThresh = th; MinHysteresisThresh = tl; Sigma = sigma; NewGradient(Image, (int)(3 * Sigma), Sigma); ImageGrad2 = new GrayscaleFloatImage(ImageGrad.Width, ImageGrad.Height); for (var i = 1; i < Image.Width - 1; i++) for (var j = 1; j < Image.Height - 1; j++) { var tang = Math.Round(Math.Atan2(ImageX[i, j], ImageY[i, j]) / (Math.PI / 4)) * (Math.PI / 4) - (Math.PI / 2); var hi = Math.Sign(Math.Cos(tang)); var hj = -Math.Sign(Math.Sin(tang)); if (ImageGrad[i, j] < ImageGrad[i + hi, j + hj] || ImageGrad[i, j] < ImageGrad[i - hi, j - hj]) { ImageGrad2[i, j] = 0; } else ImageGrad2[i, j] = ImageGrad[i, j]; } ImageGrad = ImageGrad2; var max = ImageGrad.rawdata.Max(); MinHysteresisThresh = max * MinHysteresisThresh; MaxHysteresisThresh = max * MaxHysteresisThresh; var stackX = new Stack<int>(); var stackY = new Stack<int>(); for (var i = 0; i < Image.Width; i++) for (var j = 0; j < Image.Height; j++) if (ImageGrad[i, j] > MaxHysteresisThresh) { ImageGrad[i, j] = 255; stackX.Push(i); stackY.Push(j); } else if (ImageGrad[i, j] > MinHysteresisThresh) ImageGrad[i, j] = 128; else ImageGrad[i, j] = 0; while (stackX.Count != 0) { var x = stackX.Pop(); var y = stackY.Pop(); for (var i = 0; i < 8; i++) { var nx = x + Dx[i]; var ny = y + Dy[i]; if ((nx < 0) || (ny < 0) || (nx >= Image.Width) || (ny >= Image.Height)) continue; if (ImageGrad[nx, ny] != 128) continue; ImageGrad[nx, ny] = 255; stackX.Push(nx); stackY.Push(ny); } } for (var i = 0; i < Image.Width; i++) for (var j = 0; j < Image.Height; j++) if (ImageGrad[i, j] == 255) Image[i, j] = 255; else Image[i, j] = 0; }
private static void NewGradient(GrayscaleFloatImage image, int r, float sig) { for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) ImageX[x, y] = ImageY[x, y] = 0; float s = 0, koef = 1 / (float)Math.Sqrt(2 * Math.PI * sig * sig); for (var j = -r; j <= r; j++) for (var i = -r; i <= r; i++) s += Math.Abs((float)(koef * (-2) * i / (sig * sig) * Math.Exp(-((i * i + j * j) / (2*sig * sig))))); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { for (var j = -r; j <= r; j++) for (var i = -r; i <= r; i++) { ImageX[x, y] += (float)(koef * (-2) * i / (sig * sig) * Math.Exp(-((i * i + j * j) / (2*sig * sig)))) * image[x + i < 0 ? 0 : x + i >= image.Width ? image.Width - 1 : x + i, y + j < 0 ? 0 : y + j >= image.Height ? image.Height - 1 : y + j]; } ImageX[x, y] = ImageX[x, y] / s; } for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { for (var j = -r; j <= r; j++) for (var i = -r; i <= r; i++) { ImageY[x, y] += (float)(koef * (-2) * j / (sig * sig) * Math.Exp(-((i * i + j * j) / (2*sig * sig)))) * image[x + i < 0 ? 0 : x + i >= image.Width ? image.Width - 1 : x + i, y + j < 0 ? 0 : y + j >= image.Height ? image.Height - 1 : y + j]; } ImageY[x, y] = ImageY[x, y] / s; } for (var x = 0; x < image.Width; x++) for (var y = 0; y < image.Height; y++) ImageGrad[x, y] = (float)Math.Sqrt(ImageY[x, y] * ImageY[x, y] + ImageX[x, y] * ImageX[x, y]); }
static GrayscaleFloatImage ExtendedRD(GrayscaleFloatImage img, int steps, int start = 1) { int[,] ridges_amount = new int[img.Width, img.Height]; double sigma = Math.Pow(Math.Sqrt(2), start - 1); GrayscaleFloatImage[] image_array = new GrayscaleFloatImage[steps]; GrayscaleFloatImage[] new_image_array = new GrayscaleFloatImage[steps]; int rad = 0; for (int i = 0; i < steps; ++i, sigma *= Math.Sqrt(2)) { var t = matrix_der2(img, (float)sigma); var matr = _solve_matr(t); image_array[i] = RD(matr); new_image_array[i] = nonmax(image_array[i].ToColorFloatImage(), (float)sigma); ImageIO.ImageToFile(new_image_array[i], "a_" + i.ToString() + ".bmp"); new_image_array[i] = img_expansion(new_image_array[i].ToColorFloatImage(), "odd", rad).ToGrayscaleFloatImage(); } int MAGIC_NUMBER = 3; for (int i = 0; i < steps; ++i) { for (int y = 0; y < img.Height; ++y) { for (int x = 0; x < img.Width; ++x) { if (image_array[i][x, y] > 0) { ridges_amount[x, y]++; } } } } GrayscaleFloatImage out_img = new GrayscaleFloatImage(img.Width, img.Height); for (int y = 0; y < img.Height; ++y) { for (int x = 0; x < img.Width; ++x) { if (ridges_amount[x, y] >= MAGIC_NUMBER) { for (int i = steps - MAGIC_NUMBER - 1; i >= 0; --i) { bool f = true; for (int j = 0; j < MAGIC_NUMBER; ++j) { bool an_f = false; for (int a = -rad; a <= rad; ++a) { for (int b = -rad; b <= rad; ++b) { if (new_image_array[i][x + rad + a, y + rad + b] > 0 && !an_f) { if (out_img[x, y] == 0) { out_img[x, y] = new_image_array[i + j][x + rad + a, y + rad + b]; } an_f = true; } } } if (!an_f) { f = false; out_img[x, y] = 0; } } if (f) { //out_img[x, y] /= MAGIC_NUMBER; break; } } } } } return(out_img); }
public static double Psnr(GrayscaleFloatImage image1, GrayscaleFloatImage image2) { var mse = Mse(image1, image2); if (Math.Abs(mse) <= 0) return 0; return 10 * Math.Log10(255 * 255 / mse); }
public Canny(GrayscaleFloatImage image, float sigma, float th, float tl) { Image = image; ImageX = new GrayscaleFloatImage(image.Width, image.Height); ImageY = new GrayscaleFloatImage(image.Width, image.Height); ImageGrad = new GrayscaleFloatImage(image.Width, image.Height); MaxHysteresisThresh = th; MinHysteresisThresh = tl; Sigma = sigma; NewGradient(Image, (int)(3 * Sigma), Sigma); ImageGrad2 = new GrayscaleFloatImage(ImageGrad.Width, ImageGrad.Height); for (var i = 1; i < Image.Width - 1; i++) { for (var j = 1; j < Image.Height - 1; j++) { var tang = Math.Round(Math.Atan2(ImageX[i, j], ImageY[i, j]) / (Math.PI / 4)) * (Math.PI / 4) - (Math.PI / 2); var hi = Math.Sign(Math.Cos(tang)); var hj = -Math.Sign(Math.Sin(tang)); if (ImageGrad[i, j] < ImageGrad[i + hi, j + hj] || ImageGrad[i, j] < ImageGrad[i - hi, j - hj]) { ImageGrad2[i, j] = 0; } else { ImageGrad2[i, j] = ImageGrad[i, j]; } } } ImageGrad = ImageGrad2; var max = ImageGrad.rawdata.Max(); MinHysteresisThresh = max * MinHysteresisThresh; MaxHysteresisThresh = max * MaxHysteresisThresh; var stackX = new Stack <int>(); var stackY = new Stack <int>(); for (var i = 0; i < Image.Width; i++) { for (var j = 0; j < Image.Height; j++) { if (ImageGrad[i, j] > MaxHysteresisThresh) { ImageGrad[i, j] = 255; stackX.Push(i); stackY.Push(j); } else if (ImageGrad[i, j] > MinHysteresisThresh) { ImageGrad[i, j] = 128; } else { ImageGrad[i, j] = 0; } } } while (stackX.Count != 0) { var x = stackX.Pop(); var y = stackY.Pop(); for (var i = 0; i < 8; i++) { var nx = x + Dx[i]; var ny = y + Dy[i]; if ((nx < 0) || (ny < 0) || (nx >= Image.Width) || (ny >= Image.Height)) { continue; } if (ImageGrad[nx, ny] != 128) { continue; } ImageGrad[nx, ny] = 255; stackX.Push(nx); stackY.Push(ny); } } for (var i = 0; i < Image.Width; i++) { for (var j = 0; j < Image.Height; j++) { if (ImageGrad[i, j] == 255) { Image[i, j] = 255; } else { Image[i, j] = 0; } } } }
public static GrayscaleFloatImage Vessels(GrayscaleFloatImage image, int size, double sigma) { var angle = 15; var images = new GrayscaleFloatImage[6]; for (var i = 0; i < 6; i++) { images[i] = Gabor(image, Filters.Gabor(6, 2, 3, angle, 1, 0), 6, 6); angle += 30; } var result = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) for (var x = 0; x < image.Width; x++) { result[x, y] = 0; for (var i = 0; i < 6; i++) { if (images[i][x, y] < result[x, y]) result[x, y] = images[i][x, y]; } result[x, y] = Math.Abs(result[x, y]); } return result; }
public static GrayscaleFloatImage CounterClockWiseRotate(GrayscaleFloatImage image, int angle) { GrayscaleFloatImage resultImage; switch (angle) { case 90: resultImage = new GrayscaleFloatImage(image.Height, image.Width); for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { resultImage[y, image.Width - 1 - x] = image[x, y]; } } break; case 180: resultImage = new GrayscaleFloatImage(image.Width, image.Height); for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { resultImage[image.Width - 1 - x, image.Height - 1 - y] = image[x, y]; } } break; case 270: resultImage = new GrayscaleFloatImage(image.Height, image.Width); for (var y = 0; y < image.Height; y++) { for (var x = 0; x < image.Width; x++) { resultImage[image.Height - 1 - y, x] = image[x, y]; } } break; default: var cos = Math.Cos(angle * 0.0174533); var sin = Math.Sin(angle * 0.0174533); var width = (int)Math.Floor(cos * image.Width + sin * image.Height); var height = (int)Math.Floor(sin * image.Width + cos * image.Height); resultImage = new GrayscaleFloatImage(width, height); for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var b = sin * image.Width; var hx = (x) * cos - (y - b) * sin; var hy = (x) * sin + (y - b) * cos; float c = 0; if (hx >= 1 && hy >= 1 && hx < image.Width - 1 && hy < image.Height - 1) { var x1 = (int)hx; var y1 = (int)hy; c = (float)((hy - y1) * ((x1 + 1 - hx) * image[x1, y1 + 1] + (hx - x1) * image[x1 + 1, y1 + 1]) + (y1 + 1 - hy) * ((x1 + 1 - hx) * image[x1, y1] + (hx - x1) * image[x1 + 1, y1])); } resultImage[x, y] = c; } } break; } return(resultImage); }
static GrayscaleFloatImage temp_func(GrayscaleFloatImage image, float sigma) { GrayscaleFloatImage out_matr = new GrayscaleFloatImage(image.Width, image.Height); float[,] Gxx = g_kernel(sigma, "yy"); //float[,] Gxy = g_kernel(sigma, "xy"); //float[,] Gyy = g_kernel(sigma, "yy"); int rad = (int)Math.Round(3 * sigma); GrayscaleFloatImage temp_image = img_expansion(image.ToColorFloatImage(), "odd", rad).ToGrayscaleFloatImage(); float common_sum = 0; for (int y = rad; y < image.Height + rad; ++y) { for (int x = rad; x < image.Width + rad; ++x) { float[,] q = new float[2, 2]; for (int m = -rad; m <= rad; ++m) { for (int n = -rad; n <= rad; ++n) { out_matr[x - rad, y - rad] += temp_image[x + n, y + m] * Gxx[rad + n, rad + m]; // q[0, 1] += temp_image[x + n, y + m] * Gxy[rad + n, rad + m]; // q[1, 1] += temp_image[x + n, y + m] * Gyy[rad + n, rad + m]; } } common_sum += out_matr[x - rad, y - rad]; } } Console.WriteLine(common_sum); float max_one = 0; float min_one = 0; for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; ++x) { //out_matr[x, y] += 128; if (out_matr[x, y] > max_one) { max_one = out_matr[x, y]; } if (out_matr[x, y] < min_one) { min_one = out_matr[x, y]; } } } min_one = -min_one; //if (min_one > max_one) // max_one = min_one; for (int y = 0; y < image.Height; ++y) { for (int x = 0; x < image.Width; ++x) { out_matr[x, y] *= 255 / max_one / 2; out_matr[x, y] += 128; } } return(out_matr); }
static GrayscaleFloatImage nonmax(ColorFloatImage image, float sigma) { 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, sigma); 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); }
public float[] Load_Image(string path) { GrayscaleFloatImage image = ImageIO.FileToGrayscaleFloatImage(path); return(image.rawdata); }