I'm trying to create code that reads a text file (containing (double
) numbers between 0 and 1) and filling up a 3D array/matrix (calling it matrix from now on) with those values. After that is done I need to figure out the current max-value inside this matrix and then look at the values around it (in every possible direction) to check if those values are > than a set threshold. Since you can't get the index of the maxvalue
-element in a 3D matrix I'm checking this by using 3 for-loops to find the coordinates of the maxvalue
-element.
After having found the current maxvalue
I set the element containing it to 0 and get a new maxvalue
after everything else is done. The whole process is being repeated until the BubbleFrame
Matrix only consists of values that are < Threshold
.
Now this code is working, but it's super slow. I used it in Matlab and it only took about 1:30h to find everything (27370 matches aka bubbles) compared to C# where I stopped the code at 6137 bubbles after about 2 hours.
How do I improve my code to improve the operation time?
class Blasensuche
{
public static Int32 BlasenSuche(ref int Sensor, ref int n_fr, ref string BubbleFile, ref double Threshold)
{
bool SearchDone = false;
//double Threshold = 0.12;
double[,,] BubbleFrame = new double[Sensor,Sensor,n_fr];
int[,,] BubbleCollection = new int[Sensor, Sensor, n_fr];
int BubbleCounter = 0;
//int BubbleNumber = 0;
double Maxvalue = 0;
//int Index = 0;
int CPlus1 = 0;
int CMinus1 = 0;
using (StreamReader BubbleReader = new StreamReader(BubbleFile))
{
string calibline = "";
string[] numbers;
do
{
for (int c = 0; c < n_fr; c++)
{
for (int b = 0; b < Sensor; b++)
{
calibline= BubbleReader.ReadLine();
numbers = calibline.Split(' ');
for (int a = 0; a < Sensor; a++)
{
BubbleFrame[b, a, c] = Convert.ToDouble(numbers[a]);
}
}
}
} while (!BubbleReader.EndOfStream);
}
while (SearchDone == false)
{
Maxvalue = BubbleFrame.Cast<double>().Max();
if (Maxvalue < Threshold)
{
SearchDone = true;
break;
}
BubbleCounter++;
//Blasenanzahl.SetLabel(BubbleCounter);
for (int c = 0; c < n_fr; c++)
{
for (int b = 0; b < Sensor; b++)
{
for (int a = 0; a < Sensor; a++)
{
if (BubbleFrame[b, a, c] == Maxvalue)
{
BubbleFrame[b, a, c] = 0D;
BubbleCollection[b, a, c] = BubbleCounter;
if (c > 0)
{
CMinus1 = c - 1;
BubbleSearchCMinus1(ref BubbleFrame, ref BubbleCollection, ref CMinus1, ref b, ref a, ref Threshold, ref BubbleCounter, ref Sensor);
}
BubbleSearchC(ref BubbleFrame, ref BubbleCollection, ref c, ref b, ref a, ref Threshold, ref BubbleCounter, ref Sensor);
CPlus1 = c + 1;
if (CPlus1 < n_fr)
{
BubbleSearchCPlus1(ref BubbleFrame, ref BubbleCollection, ref CPlus1, ref b, ref a, ref Threshold, ref BubbleCounter, ref Sensor, ref n_fr);
}
}
}
}
}
//Index = Array.IndexOf(BubbleFrame, Maxvalue);
}
return BubbleCounter;
}
public static void BubbleSearchCMinus1(ref double[, ,] BubbleFrame,ref int[, ,] BubbleCollection, ref int CMinus1, ref int b, ref int a, ref double Threshold, ref int BubbleCounter, ref int Sensor)
{
//C-1 Ebene
int BPlus1 = b + 1; int BMinus1 = b - 1;
int APlus1 = a + 1; int AMinus1 = a - 1;
if (b > 0 && a > 0 && BubbleFrame[BMinus1, AMinus1, CMinus1] > Threshold) //1
{
BubbleFrame[BMinus1, AMinus1, CMinus1] = 0D;
BubbleCollection[BMinus1, AMinus1, CMinus1] = BubbleCounter;
}
if (b > 0 && BubbleFrame[BMinus1, a, CMinus1] > Threshold) //2
{
BubbleFrame[BMinus1, a, CMinus1] = 0D;
BubbleCollection[BMinus1, a, CMinus1] = BubbleCounter;
}
if (b > 0 && (APlus1) < Sensor && BubbleFrame[BMinus1, APlus1, CMinus1] > Threshold)//3
{
BubbleFrame[BMinus1, APlus1, CMinus1] = 0D;
BubbleCollection[BMinus1, APlus1, CMinus1] = BubbleCounter;
}
if (a > 0 && BubbleFrame[b, AMinus1, CMinus1] > Threshold)//4
{
BubbleFrame[b, AMinus1, CMinus1] = 0D;
BubbleCollection[b, AMinus1, CMinus1] = BubbleCounter;
}
if (BubbleFrame[b, a, CMinus1] > Threshold)//5
{
BubbleFrame[b, a, CMinus1] = 0D;
BubbleCollection[b, a, CMinus1] = BubbleCounter;
}
if ((APlus1) < Sensor && BubbleFrame[b, APlus1, CMinus1] > Threshold)//6
{
BubbleFrame[b, APlus1, CMinus1] = 0D;
BubbleCollection[b, APlus1, CMinus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && a > 0 && BubbleFrame[BPlus1, AMinus1, CMinus1] > Threshold)//7
{
BubbleFrame[BPlus1, AMinus1, CMinus1] = 0D;
BubbleCollection[BPlus1, AMinus1, CMinus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && BubbleFrame[BPlus1, a, CMinus1] > Threshold)//8
{
BubbleFrame[BPlus1, a, CMinus1] = 0D;
BubbleCollection[BPlus1, a, CMinus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && (APlus1) < Sensor && BubbleFrame[BPlus1, APlus1, CMinus1] > Threshold)//9
{
BubbleFrame[BPlus1, APlus1, CMinus1] = 0D;
BubbleCollection[BPlus1, APlus1, CMinus1] = BubbleCounter;
}
}
public static void BubbleSearchC(ref double[, ,] BubbleFrame, ref int[, ,] BubbleCollection, ref int c, ref int b, ref int a, ref double Threshold, ref int BubbleCounter, ref int Sensor)
{
int BPlus1 = b + 1; int BMinus1 = b - 1;
int APlus1 = a + 1; int AMinus1 = a - 1;
//C Ebene
if (b > 0 && a > 0 && BubbleFrame[BMinus1, AMinus1, c] > Threshold) //1
{
BubbleFrame[BMinus1, AMinus1, c] = 0D;
BubbleCollection[BMinus1, AMinus1, c] = BubbleCounter;
}
if (b > 0 && BubbleFrame[BMinus1, a, c] > Threshold) //2
{
BubbleFrame[BMinus1, a, c] = 0D;
BubbleCollection[BMinus1, a, c] = BubbleCounter;
}
if (b > 0 && (APlus1) < Sensor && BubbleFrame[BMinus1, APlus1, c] > Threshold)//3
{
BubbleFrame[BMinus1, APlus1, c] = 0D;
BubbleCollection[BMinus1, APlus1, c] = BubbleCounter;
}
if (a > 0 && BubbleFrame[b, AMinus1, c] > Threshold)//4
{
BubbleFrame[b, AMinus1, c] = 0D;
BubbleCollection[b, AMinus1, c] = BubbleCounter;
}
/*if (BubbleFrame[b, a, c] > Threshold)//5 Entfällt!
{
BubbleFrame[b, a, c] = 0D;
BubbleCollection[b, a, c] = BubbleCounter;
}*/
if ((APlus1) < Sensor && BubbleFrame[b, APlus1, c] > Threshold)//6
{
BubbleFrame[b, APlus1, c] = 0D;
BubbleCollection[b, APlus1, c] = BubbleCounter;
}
if ((BPlus1) < Sensor && a > 0 && BubbleFrame[BPlus1, AMinus1, c] > Threshold)//7
{
BubbleFrame[BPlus1, AMinus1, c] = 0D;
BubbleCollection[BPlus1, AMinus1, c] = BubbleCounter;
}
if ((BPlus1) < Sensor && BubbleFrame[BPlus1, a, c] > Threshold)//8
{
BubbleFrame[BPlus1, a, c] = 0D;
BubbleCollection[BPlus1, a, c] = BubbleCounter;
}
if ((BPlus1) < Sensor && (APlus1) < Sensor && BubbleFrame[BPlus1, APlus1, c] > Threshold)//9
{
BubbleFrame[BPlus1, APlus1, c] = 0D;
BubbleCollection[BPlus1, APlus1, c] = BubbleCounter;
}
}
public static void BubbleSearchCPlus1(ref double[, ,] BubbleFrame, ref int[, ,] BubbleCollection, ref int CPlus1, ref int b, ref int a, ref double Threshold, ref int BubbleCounter, ref int Sensor, ref int n_fr)
{
//C+1 Ebene
int BPlus1 = b + 1; int BMinus1 = b - 1;
int APlus1 = a + 1; int AMinus1 = a - 1;
if (b > 0 && a > 0 && BubbleFrame[BMinus1, AMinus1, CPlus1] > Threshold) //1
{
BubbleFrame[BMinus1, AMinus1, CPlus1] = 0D;
BubbleCollection[BMinus1, AMinus1, CPlus1] = BubbleCounter;
}
if (b > 0 && BubbleFrame[BMinus1, a, CPlus1] > Threshold) //2
{
BubbleFrame[BMinus1, a, CPlus1] = 0D;
BubbleCollection[BMinus1, a, CPlus1] = BubbleCounter;
}
if (b > 0 && (APlus1) < Sensor && BubbleFrame[BMinus1, APlus1, CPlus1] > Threshold)//3
{
BubbleFrame[BMinus1, APlus1, CPlus1] = 0D;
BubbleCollection[BMinus1, APlus1, CPlus1] = BubbleCounter;
}
if (a > 0 && BubbleFrame[b, AMinus1, CPlus1] > Threshold)//4
{
BubbleFrame[b, AMinus1, CPlus1] = 0D;
BubbleCollection[b, AMinus1, CPlus1] = BubbleCounter;
}
if (BubbleFrame[b, a, CPlus1] > Threshold)//5
{
BubbleFrame[b, a, CPlus1] = 0D;
BubbleCollection[b, a, CPlus1] = BubbleCounter;
}
if ((APlus1) < Sensor && BubbleFrame[b, APlus1, CPlus1] > Threshold)//6
{
BubbleFrame[b, APlus1, CPlus1] = 0D;
BubbleCollection[b, APlus1, CPlus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && a > 0 && BubbleFrame[BPlus1, AMinus1, CPlus1] > Threshold)//7
{
BubbleFrame[BPlus1, AMinus1, CPlus1] = 0D;
BubbleCollection[BPlus1, AMinus1, CPlus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && BubbleFrame[BPlus1, a, CPlus1] > Threshold)//8
{
BubbleFrame[BPlus1, a, CPlus1] = 0D;
BubbleCollection[BPlus1, a, CPlus1] = BubbleCounter;
}
if ((BPlus1) < Sensor && (APlus1) < Sensor && BubbleFrame[BPlus1, APlus1, CPlus1] > Threshold)//9
{
BubbleFrame[BPlus1, APlus1, CPlus1] = 0D;
BubbleCollection[BPlus1, APlus1, CPlus1] = BubbleCounter;
}
}
}
n_fr
, is that a bug? – mjolka Jul 7 at 12:49