public static GrayscaleFloatImage Gabor(GrayscaleFloatImage image, float sigma, float gamma, float theta, float lambda, float psi) { var tempImg = gabor(image, sigma, gamma, theta, lambda, psi); float max = -1000, min = 1000; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (tempImg[x, y] > max) { max = tempImg[x, y]; } if (tempImg[x, y] < min) { min = tempImg[x, y]; } } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { tempImg[x, y] = (tempImg[x, y] - min) * 255 / (max - min); } } return(tempImg); }
static GrayscaleFloatImage EdgeFunction(GrayscaleFloatImage edges) { GrayscaleFloatImage[] ans = new GrayscaleFloatImage[2]; ans[0] = new GrayscaleFloatImage(edges.Width, edges.Height); ans[1] = new GrayscaleFloatImage(edges.Width, edges.Height); for (int i = 0; i < ans[0].Width; i++) { for (int j = 0; j <= 1; j++) { ans[j][i, 0] = edges[i, 0]; ans[j][i, ans[0].Height] = edges[i, ans[0].Height]; } } for (int k = 0; k < 100; k++) { for (int i = 0; i < edges.Width; i++) { for (int j = 0; j < edges.Height; j++) { ans[(k + 1) % 2][i, j] = 1 / 4 * (ans[k % 2][i + 1, j] + ans[k % 2][i - 1, j] + ans[k % 2][i, j + 1] + ans[k % 2][i, j + 1]); } } } return(ans[1]); }
static private void EdgeDetect(GrayscaleFloatImage Mag, float thigh, float tlow, GrayscaleFloatImage Result) { VisitedMap = new GrayscaleFloatImage(Mag.Width, Mag.Height); for (int x = 0; x < Mag.Width; x++) { for (int y = 0; y < Mag.Height; y++) { VisitedMap[x, y] = 0; } } for (int x = 0; x < Mag.Width; x++) { for (int y = 0; y < Mag.Height; y++) { if (Mag[x, y] >= thigh) { Result[x, y] = 255; FollowEdge(x, y, Mag, thigh, tlow, ref Result); } else if (Mag[x, y] < tlow) { Result[x, y] = 0; } } } }
public static GrayscaleFloatImage GradientGrayscale(ColorFloatImage image, float[] window, float divider) { GrayscaleFloatImage dest = new GrayscaleFloatImage(image.Width, image.Height); int windowSide = (int)Math.Pow(window.Length, 0.5); int halfWindowSide = (windowSide - 1) / 2; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { List <int> i = NeighbourIndexes(x, image.Width - 1, halfWindowSide, FillMode.Reflection); List <int> j = NeighbourIndexes(y, image.Height - 1, halfWindowSide, FillMode.Reflection); List <ColorFloatPixel> pix = new List <ColorFloatPixel>(); for (int k = 0; k < windowSide; k++) { for (int n = 0; n < windowSide; n++) { pix.Add(image[i[n], j[k]]); } } dest[x, y] = RGB2GrayPix(Convolve(window, pix, divider)); // + 128; } } return(dest); }
private static GrayscaleFloatImage gabor(GrayscaleFloatImage image, float sigma, float gamma, float theta, float lambda, float psi) { GrayscaleFloatImage tempImg = new GrayscaleFloatImage(image.Width, image.Height); int n = (int)(6 * Math.Abs(sigma) + 1); n = n == 1 || n % 2 == 0 ? 3 : n; float[,] matr = new float[n, n]; float normValue = 0; theta *= (float)Math.PI / 180.0f; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { double x = (i - (n - 1) / 2) * Math.Cos(theta) + (j - (n - 1) / 2) * Math.Sin(theta); double y = -(i - (n - 1) / 2) * Math.Sin(theta) + (j - (n - 1) / 2) * Math.Cos(theta); matr[i, j] = (float)(Math.Exp(-(x * x + y * y * gamma * gamma) / (2 * sigma * sigma)) * Math.Cos(2 * x * Math.PI / lambda + psi)); normValue += (float)(Math.Exp(-(x * x + y * y * gamma * gamma) / (2 * sigma * sigma))); //matr[i, j]; //normValue = 1; } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { tempImg[x, y] = Conv(image, x, y, matr, normValue); } } return(tempImg); }
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 Complex[,] GaborFiltered(GrayscaleFloatImage image, int[,] mask, int r, float lam, float th, float sig, float gam, float psi, float s) { Complex[,] ans = new Complex[image.Width, image.Height]; Complex[,] filter = GetFilter(r, th, lam, sig, gam, psi, s); float sre = 0, sim = 0; for (int i = 0; i <= 2 * r; i++) { for (int j = 0; j <= 2 * r; j++) { sre += (float)filter[i, j].Re; sim += (float)filter[i, j].Im; } } // Console.WriteLine("re"+sre.ToString()); // Console.WriteLine("im" + sim.ToString()); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (x < 0 || y < 0 || x >= image.Width || y >= image.Height) { continue; } if (mask[x, y] == 1) { ans[x, y] = Convolution(image, filter, x, y) / (sig * sig); } // ans[x, y] = Convolution(image, filter, x, y); } } return(ans); }
/***********************************************************************/ public static void Gauss(ref GrayscaleFloatImage image, float sigma) { GrayscaleFloatImage tempImg = new GrayscaleFloatImage(image.Width, image.Height); int n = (int)(6 * Math.Abs(sigma) + 1); n = n == 1 || n % 2 == 0 ? 3 : n; float[,] matr = new float[n, n]; float normValue = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { matr[i, j] = (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma)); normValue += matr[i, j]; } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { tempImg[x, y] = GaussConversion(image, x, y, matr, normValue); } } image = tempImg; }
public static GrayscaleFloatImage FileToGrayscaleFloatImage(string filename) { Bitmap B = new Bitmap(filename); GrayscaleFloatImage res = BitmapToGrayscaleFloatImage(B); B.Dispose(); return(res); }
private static float SobelX(GrayscaleFloatImage image, int x, int y) { int w = image.Width - 1, h = image.Height - 1; return ((image[x + 1 > w ? w : x + 1, y - 1 < 0 ? 0 : y - 1] - image[x - 1 < 0 ? 0 : x - 1, y - 1 < 0 ? 0 : y - 1] + 2 * image[x + 1 > w ? w : x + 1, y] - 2 * image[x - 1 < 0 ? 0 : x - 1, y] + image[x + 1 > w ? w : x + 1, y + 1 > h ? h : y + 1] - image[x - 1 < 0 ? 0 : x - 1, y + 1 > h ? h : y + 1]) / 2); }
public static GrayscaleFloatImage vessels(ColorFloatImage ColorImage, float sigma) { var blurred = Gauss(ColorImage, 5.0f); var image = new GrayscaleFloatImage(ColorImage.Width, ColorImage.Height); for (int y = 0; y < ColorImage.Height; y++) { for (int x = 0; x < ColorImage.Width; x++) { image[x, y] = (ColorImage[x, y] - blurred[x, y]).g; } } float lambda = 6, psi = 0, gamma = 1; GrayscaleFloatImage result = new GrayscaleFloatImage(image.Width, image.Height); for (int theta = 0; theta < 180; theta += 30) { GrayscaleFloatImage tempIm = gabor(image, sigma, gamma, theta, lambda, psi); result = add(result, tempIm); } float min = 1000, max = -1000; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (result[x, y] > 0) { result[x, y] = 0; } else { result[x, y] = -result[x, y]; } if (result[x, y] > max) { max = result[x, y]; } if (result[x, y] < min) { min = result[x, y]; } } } for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { result[x, y] = (result[x, y] - min) * 5 > (max - min) * 5.0f / 7.5? (result[x, y] - min) * 255 / (max - min) :0; } } return(result); }
public static void Susperss_NonMaxima(ref GrayscaleFloatImage Mag, GrayscaleFloatImage Dir) { GrayscaleFloatImage tmp = new GrayscaleFloatImage(Mag.Width, Mag.Height); for (int x = 0; x < Mag.Width; x++) { for (int y = 0; y < Mag.Height; y++) { float dx = SobelX(Dir, x, y), dy = SobelY(Dir, x, y), v1 = 0, v2 = 0; int x_plus1 = x + 1 > Mag.Width - 1 ? Mag.Width - 1 : x + 1, x_minus1 = x - 1 < 0 ? 0 : x - 1, y_plus1 = y + 1 > Mag.Height - 1 ? Mag.Height - 1 : y + 1, y_minus1 = y - 1 < 0 ? 0 : y - 1; if (Math.Abs(dx) < Math.Abs(dy)) { float th = dx / dy; if (th < 0) { v1 = (1 + th) * Mag[x, y_minus1] - th * Mag[x_plus1, y_minus1]; v2 = (1 + th) * Mag[x, y_plus1] - th * Mag[x_minus1, y_plus1]; } else { v1 = (1 - th) * Mag[x, y_minus1] + th * Mag[x_plus1, y_minus1]; v2 = (1 - th) * Mag[x, y_plus1] + th * Mag[x_minus1, y_plus1]; } } else { float th = dy / dx; if (th < 0) { v1 = (1 + th) * Mag[x_minus1, y] - th * Mag[x_minus1, y_plus1]; v2 = (1 + th) * Mag[x_plus1, y] - th * Mag[x_plus1, y_minus1]; } else { v1 = (1 + th) * Mag[x_minus1, y] - th * Mag[x_minus1, y_plus1]; v2 = (1 + th) * Mag[x_plus1, y] - th * Mag[x_plus1, y_minus1]; } } if (Mag[x, y] > v1 && Mag[x, y] > v2) { tmp[x, y] = Mag[x, y]; } else { tmp[x, y] = 0; } } } Mag = tmp; }
static void ToComplex(Complex[,] data, GrayscaleFloatImage img) { for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { data[i, j] = new Complex(img[i, j], 0); } } }
private static GrayscaleFloatImage add(GrayscaleFloatImage image, GrayscaleFloatImage image2) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { image[x, y] = Math.Abs(image[x, y]) > Math.Abs(image2[x, y]) ? image[x, y] : image2[x, y]; } } return(image); }
static void ToImg(Complex[,] data, GrayscaleFloatImage img) { img = new GrayscaleFloatImage(data.GetLength(0), data.GetLength(1)); for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) { img[i, j] = (float)data[i, j].Re; } } }
public GrayscaleFloatImage ToGrayscaleFloatImage() { //res[i, j] = 0.114f * b + 0.587f * g + 0.299f * r GrayscaleFloatImage res = new GrayscaleFloatImage(Width, Height); for (int i = 0; i < res.rawdata.Length; i++) { res.rawdata[i] = 0.114f * rawdata[i].b + 0.587f * rawdata[i].g + 0.299f * rawdata[i].r; } return(res); }
static void FlipImage(GrayscaleFloatImage image) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width / 2; x++) { float p = image[x, y]; image[x, y] = image[image.Width - 1 - x, y]; image[image.Width - 1 - x, y] = p; } } }
public static GrayscaleFloatImage RGB2Gray(ColorFloatImage image) { GrayscaleFloatImage gray = new GrayscaleFloatImage(image.Width, image.Height); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { gray[x, y] = RGB2GrayPix(image[x, y]); } } return(gray); }
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); }
private static GrayscaleFloatImage ToGrayscaleImage(ColorFloatImage image) { GrayscaleFloatImage tmp = new GrayscaleFloatImage(image.Width, image.Height); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { tmp[x, y] = image[x, y].b * 0.114f + image[x, y].g * 0.587f + image[x, y].r * 0.299f; } } return(tmp); }
private static float Conv(GrayscaleFloatImage image, int x, int y, float[,] matr, float normValue) { float tempPixel = image[x, y]; int n = matr.GetLength(0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { tempPixel += matr[j, i] * 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))]; } } tempPixel = tempPixel / normValue; return(tempPixel); }
private static float harrisConver(GrayscaleFloatImage image, int x, int y, float[,] matr, float normValue) { float Res = 0; int n = matr.GetLength(0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { Res += 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))]; } } Res = Res / normValue; return(Res); }
static public GrayscaleFloatImage Canny(ColorFloatImage image, float sigma, float thigh, float tlow) { thigh *= 255; tlow *= 255; GrayscaleFloatImage EdgeMap = new GrayscaleFloatImage(image.Width, image.Height); image = ImageProcessing.Gauss(image, sigma); GrayscaleFloatImage GradientMap = ToGrayscaleImage(image); GrayscaleFloatImage Dir = ToGrayscaleImage(image); GradientSobel(ref GradientMap); Susperss_NonMaxima(ref GradientMap, Dir); EdgeDetect(GradientMap, thigh, tlow, EdgeMap); // Result = Mag; return(EdgeMap); }
public static void GradientSobel(ref GrayscaleFloatImage image) { GrayscaleFloatImage tempImg = new GrayscaleFloatImage(image.Width, image.Height); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { float dx = SobelX(image, x, y), dy = SobelY(image, x, y); tempImg[x, y] = (float)Math.Sqrt(dx * dx + dy * dy); //tempImg[x, y] = Math.Abs(dx) + Math.Abs(dy); } } image = tempImg; }
static void Main() { PointPairList us = new PointPairList(), sift = new PointPairList(); GrayscaleFloatImage img = ImageIO.FileToGrayscaleFloatImage("lena_min.bmp"); var a = Count.lpc("lena", img, 2, 0, 0, 0, true); /* * Console.WriteLine("начало"); * // Count.CountMetric("lena_min"); * // Count.CountMetric("lena_min_rot"); * int corresp = Compare.CountCorrespondenses("lena_min", "lena_min_rot"); * * for (float th = 0.0f; th < 10; th += 0.1f) * Compare.Compare_metrik("lena_min", "lena_min_rot", 0, corresp, th, us, 1); * * for (float th = 0; th < 1000; th += 100) * Compare.Compare_metrik("lena_min", "lena_min_rot", 1, corresp, th, sift); * * Extra.DrawGpaph(us, sift, "ans_quant.bmp");*/ }
public static void Draw_Comparasion(GrayscaleFloatImage img1, GrayscaleFloatImage img2, List <float> x1, List <float> y1, List <float> x2, List <float> y2, string name) { int delta = 10; Pen redPen = new Pen(Color.Red, 1); Pen greenPen = new Pen(Color.Green, 1); int h = Math.Max(img1.Height, img2.Height); Bitmap map = new Bitmap(img1.Width + delta + img2.Width, h); Graphics gr = Graphics.FromImage(map); for (int i = 0; i < img1.Width; i++) { for (int j = 0; j < img1.Height; j++) { int col = (int)(img1[i, j]); map.SetPixel(i, j, Color.FromArgb(col, col, col)); } } for (int i = 0; i < img2.Width; i++) { for (int j = 0; j < img2.Height; j++) { int col = (int)(img2[i, j]); map.SetPixel(img1.Width + delta + i, j, Color.FromArgb(col, col, col)); } } for (int i = 0; i < x1.Count; i++) { // if (Math.Abs(x1[i]* 0.9 - x2[i]) < 3 && Math.Abs(y1[i]* 0.9 - y2[i]) < 3) if (Math.Abs(x2[i] - (256 - y1[i])) < 4 && Math.Abs(y2[i] - x1[i]) < 4) { gr.DrawLine(greenPen, (int)x1[i], (int)y1[i], img1.Width + delta + (int)x2[i], (int)y2[i]); } else { gr.DrawLine(redPen, (int)x1[i], (int)y1[i], img1.Width + delta + (int)x2[i], (int)y2[i]); } } map.Save(name); }
public static List <GrayscaleFloatImage> MagnitudeAndDirections(ColorFloatImage image, ConvolutionKernel xKernel, ConvolutionKernel yKernel) { var xWindow = xKernel.Kernel; var yWindow = yKernel.Kernel; GrayscaleFloatImage magn = new GrayscaleFloatImage(image.Width, image.Height); GrayscaleFloatImage angles = new GrayscaleFloatImage(image.Width, image.Height); int windowSide = (int)Math.Pow(xWindow.Length, 0.5); int halfWindowSide = (windowSide - 1) / 2; for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { List <int> i = NeighbourIndexes(x, image.Width - 1, halfWindowSide, FillMode.Reflection); List <int> j = NeighbourIndexes(y, image.Height - 1, halfWindowSide, FillMode.Reflection); List <float> pix = new List <float>(); for (int k = 0; k < windowSide; k++) { for (int n = 0; n < windowSide; n++) { pix.Add(RGB2GrayPix(image[i[n], j[k]])); } } float xPix = ConvolveGray(xWindow, pix, xKernel.Sum); float yPix = ConvolveGray(yWindow, pix, yKernel.Sum); magn[x, y] = (float)Math.Sqrt(Math.Pow(xPix, 2) + Math.Pow(yPix, 2)); angles[x, y] = FitAngleInBin(Math.Abs(Math.Atan2(yPix, xPix))); } } return(new List <GrayscaleFloatImage>() { magn, angles }); }
public static Complex Convolution(GrayscaleFloatImage image, Complex[,] filter, int x, int y) { Complex result = new Complex(0, 0); int pix = 0; Complex koef = new Complex(0, 0); int r = filter.GetLength(0) / 2; for (int i = 0; i < filter.GetLength(0); i++) { for (int j = 0; j < filter.GetLength(1); j++) { Complex deltaResult = image[x + i - r, y + j - r] * filter[i, j]; result += deltaResult; koef += filter[i, j]; pix++; } } Complex filteredValue = result; return(filteredValue); }
public static GrayscaleFloatImage Hysteresis(ImageEdges edges, float tMin) { var magnitude = edges.Image; GrayscaleFloatImage dest = new GrayscaleFloatImage(magnitude.Width, magnitude.Height); float maxBrightness = 256; var queue = new Queue <Point>(edges.Points); while (queue.Count > 0) { var currentPoint = queue.Dequeue(); var x = currentPoint.X; var y = currentPoint.Y; dest[x, y] = maxBrightness; var xList = Enumerable.Range(x - 1, 3).ToList(); var yList = Enumerable.Range(y - 1, 3).ToList(); var points = xList.SelectMany(i => yList.Select(j => new Point() { X = i, Y = j })) .Where(p => p.X >= 0 && p.X <= magnitude.Width - 1 && p.Y >= 0 && p.Y <= magnitude.Height - 1).ToList(); foreach (var point in points) { if (dest[point.X, point.Y] == 0 && magnitude[point.X, point.Y] >= tMin) { queue.Enqueue(point); } } } return(dest); }
public static GrayscaleFloatImage harris(GrayscaleFloatImage image, float sigma) { int n = (int)(6 * Math.Abs(sigma) + 1); n = n == 1 || n % 2 == 0 ? 3 : n; float[,] matrX = new float[n, n], matrY = new float[n, n], matrXY = new float[n, n]; float normValueX = 0, normValueY = 0, normValueXY = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { matrXY[i, j] = ((i - (n - 1) / 2) * (j - (n - 1) / 2) / (sigma * sigma * sigma * sigma)) * (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma)); normValueXY += Math.Abs(matrXY[i, j]); matrY[i, j] = (-(j - (n - 1) / 2) / (sigma * sigma)) * (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma)); normValueY += Math.Abs(matrY[i, j]); matrX[i, j] = (-(i - (n - 1) / 2) / (sigma * sigma)) * (float)Math.Exp(-((i - (n - 1) / 2) * (i - (n - 1) / 2) + (j - (n - 1) / 2) * (j - (n - 1) / 2)) / (2 * sigma * sigma)); normValueX += Math.Abs(matrX[i, j]); } } GrayscaleFloatImage Result = new GrayscaleFloatImage(image.Width, image.Height); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { //Result[x, y] = image[x, y]; float[,] A = new float[2, 2]; A[0, 0] = harrisConver(image, x, y, matrX, normValueX); A[0, 0] = A[0, 0] * A[0, 0]; A[1, 1] = harrisConver(image, x, y, matrY, normValueY); A[1, 1] = A[1, 1] * A[1, 1]; A[0, 1] = harrisConver(image, x, y, matrXY, normValueXY); A[1, 0] = A[0, 1]; float k = 0.05f; double detA = A[0, 0] * A[1, 1] - A[0, 1] * A[1, 0], trA = A[0, 0] + A[1, 1]; double R = detA - k * trA * trA; if (R > 10000) { Result[x, y] = 255; } /*double lambda1 = (trA + Math.Sqrt(trA * trA - 4 * detA))/2, * lambda2 = (trA - Math.Sqrt(trA * trA - 4 * detA))/2; * if (lambda1 > 128 && lambda2 > 128) Result[x, y] = 255; */ } } Susperss_NonMaxima(ref Result, image); for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { Result[x, y] += image[x, y]; } } return(Result); }