public void Add( DataSet data ) { for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { this.volume[ i, j, k ] += data.volume[ i, j, k ]; } }
static void Main(string[] args) { Bitmap bmp = new Bitmap("test.bmp"); Console.WriteLine("Only Bitmap is loaded"); //Console.ReadKey(); DataSet orig = new DataSet( bmp); Console.WriteLine("Allocated Memory {0}", bmp.Width * bmp.Height * sizeof(short) / 1000000); //Console.ReadKey(); DataSet Gx = new DataSet( (short) bmp.Width, (short) bmp.Height, 1 ); DataSet Gy = new DataSet( (short) bmp.Width, (short) bmp.Height, 1 ); DataSet C = new DataSet( (short) bmp.Width, (short) bmp.Height, 1 ); Console.WriteLine( "Allocated Memory {0}", bmp.Width * bmp.Height * sizeof( short ) / 1000000 ); //Console.ReadKey(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // orig.Convolve1D_Vertikal( result ); orig.Convolve1D_VH( Gx, Gy ); C.Diffusivity( Gx, Gy ); // Gx = Cx = Gx .* C Gx.MultiplyBy( C ); //Gy.MultiplyBy( C ); // Gx = Gx.*C + Gy.C = Cx+Cy //Gx.Add( Gy ); stopWatch.Stop(); Console.WriteLine("{0}", stopWatch.ElapsedMilliseconds); //Console.ReadKey(); C.SaveAsBitmap( "C.bmp" ); Gx.SaveAsBitmap("Gx.bmp"); Gy.SaveAsBitmap("Gy.bmp"); }
public void Convolve1D_Horizontal( DataSet result ) { int kernelSize = 5; //double[] kernel1D = { 0.0833333333333333, -0.666666666666667, 0, 0.666666666666667, -0.0833333333333333 }; short[] kernel1D = { +1, -8, 0, 8, -1 }; short[] doubleIndexes = { -2, -1, 0, +1, +2 }; // check the odd size of kernel1D //for ( int i = kernelSize / 2 + 1; i < ( data.Width - ( kernelSize / 2 + 1 ) ) - 2; i++ ) for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { short sum = 0; for ( int m = 0; m < kernelSize; m++ ) { // try // { int indexes = i + doubleIndexes[ m ]; if ( indexes <= 0 ) indexes = Math.Abs( indexes ); if ( indexes >= Width ) indexes = Width - 1; //-( indexes - data.Width ); short a = volume[ indexes, j, k ]; short b = kernel1D[ m ]; sum += (short) ( a * b ); } // testing direct access result.volume[ i, j, k ] = (short) ( sum / 12 ); } }
public void MultiplyBy( DataSet data ) { for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { double temp = this.volume[ i, j, k ]; double temp2 = data.volume[ i, j, k ]; temp = temp * temp2; this.volume[ i, j, k ] = (short) ( temp); } }
public void Diffusivity( DataSet Gx, DataSet Gy ) { // C = diffusivity(sqrt(Gx.^2 + Gy.^2)); // function R = diffusivity(value) // K = 0.05; //% R = 1 ./ (1+(abs(value)./K).^2); // R = exp(-(abs(value)./K).^2 ); // end double K = 0.05; for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { double gx = (double) Gx.volume[ i, j, k ] / scale; double gy = (double) Gy.volume[ i, j, k ] / scale; double c = Math.Sqrt( gx * gx + gy * gy ); c = Math.Abs( c ); this.volume[ i, j, k ] = (short) ( scale * Math.Exp( -c * c / ( K * K ) ) ); //this.volume[ i, j, k ] = (short) ( c * scale ); } }
//public void ScaleMax() //{ // short max = GetMaximum(); // short min = GetMinimum(); // for ( int i = 0; i < Width; i++ ) // for ( int j = 0; j < Height; j++ ) // this.volume[ i, j, 0 ] /= max; //} public void Convolve1D_VH( DataSet Gx, DataSet Gy ) { int kernelSize = 5; short[] kernel1D = { +1, -8, 0, 8, -1 }; short[] doubleIndexes = { -2, -1, 0, +1, +2 }; for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { short sumY = 0; short sumX = 0; for ( int m = 0; m < kernelSize; m++ ) { // Vertikal int indexesV = j + doubleIndexes[ m ]; if ( indexesV <= 0 ) indexesV = Math.Abs( indexesV ); if ( indexesV >= Height ) indexesV = Height - 1; short a = volume[ i, indexesV, k ]; short b = kernel1D[ m ]; sumY += (short) ( a * b ); // Horizontal int indexesH = i + doubleIndexes[ m ]; if ( indexesH <= 0 ) indexesH = Math.Abs( indexesH ); if ( indexesH >= Width ) indexesH = Width - 1; //-( indexes - data.Width ); a = volume[ indexesH, j, k ]; //b = kernel1D[ m ]; sumX += (short) ( a * b ); } Gy.volume[ i, j, k ] = (short) sumY; Gx.volume[ i, j, k ] = (short) sumX; } }
public void Convolve1D_Vertikal( DataSet result ) { int kernelSize = 5; short[] kernel1D = { +1, -8, 0, 8, -1 }; short[] doubleIndexes = { -2, -1, 0, +1, +2 }; for ( int k = 0; k < Depth; k++ ) for ( int i = 0; i < Width; i++ ) for ( int j = 0; j < Height; j++ ) { short sum = 0; for ( int m = 0; m < kernelSize; m++ ) { // try // { int indexes = j + doubleIndexes[ m ]; if ( indexes <= 0 ) indexes = Math.Abs( indexes ); if ( indexes >= Height ) indexes = Height - 1; //-( indexes - data.Width ); short a = volume[ i, indexes, k ]; short b = kernel1D[ m ]; sum += (short) ( a * b ); } // testing direct access result.volume[ i, j, k ] = (short) ( sum / 12 ); } }