public Image Apply(Image input) { int slice_width = input.Width / X_Slices; int slice_height = input.Height / Y_Slices; Image[,] slices = new Image[X_Slices, Y_Slices]; Rect[,] regions = new Rect[X_Slices, Y_Slices]; for (int i = 0; i < X_Slices; i++) { for (int j = 0; j < Y_Slices; j++) { bool border = false; if (j == Y_Slices - 1) { border = true; } if (i == X_Slices - 1) { border = true; } int cutting_width = 0; int cutting_height = 0; int XStart = 0, YStart = 0, XEnd = 0, YEnd = 0; if (border) { XStart = i * slice_width; YStart = j * slice_height; XEnd = (i + 1) * slice_width; YEnd = (j + 1) * slice_height; if (j == Y_Slices - 1) { cutting_height = input.Height - ((Y_Slices - 1) * slice_height); YEnd = input.Height; } if (i == X_Slices - 1) { cutting_width = input.Width - ((X_Slices - 1) * slice_width); XEnd = input.Width; } } else { cutting_height = slice_height; cutting_width = slice_width; XStart = i * slice_width; YStart = j * slice_height; XEnd = (i + 1) * slice_width; YEnd = (j + 1) * slice_height; } slices[i, j] = new Image(XEnd - XStart, YEnd - YStart, input.Channels); regions[i, j] = new Rect(XStart, YStart, XEnd, YEnd); int si = 0, sj = 0; for (int x = XStart; x < XEnd; x++) { for (int y = YStart; y < YEnd; y++) { slices[i, j][si, sj] = input[x, y]; sj++; } si++; sj = 0; } } } double rT = Utils.NormalRand(25, 10); IOperation operation = new AutoThreshold((int)rT, 25); for (int i = 0; i < slices.GetLength(0); i++) { for (int j = 0; j < slices.GetLength(1); j++) { slices[i, j] = operation.Apply(slices[i, j]); } } Image result = new Image(input.Width, input.Height, input.Channels); //int res_i = 0, res_j = 0; for (int i = 0; i < slices.GetLength(0); i++) { for (int j = 0; j < slices.GetLength(1); j++) { Image slice = slices[i, j]; Rect r = regions[i, j]; //res_i = r.X; //res_j = r.Y; for (int si = 0, res_i = r.X; si < slice.Width && res_i < r.Width; si++, res_i++) { for (int sj = 0, res_j = r.Y; sj < slice.Height && res_j < r.Height; sj++, res_j++) { Pixel p = slice[si, sj]; result[res_i, res_j] = p; } } } } return(result); }
/// <summary> /// Auto Threshold algorithm. Here we create a new window from where we implement the algorithm. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void autoThreshold_Click(object sender, RoutedEventArgs e) { if (m_data.M_inputFilename == string.Empty || m_data.M_bitmap == null) { MessageBox.Show("Open image first!", "ArgumentsNull", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { AutoThreshold autoThresholdWindow = new AutoThreshold(m_data, m_vm); autoThresholdWindow.Owner = this; autoThresholdWindow.Show(); } catch (FileNotFoundException ex) { MessageBox.Show(ex.Message, "FileNotFoundException", MessageBoxButton.OK, MessageBoxImage.Error); } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "ArgumentException", MessageBoxButton.OK, MessageBoxImage.Error); } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message, "InvalidOperationException", MessageBoxButton.OK, MessageBoxImage.Error); } catch (IndexOutOfRangeException ex) { MessageBox.Show(ex.Message, "IndexOutOfRangeException", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } }
public Image Apply(Image input) { int slice_width = input.Width/X_Slices; int slice_height = input.Height/Y_Slices; Image[,] slices = new Image[X_Slices,Y_Slices]; Rect[,] regions = new Rect[X_Slices, Y_Slices]; for (int i = 0; i < X_Slices; i++) { for (int j = 0; j < Y_Slices; j++) { bool border = false; if (j == Y_Slices - 1) border = true; if (i == X_Slices - 1) border = true; int cutting_width = 0; int cutting_height = 0; int XStart = 0, YStart = 0, XEnd = 0, YEnd = 0; if (border) { XStart = i * slice_width; YStart = j * slice_height; XEnd = (i + 1) * slice_width; YEnd = (j + 1) * slice_height; if (j == Y_Slices - 1) { cutting_height = input.Height - ((Y_Slices - 1)*slice_height); YEnd = input.Height; } if (i == X_Slices - 1) { cutting_width = input.Width - ((X_Slices - 1) * slice_width); XEnd = input.Width; } } else { cutting_height = slice_height; cutting_width = slice_width; XStart = i*slice_width; YStart = j*slice_height; XEnd = (i + 1)*slice_width; YEnd = (j + 1)*slice_height; } slices[i,j] = new Image(XEnd - XStart,YEnd-YStart,input.Channels); regions[i,j] = new Rect(XStart, YStart, XEnd, YEnd); int si = 0, sj = 0; for (int x = XStart; x < XEnd; x++) { for (int y = YStart; y < YEnd; y++) { slices[i, j][si, sj] = input[x, y]; sj++; } si++; sj = 0; } } } double rT = Utils.NormalRand(25, 10); IOperation operation = new AutoThreshold((int)rT,25); for (int i = 0; i < slices.GetLength(0); i++) { for (int j = 0; j < slices.GetLength(1); j++) { slices[i, j] = operation.Apply(slices[i, j]); } } Image result = new Image(input.Width, input.Height, input.Channels); //int res_i = 0, res_j = 0; for (int i = 0; i < slices.GetLength(0); i++) { for (int j = 0; j < slices.GetLength(1); j++) { Image slice = slices[i, j]; Rect r = regions[i, j]; //res_i = r.X; //res_j = r.Y; for (int si = 0, res_i = r.X; si < slice.Width && res_i < r.Width; si++, res_i++) { for (int sj = 0, res_j = r.Y; sj < slice.Height && res_j < r.Height; sj++, res_j++) { Pixel p = slice[si, sj]; result[res_i, res_j] = p; } } } } return result; }