private float[] GetGrad(Models.Image image, int line_x, int line_y, int rows_AVG, int line_length) { //throw new NotImplementedException(); float[] averaged_grad = new float[Convert.ToInt32(image_cnv.Width)]; for (int i = line_x; i < line_x + line_length; i++) { float sumDiff = 0; int count = 0; for (int j = Convert.ToInt32(line_y - rows_AVG / 2); j < Convert.ToInt32(line_y + rows_AVG / 2); j++) { sumDiff += Math.Abs(image.pixels[j][i + 1] - image.pixels[j][i]); count++; } averaged_grad[Convert.ToInt32(profile_ln.Margin.Left) + i - line_x] = sumDiff / count; } return(averaged_grad); }
private float[] GetProf(Models.Image image, int line_x, int line_y, int rows_AVG, int line_length) { //throw new NotImplementedException(); float[] averaged_row = new float[Convert.ToInt32(image_cnv.Width)]; for (int i = line_x; i < line_x + line_length; i++) { //loop through and get the average based on the thickness of the line. float sum = 0; int count = 0; for (int j = Convert.ToInt32(line_y - rows_AVG / 2); j < Convert.ToInt32(line_y + rows_AVG / 2); j++) { sum += image.pixels[j][i]; count++; } averaged_row[Convert.ToInt32(profile_ln.Margin.Left) + i - line_x] = sum / count; } return(averaged_row); }
public static BitmapSource DrawImage(Models.Image image) { //PD_AdvAnalysis.WLAnalysis wla = new PD_AdvAnalysis.WLAnalysis(); //System.Windows.Controls.Image imag = wla.field_img; //System.Windows.Application.Current.MainWindow.FindName("field_img") as System.Windows.Controls.Image; WL_AutoDetectTest.MainWindow mw = new WL_AutoDetectTest.MainWindow(); System.Windows.Controls.Image imag = mw.image_im; int w = Convert.ToInt32(imag.Width); int h = Convert.ToInt32(imag.Height); double[] image_pixels = new double[w * h]; int index_out = 0; //this does a lot of the scaling. It insures the size of the image is the same as the size of the canvas. for (int j = (image.sizeY + h) / 2; j > (image.sizeY - h) / 2; j--)//I'm not sure why this has to be backward from the PD analysis code { int index_in = 0; for (int k = (image.sizeX - w) / 2; k < (image.sizeX + w) / 2; k++) { image_pixels[index_in + index_out * w] = image.pixels[j][k]; index_in++; } //index_out+=zoom_level; index_out++; //no zoom implemented in this code. } double image_max = image_pixels.Max(); double image_min = image_pixels.Min(); //byte[] image_bytes = new byte[image_pixels.Length * sizeof(UInt16)]; //Buffer.BlockCopy(image_pixels, 0, image_bytes, 0, image_bytes.Length); PixelFormat format = PixelFormats.Rgb24; //get min and max of the data. //iamge min now comes from the method input parameter image_min //image_min = image_pixels.Min();//this is the red value. I.e. 255,0,0 //image_max = image_pixels.Max();//this is the blue value. i.e. 0,0,255. double image_med = (image_max + image_min) / 2; int stride = (w * format.BitsPerPixel + 7) / 8; byte[] image_bytes = new byte[stride * h]; //copy data to image bytes for (int j = 0; j < image_pixels.Length; j++) { double value = image_pixels[j]; if (value < 0) { MessageBox.Show("hello"); } System.Windows.Media.Color c = new System.Windows.Media.Color(); if (value < image_min) { c.B = 0; c.R = 0; c.G = 0; } else if (value < image_med) { c.R = 0; c.B = Convert.ToByte(255 - (255 * (value - image_min) / (image_med - image_min))); c.G = Convert.ToByte(255 - (255 * (image_med - value) / (image_med - image_min))); } else if (value <= image_max) { c.B = 0; c.R = Convert.ToByte(255 - (255 * (image_max - value) / (image_max - image_med))); c.G = Convert.ToByte(255 - (255 * (value - image_med) / (image_max - image_med))); } else if (value > image_max) { c.R = 0; c.B = 0; c.G = 0; } image_bytes[j * 3] = c.R; image_bytes[j * 3 + 1] = c.G; image_bytes[j * 3 + 2] = c.B; } BitmapSource bmp = BitmapSource.Create( w, h, 10 * 2.54 / image.resX, 10 * 2.54 / image.resY, format, null, image_bytes, stride ); return(bmp); }
private void image_btn_Click(object sender, RoutedEventArgs e) { //global variable declaration int xSize = 0; int ySize = 0; int data_line; //load image here. OpenFileDialog ofd = new OpenFileDialog(); ofd.RestoreDirectory = true; ofd.Filter = "DXF Files (*.dxf)|*.dxf"; if (ofd.ShowDialog() == true) { bool line_count = false; bool xSizeb = false; bool ySizeb = false; //List<float[]> image = new List<float[]>(); Models.Image image = new Models.Image(); foreach (string line in File.ReadAllLines(ofd.FileName)) { if (line_count && xSizeb && ySizeb) { //this evaluates once you've found all 3. float[] row = new float[ySize]; //image.sizeX = xSize; image.sizeY = ySize; row = Array.ConvertAll(line.Split('\t'), float.Parse); image.pixels.Add(row); } else { if (line.Contains("Size1")) { //xSizeb = int.TryParse(line.Split('=').Last(), out xSize); image.sizeX = int.Parse(line.Split('=').Last()); xSizeb = true; } else if (line.Contains("Size2")) { //ySizeb = int.TryParse(line.Split('=').Last(), out ySize); image.sizeY = int.Parse(line.Split('=').Last()); ySizeb = true; } else if (line.Contains("Res1")) { image.resX = double.Parse(line.Split('=').Last()); } else if (line.Contains("Res2")) { image.resY = double.Parse(line.Split('=').Last()); } else if (line == "[Data]") { line_count = true; } } //line_count++; } //write image if (line_count && ySizeb && xSizeb) { //float[,] image_f = new float[ySize, xSize]; //for(int i =0; i<image.Count; i++) //{ // image_f.getrow = image[i]; //} BitmapSource bms = WL_AutoDetectTest.ImageDecon2.DrawImage(image); image_im.Source = bms; //get x and y position of the line. int line_x = Convert.ToInt32((image.sizeX - image_cnv.Width) / 2 + profile_ln.Margin.Left); int line_y = Convert.ToInt32((image.sizeY - image_cnv.Height) / 2 + profile_ln.Margin.Top); int rows_AVG = Convert.ToInt16(profile_ln.StrokeThickness);//some rounding issues may occur. int line_length = Convert.ToInt32(profile_ln.Width); //line orientation shoudl be here as well in order to allow the user to get vertical position. //get profile row. float[] profile = GetProf(image, line_x, line_y, rows_AVG, line_length); plotCanv1(profile, profile_cnv); float[] gradient = GetGrad(image, line_x, line_y, rows_AVG, line_length); plotCanv1(gradient, gradient_cnv); } } }