/// <summary> /// assume that the input matrix is purely binary, i.e. zeros and ones /// </summary> /// <param name="matrix"> /// </param> /// <returns> /// The <see cref="ArrayList"/>. /// </returns> public static ArrayList ShapesDetect(double[,] matrix) { var shapes = new ArrayList(); var random = new RandomNumber(); int mHeight = matrix.GetLength(0); int mWidth = matrix.GetLength(1); for (int x = 5; x < mWidth; x++) { for (int y = 0; y < mHeight - 1; y++) { if (matrix[y, x] != 1.0) { continue; // not in an object } if (matrix[y + 1, x] != 1.0) { y++; continue; // shape must be > 2 pixels wide } // explore shape in y dimension int rowWidth = 0; while (rowWidth + y < mHeight && matrix[y + rowWidth, x] == 1.0) { rowWidth++; } rowWidth--; // back off one place int yCentre = y + (rowWidth / 2); // position in centre of shape if (InExistingShape(yCentre, x, shapes)) { continue; } // explore shape in x dimension int upDist = 0; while (x + upDist < mWidth && matrix[yCentre, x + upDist] == 1.0) { upDist++; } if (matrix[yCentre, x + 1] == 0.0) { upDist = 1; } int dnDist = 0; while (x - dnDist > 0 && matrix[yCentre, x - dnDist] == 1.0) { dnDist++; } dnDist--; // pull back one // initialise possible shape. int col1 = x - dnDist + 1; int colWidth = upDist + dnDist - 2; var shape = new Oblong(y, col1, y + rowWidth - 1, col1 + colWidth - 1); shape.RandomNumber = random.GetInt(200); // set random number for id and color purposes int[] centroid = shape.Centroid(); // LoggedConsole.WriteLine("Centroid=" + centroid[0] + ", " + centroid[1]); // LoggedConsole.WriteLine("RowWidth=" + shape.RowWidth + " ColWidth=" + shape.ColWidth); shapes.Add(shape); // more to end of shape y = shape.RowBottom; } x += 4; // jump through 5 lines at a time. } LoggedConsole.WriteLine("Number of shapes=" + shapes.Count); shapes = ProcessShapes(shapes); // Console.ReadLine(); return(shapes); }
public RandomVariable(double mean, double SD, int seed) { this.mean = mean; this.SD = SD; this.R = new RandomNumber(seed); }
/// <summary> /// CONSTRUCTOR /// </summary> public RandomVariable() { this.R = new RandomNumber(); }
/// <summary> /// CONSTRUCTOR /// </summary> /// <param name="mean"></param> /// <param name="SD"></param> public RandomVariable(double mean, double SD) { this.mean = mean; this.SD = SD; this.R = new RandomNumber(); }
/// <summary> /// Runs a test of the ScanArrayForGratingPattern() method. /// First constructs a grating signal, then embeds it in longer noise signal /// The grating is defined by a period and the number of cycles. /// The search is repeated many iterations in order to get everage accuracy. /// Accuracy depends on relative levels of noise gain and signal gain i.e. the SNR. /// </summary> public static void Test_ScanArrayForGridPattern1() { int n = 500; double[] v = new double[n]; var rn = new RandomNumber(); int maxIterations = 1000; int count = 0; double[] template = { 1.0, 0.0, 1.1, 0.1, 1.2, 0.2, 1.3, 0.3 }; //double[] template = { 1.5, 0.2, 0.8, 0.1, 0.9, 0.0, 0.8, 0.0 }; //double[] template = { 2.0, 0.0, 0.7, 0.0, 0.6, 0.0, 0.7, 0.0 }; //double[] template = { 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; int numberOfCycles = 4; int cyclePeriod = 64; //MUST BE AN EVEN NUMBER!! int signalLength = numberOfCycles * cyclePeriod; double[] signal = template; if (cyclePeriod > 2) { int halfPeriod = cyclePeriod / 2; signal = new double[signalLength]; for (int x = 0; x < template.Length; x++) { for (int p = 0; p < halfPeriod; p++) { signal[(x * halfPeriod) + p] = template[x]; //transfer signal } if (cyclePeriod > 6) { signal = DataTools.filterMovingAverage(signal, halfPeriod - 1); } signal = DataTools.normalise(signal); } //for } // if (cyclePeriod > 2) DataTools.writeBarGraph(signal); double bgNoiseGain = 0.1; double signalGain = 0.13; int locationOfSignalStart = 100; int searchStep = 5; int errorTolerance = cyclePeriod; if (errorTolerance < searchStep) { errorTolerance = searchStep + 1; } //run many repeats of the detection to determine its accuracy. Noise in signal means result varies from iteration to iteration. for (int iter = 0; iter < maxIterations; iter++) { //construct background signal. for (int i = 0; i < n; i++) { v[i] = rn.GetDouble() * bgNoiseGain; } //add in the signal for (int i = 0; i < signal.Length; i++) { v[locationOfSignalStart + i] += (signal[i] * signalGain); } //detect grating in signal var output = ScanArrayForGratingPattern(v, searchStep, numberOfCycles, cyclePeriod); int maxLocation = DataTools.GetMaxIndex(output); if ((maxLocation > (locationOfSignalStart - errorTolerance)) && (maxLocation < (locationOfSignalStart + errorTolerance))) { Console.WriteLine("score = {0:f2}", output[maxLocation]); count++; } } Console.WriteLine("% correct = {0:f1}", 100 * count / (double)maxIterations); }