public void drawHorizontalLine(Map2d <Type> target, Vector2d <uint> start, uint width, Type value) { for (uint i = start.x; i < Math.Min(start.x + width, target.getSize().x - 1); i++) { target.write(new Vector2d <uint>(i, start.y), value); } }
public bool Init(string cellset) { string datapath = Path.Combine(EDDOptions.Instance.AppDataDirectory, "Maps"); if (Directory.Exists(datapath)) { galaxy = Map2d.LoadImage(Path.Combine(datapath, "Galaxy_L_Grid.json")); if (galaxy != null) { imageViewer.Image = new Bitmap(galaxy.FilePath); imageViewer.ZoomToFit(); imageViewer.Init(galaxy); comboBoxSelections.Items.AddRange((from x in defaultsel select x.Item1)); initialsel = Selection = cellset; initiallist = new List <int>(imageViewer.Selection); // copy of.. EDDiscovery.EDDTheme theme = EDDiscovery.EDDTheme.Instance; bool winborder = theme.ApplyToForm(this); statusStripCustom.Visible = panel_close.Visible = panel_minimize.Visible = !winborder; SetComboBox(); imageViewer.BackColor = Color.FromArgb(5, 5, 5); return(true); } } return(false); }
public static Map2d <float> convolution(Map2d <float> input, Map2d <float> kernel) { Map2d <float> resultMap = new Map2d <float>(input.getSize()); for (int y = (int)kernel.getSize().y / 2; y < input.getSize().y - kernel.getSize().y / 2; y++) { for (int x = (int)kernel.getSize().x / 2; x < input.getSize().x - kernel.getSize().x / 2; x++) { float sum = 0.0f; for (int kernelY = 0; kernelY < kernel.getSize().y; kernelY++) { for (int kernelX = 0; kernelX < kernel.getSize().x; kernelX++) { float kernelValue = kernel.read(new Vector2d <uint>((uint)kernelX, (uint)kernelY)); float inputValue = input.read(new Vector2d <uint>((uint)(x + kernelX - kernel.getSize().x / 2), (uint)(y + kernelY - kernel.getSize().y / 2))); sum += (kernelValue * inputValue); } } resultMap.write(new Vector2d <uint>((uint)x, (uint)y), sum); } } return(resultMap); }
public bool Init(string cellset) { string datapath = EDDOptions.Instance.MapsAppDirectory(); if (Directory.Exists(datapath)) { galaxy = EDDiscovery.Icons.IconMaps.StandardMaps().Find(x => x.FilePath.Contains("Galaxy L")); imageViewer.Image = galaxy.Image; imageViewer.ZoomToFit(); imageViewer.Init(galaxy); imageViewer.MinZoom = 1; comboBoxSelections.Items.AddRange((from x in DefaultGalaxyOptions select x.Item1)); initialsel = Selection = cellset; initiallist = new List <int>(imageViewer.Selection); // copy of.. var theme = ExtendedControls.Theme.Current; bool winborder = theme.ApplyDialog(this); statusStripCustom.Visible = panel_close.Visible = panel_minimize.Visible = !winborder; var enumlist = new Enum[] { EDTx.GalaxySectorSelect, EDTx.GalaxySectorSelect_buttonExtSet, EDTx.GalaxySectorSelect_labelSectorName }; BaseUtils.Translator.Instance.TranslateControls(this, enumlist, new Control[] { labelX, labelXName, labelZ, labelZName, labelID }); SetComboBox(); imageViewer.BackColor = Color.FromArgb(5, 5, 5); return(true); } return(false); }
public static void blurY(Map2d<float> input, Map2d<float> output, float[] kernel) { int x; int y; int radius = 1 + (kernel.Length - 1) / 2; for (x = 0; x < input.getWidth(); x++) { for (y = 0; y < input.getLength(); y++) { int ir; float temp; temp = 0.0f; for (ir = -radius; ir < radius - 1; ir++) { if (ir + y < 0 || ir + y >= input.getLength()) { continue; } temp += (input.readAt(x, y+ir) * kernel[radius + ir]); } output.writeAt(x, y, temp); } } }
public bool Init(string cellset) { string datapath = EDDOptions.Instance.MapsAppDirectory(); if (Directory.Exists(datapath)) { galaxy = Map2d.LoadImage(Path.Combine(datapath, "Galaxy_L_Grid.json")); if (galaxy != null) { imageViewer.Image = new Bitmap(galaxy.FilePath); imageViewer.ZoomToFit(); imageViewer.Init(galaxy); comboBoxSelections.Items.AddRange((from x in DefaultGalaxyOptions select x.Item1)); initialsel = Selection = cellset; initiallist = new List <int>(imageViewer.Selection); // copy of.. EDDiscovery.EDDTheme theme = EDDiscovery.EDDTheme.Instance; bool winborder = theme.ApplyDialog(this); statusStripCustom.Visible = panel_close.Visible = panel_minimize.Visible = !winborder; BaseUtils.Translator.Instance.Translate(this, new Control[] { labelX, labelXName, labelZ, labelZName, labelID }); SetComboBox(); imageViewer.BackColor = Color.FromArgb(5, 5, 5); return(true); } } return(false); }
private float multiplyKernelWithMapAtCenter(int centerX, int centerY, Map2d<float> map) { int ix; int iy; int relativeCenterX; int relativeCenterY; float result; result = 0.0f; if( centerX < kernelRadius || centerX + kernelRadius >= map.getWidth() || centerY < kernelRadius || centerY + kernelRadius >= map.getLength() ) { return 0.0f; } relativeCenterX = centerX - kernelRadius; relativeCenterY = centerY - kernelRadius; for( iy = 0; iy < kernelSize; iy++ ) { for( ix = 0; ix < kernelSize; ix++ ) { float readValue; readValue = map.readAt(ix + relativeCenterX, iy + relativeCenterY); result += (readValue * kernel[ix + iy*kernelSize]); } } return result; }
private void Display() { string str = toolStripComboExpo.SelectedItem.ToString(); Map2d map = images.FirstOrDefault(i => i.FileName == str); if (map != null) { try { imageViewer.Image?.Dispose(); imageViewer.Image = (Bitmap)map.Image.Clone(); // clone as we are going to draw on it. imageViewer.ZoomToFit(); currentimage = map; if (toolStripButtonStars.Checked) { DrawStars(); } DrawTravelHistory(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Loading 2dmap " + ex); imageViewer.Image = null; } imageViewer.Invalidate(); } }
private void drawCachedCircle(Map2d <Type> target, int index, Vector2d <uint> center, Type value) { // TODO< speed up with special case checks > Map2d <bool> bitmapToDraw = cachedCircles[index]; for (int bitmapY = 0; bitmapY < bitmapToDraw.getSize().y; bitmapY++) { for (int bitmapX = 0; bitmapX < bitmapToDraw.getSize().x; bitmapX++) { int absoluteX = bitmapX + (int)center.x; int absoluteY = bitmapY + (int)center.y; if (absoluteX < 0 || absoluteX >= target.getSize().x || absoluteY < 0 || absoluteY >= target.getSize().y) { continue; } if (!bitmapToDraw.read(new Vector2d <uint>((uint)bitmapX, (uint)bitmapY))) { continue; } target.write(new Vector2d <uint>((uint)absoluteX, (uint)absoluteY), value); } } }
private bool AddImages() { fgeimages = new List <Map2d>(); string datapath = EDDOptions.Instance.MapsAppDirectory(); fgeimages = Map2d.LoadImages(datapath); return(fgeimages.Count > 0); }
public void calculateKernelsForPositions(Map2d<float> map, ref Misc.Vector2<int>[] kernelPositions, ref float[] kernelValues ) { int i; for( i = 0; i < kernelPositions.Length; i++ ) { kernelValues[i] = multiplyKernelWithMapAtCenter(kernelPositions[i].x, kernelPositions[i].y, map); } }
public void drawCircle(Map2d <Type> target, Vector2d <uint> position, uint radius, Type value) { bool cachedCircleDrawn = drawCircleIfCached(target, position, radius, value); if (!cachedCircleDrawn) { // TODO< draw circle using slow algorithm, bresenham > Debug.Assert(false, "TODO"); } }
bool isFiring(Map2d <float> map) { float activation = 0.0f; foreach (Vector2d <uint> iterationProbePosition in probePositions) { activation += map.read(iterationProbePosition); } return(activation > threshold); }
public void Init(Map2d g) { galaxy = g; xlines = GridId.XLines(galaxy.TopRight.X); xlines[0] = galaxy.TopLeft.X; zlines = GridId.ZLines(galaxy.TopLeft.Y); // in numeric incr order zlines[0] = galaxy.BottomLeft.Y; ClickToZoom = false; includedgridid = new List <int>(); }
public static Map2d<float> convolution(Map2d<float> input, Map2d<float> inputKernel) { float[,] inputAsMatrix, kernelAsMatrix; Map2d<float> resultMap; int x, y; inputAsMatrix = convertMapToArray(input); kernelAsMatrix = convertMapToArray(inputKernel); resultMap = new Map2d<float>(input.getWidth(), input.getLength()); for( y = 0; y < input.getLength() - inputKernel.getLength(); y++ ) { for( x = 0; x < input.getWidth() - inputKernel.getWidth(); x++ ) { float value; value = convolutionAt(ref inputAsMatrix, ref kernelAsMatrix, x, y); resultMap.writeAt(x, y, value); } } return resultMap; // old broken not working code /* combinedWidth = calculateCombinedWidthPowerTwo((int)input.getWidth(), (int)inputKernel.getWidth()); combinedHeight = calculateCombinedWidthPowerTwo((int)input.getLength(), (int)inputKernel.getLength()); paddedInput = getPadded(input, combinedWidth, combinedHeight); paddedKernel = getPadded(inputKernel, combinedWidth, combinedHeight); //return cutAndConvertToMap(paddedKernel, 0, 0, paddedInput.GetLength(0), paddedInput.GetLength(1)); fft.FFT fftKernel = new fft.FFT(paddedKernel); fftKernel.doFft(fft.FFT.EnumDirection.FORWARD); fft.ComplexNumber[,] fftOfKernel = fftKernel.Output; fft.FFT fftInput = new fft.FFT(paddedInput); fftInput.doFft(fft.FFT.EnumDirection.FORWARD); fft.ComplexNumber[,] fftOfInput = fftInput.Output; //fft.ComplexNumber[,] multipliedFft = multiplyComplexNumberArray(ref fftOfKernel, ref fftOfInput); fft.ComplexNumber[,] multipliedFft = NativeMatrix.multiply<fft.ComplexNumber>(fftOfKernel, fftOfInput); fft.FFT fftInverse = new fft.FFT(multipliedFft); fftInverse.doFft(fft.FFT.EnumDirection.BACKWARD); double[,] inverseResult = fftInverse.inverseResult; // grab the result and output it return cutAndConvertToMap(inverseResult, (int)inputKernel.getWidth() / 2, (int)inputKernel.getLength() / 2, (int)input.getWidth(), (int)input.getLength()); */ }
public void calculate(ResourceMetric metric, OpenCl.ComputeContext computeContext) { // blur motion map bluredMotionMap = doBlurMotionMap(metric, computeContext); // downsample maps downsampleMaps(metric); // novelity motion calculateNovelity(); calculateMasterNovelity(); }
/** * calculate the result of the radial kernel (rbf) on the OpenCL device * * * */ public void calculateRadialKernel(ResourceMetric resourceMetric, Map2d<float> inputMap, Vector2<int>[] radialKernelPositions, ref float[] radialKernelResults) { operatorRadialKernel.inputMap = inputMap; operatorRadialKernel.kernelPositions = radialKernelPositions; resourceMetric.startTimer("visual", "rbf calculation", ""); operatorRadialKernel.calculate(computeContext); // pull out the result operatorRadialKernel.kernelResults.CopyTo(radialKernelResults, 0); resourceMetric.stopTimer(); }
public static void test() { Map2d <float> toConvolute = new Map2d <float>(new Vector2d <uint>(10, 10)); toConvolute.write(new Vector2d <uint>(3, 2), 5.0f); toConvolute.write(new Vector2d <uint>(7, 5), -2.0f); Map2d <float> kernel = createGaussianKernel(5); Map2d <float> convolutionResult = Convolution2d.convolution(toConvolute, kernel); int debugBreakpointHere = 1; }
public void run2(Map2d <float> a, Map2d <float> result) { for (uint y = 0; y < result.getSize().y; y++) { for (uint x = 0; x < result.getSize().x; x++) { Vector2d <uint> pos = new Vector2d <uint>(x, y); float aValue = a.read(pos), temp = (float)Math.Sqrt(aValue); result.write(pos, temp); } } }
private bool AddImages() { fgeimages = new List <Map2d>(); string datapath = Path.Combine(EDDOptions.Instance.AppDataDirectory, "Maps"); if (Directory.Exists(datapath)) { fgeimages = Map2d.LoadImages(datapath); return(fgeimages.Count > 0); } else { return(false); } }
public override void Init() { BaseUtils.Translator.Instance.Translate(this); // translate before we add anything else to the panel systemlist = HistoryList.FilterByFSDCarrierJumpAndPosition(discoveryform.history.EntryOrder()); pickerStart = new ExtendedControls.ExtDateTimePicker(); pickerStop = new ExtendedControls.ExtDateTimePicker(); pickerStart.Size = new Size(200, 20); pickerStop.Size = new Size(200, 20); host1 = new ToolStripControlHost(pickerStart); toolStrip.Items.Add(host1); host2 = new ToolStripControlHost(pickerStop); toolStrip.Items.Add(host2); pickerStart.Value = DateTime.Today.AddMonths(-1); this.pickerStart.ValueChanged += new System.EventHandler(this.dateTimePickerStart_ValueChanged); this.pickerStop.ValueChanged += new System.EventHandler(this.dateTimePickerStop_ValueChanged); startDate = new DateTime(2010, 1, 1); images = EDDiscovery.Icons.IconMaps.StandardMaps(); images.AddRange(Map2d.LoadFromFolder(EDDOptions.Instance.MapsAppDirectory())); toolStripComboExpo.Items.Clear(); foreach (Map2d img in images) { toolStripComboExpo.Items.Add(img.FileName); } toolStripComboBoxTime.Items.AddRange(new string[] { "Last Week".T(EDTx.Form2DMap_LastWeek), "Last Month".T(EDTx.Form2DMap_LastMonth), "Last Year".T(EDTx.Form2DMap_LastYear), "All".T(EDTx.Form2DMap_All), "Custom".T(EDTx.Form2DMap_Custom) }); toolStripComboExpo.SelectedIndex = 0; // causes a display toolStripComboBoxTime.SelectedIndex = 3; imageViewer.BackColor = Color.FromArgb(5, 5, 5); discoveryform.OnHistoryChange += Discoveryform_OnHistoryChange; discoveryform.OnNewEntry += Discoveryform_OnNewEntry; }
public MainWindow() { string devTexturesDir = @"..\..\Textures"; if (IO.Directory.Exists("Textures")) { texturesDir = "Textures"; } else if (IO.Directory.Exists(devTexturesDir)) { texturesDir = devTexturesDir; } else { IO.Directory.CreateDirectory("Textures"); texturesDir = "Textures"; } Textures = new ObservableCollection <TextureEntry> (); LoadTextures(); fsWatcher = new IO.FileSystemWatcher(texturesDir); fsWatcher.Created += new IO.FileSystemEventHandler(fsWatcher_Created); fsWatcher.EnableRaisingEvents = true; Map2d map = Texture2d.Load(IO.Path.Combine(texturesDir, "checker_small.bmp")); //Map2d map = Texture2d.Load ( IO.Path.Combine ( texturesDir, "checker_small_2x2.bmp" ) ); //Map2d map = Texture2d.Load ( IO.Path.Combine ( texturesDir, "checker.jpg" ); //Map2d map = Texture2d.Load ( @"D:\Programming\Projects\msvs\SoftwareRendering\TextureFilteringDev\lines.bmp" ); //Map2d map = Texture2d.Load ( IO.Path.Combine ( texturesDir, "checker_colored.jpg" ) ); //Map2d map = Texture2d.Load ( @"C:\media\images\Wallpaper\Текстуры\Architectual\DRTCHEK1.JPG" ); //Map2d map = Texture2d.Load ( @"C:\media\images\keira knightley\marso-uwi_bI.jpg" ); //Map2d map = Texture2d.Load ( @"C:\media\images\my renders\my first raytracer\refraction_fixed_water_1.342.bmp" ); //Map2d map = Texture2d.Load ( IO.Path.Combine ( texturesDir, "alena_zayac.jpg" ) ); sampler = new Sampler2d(map, TextureWrap.Clamp, FilteringType.Nearest); InitializeComponent(); mouseXRatio = 360.0 / System.Windows.SystemParameters.PrimaryScreenWidth; mouseYRatio = 360.0 / System.Windows.SystemParameters.PrimaryScreenHeight; figureTriStrip = GenQuadUsingTriangleStrip(); GenCubeUsingIndices(out figureVertices, out figureIndices, out figureTexCoords); resizeTimer.Interval = TimeSpan.FromSeconds(0.5); resizeTimer.Tick += new EventHandler(resizeTimer_Tick); }
private bool drawCircleIfCached(Map2d <Type> target, Vector2d <uint> center, uint radius, Type value) { if (radius == 0) { return(false); } int index = (int)(radius - cachedCirclesFirstRadius); if (index >= cachedCircles.Count) { return(false); } drawCachedCircle(target, index, center, value); return(true); }
private static float averageForFloat(Map2d<float> input, int x, int y, int squareSize) { int ix, iy; float average; average = 0.0f; for( iy = y*squareSize; iy < y*(squareSize+1); iy++ ) { for( ix = x*squareSize; ix < x*(squareSize+1); ix++ ) { average += input.readAt(ix, iy); } } return average/(float)(squareSize*squareSize); }
public static void drawTriangle(Vector2<uint> a, Vector2<uint> b, Vector2<uint> c, Map2d<bool> image) { Vector2<uint> drawStart; Vector2<uint> drawEnd; uint x; uint y; // small hack drawStart = Vector2<uint>.min(a, b, c, c); drawEnd = Vector2<uint>.max(a, b, c, c); for( y = drawStart.y; y < drawEnd.y; y++ ) { for( x = drawStart.x; x < drawEnd.x; x++ ) { Vector2<float> aAsFloat, bAsFloat, cAsFloat, point; aAsFloat = new Vector2<float>(); bAsFloat = new Vector2<float>(); cAsFloat = new Vector2<float>(); point = new Vector2<float>(); aAsFloat.x = (float)a.x; aAsFloat.y = (float)a.y; bAsFloat.x = (float)b.x; bAsFloat.y = (float)b.y; cAsFloat.x = (float)c.x; cAsFloat.y = (float)c.y; point.x = (float)x; point.y = (float)y; if( checkIfItTriangle(aAsFloat, bAsFloat, cAsFloat, point) ) { image.writeAt((int)x, (int)y, true); } else { image.writeAt((int)x, (int)y, false); } } } }
public static void testImageCompressor() { Vector2d <uint> imageSize = new Vector2d <uint>(5, 7); Map2d <float> stimulus = new Map2d <float>(imageSize); ImageDrawer <float> drawer = new ImageDrawer <float>(); // draw candidate drawer.drawHorizontalLine(stimulus, new Vector2d <uint>(1, 1), 3, 1.0f); TestEncoder testEncoder = new TestEncoder(); TestEncoder.Candidate bestCandidate = testEncoder.encode(stimulus); int debug = 1; }
float TileScorer(MazeTile mt, Map2d map) { int neighborFloor = -1;// CountTileNeighbors(mt.coord, _allDirs, c => map[c].letter == '#');//-1; Coord msize = map.GetSize(); Coord min = -Coord.One, max = Coord.One; Coord.ForEachInclusive(min, max, off => { Coord c = mt.coord + off; if (c.IsWithin(msize) && map[c].letter == ' ') { ++neighborFloor; } }); int maxDist = (msize.row + msize.col) / 2; float distFromCenter = Mathf.Abs((msize.row - 1) / 2f - mt.coord.row) + Mathf.Abs((msize.col - 1) / 2f - mt.coord.col); return(neighborFloor + distFromCenter / maxDist); }
// slow and naive implementation private static Map2d <bool> drawCircleSlow(uint radius) { Map2d <bool> result = new Map2d <bool>(new Vector2d <uint>(radius * 2 + 1, radius * 2 + 1)); Vector2d <float> center = new Vector2d <float>((float)radius, (float)radius); for (uint y = 0; y < radius * 2 + 1; y++) { for (uint x = 0; x < radius * 2 + 1; x++) { if (distance(new Vector2d <float>((float)x, (float)y), center) <= (float)radius) { result.write(new Vector2d <uint>(x, y), true); } } } return(result); }
public Map2d<float> getMasterNovelityAsMap() { Map2d<float> result; int x, y; result = new Map2d<float>((uint)featureMapSize.x, (uint)featureMapSize.y); for( y = 0; y < featureMapSize.y; y++ ) { for( x = 0; x < featureMapSize.x; x++ ) { float value; value = masterNovelity[x + y * featureMapSize.x]; result.writeAt(x, y, value); } } return result; }
public static Map2d<float> forFloat(Map2d<float> input, uint squareSize) { Map2d<float> resultMap; int x, y; resultMap = new Map2d<float>(input.getWidth() / squareSize, input.getLength() / squareSize); for( y = 0; y < resultMap.getLength(); y++ ) { for( x = 0; x < resultMap.getWidth(); x++ ) { float averageResult; averageResult = averageForFloat(input, x, y, (int)squareSize); resultMap.writeAt(x, y, averageResult); } } return resultMap; }
private float calcDistance(Map2d <float> image, Map2d <float> compressedCompare) { float distance = 0.0f; for (uint y = 0; y < image.getSize().y; y++) { for (uint x = 0; x < image.getSize().x; x++) { Vector2d <uint> position; position.x = x; position.y = y; float valueImage = image.read(position); float valueCompressedCompare = compressedCompare.read(position); distance += ((valueImage - valueCompressedCompare) * (valueImage - valueCompressedCompare)); } } return(distance); }
public static Map2d<float> blur(int radius, Map2d<float> input) { Map2d<float> resultMap; Map2d<float> tempMap; float[] kernel; int i; int di; // create kernel kernel = new float[(radius-1) * 2 + 1]; float variance; float normalisation; variance = (float)System.Math.Sqrt(0.15f); kernel[radius-1] = 1.0f; normalisation = Misc.Gaussian.calculateGaussianDistribution(0.0f, 0.0f, variance); for( di = 1; di < radius; di++ ) { float gaussianResult; float normalizedResult; gaussianResult = Misc.Gaussian.calculateGaussianDistribution((float)(di)/(float)(radius), 0.0f, variance); normalizedResult = gaussianResult / normalisation; kernel[radius-1+di] = normalizedResult; kernel[radius-1-di] = normalizedResult; } resultMap = new Map2d<float>(input.getWidth(), input.getLength()); tempMap = new Map2d<float>(input.getWidth(), input.getLength()); blurX(input, tempMap, kernel); blurY(tempMap, resultMap, kernel); return resultMap; }
public static Map2d<bool> mapOr2(Map2d<bool> a, Map2d<bool> b) { // TODO< assert same size Map2d<bool> resultMap; int x, y; resultMap = new Map2d<bool>(a.getWidth(), a.getLength()); for( y = 0; y < a.getLength(); y++ ) { for( x = 0; x < a.getWidth(); x++ ) { bool bit; bit = a.readAt(x, y) || b.readAt(x, y); resultMap.writeAt(x, y, bit); } } return resultMap; }
private void ShowSelectedImage() { string str = toolStripComboExpo.SelectedItem.ToString(); Map2d fgeimg = fgeimages.FirstOrDefault(i => i.FileName == str); if (fgeimg != null && initdone) { imageViewer.Image?.Dispose(); //panel1.BackgroundImage = new Bitmap(fgeimg.FilePath); imageViewer.Image = new Bitmap(fgeimg.FilePath); imageViewer.ZoomToFit(); currentFGEImage = fgeimg; if (toolStripButtonStars.Checked) { DrawStars(); } DrawTravelHistory(); imageViewer.Invalidate(); } }
public static Map2d<bool> corode(Map2d<bool> input) { Map2d<bool> result; int x, y; result = new Map2d<bool>(input.getWidth(), input.getLength()); for( y = 1; y < input.getLength() - 1; y++ ) { for( x = 1; x < input.getWidth() - 1; x++ ) { // optimized if( !input.readAt(x-1,y-1) || !input.readAt(x,y-1) || !input.readAt(x+1,y-1) || !input.readAt(x-1,y) || !input.readAt(x,y) || !input.readAt(x+1,y) || !input.readAt(x-1,y+1) || !input.readAt(x,y+1) || !input.readAt(x+1,y+1) ) { continue; } result.writeAt(x, y, true); } } return result; }
public void calculate(ComputationBackend.OpenCl.ComputeContext computeContext) { int x, y; System.Diagnostics.Debug.Assert(inputA.getWidth() == inputB.getWidth()); System.Diagnostics.Debug.Assert(inputA.getLength() == inputB.getLength()); result = new Map2d<float>((uint)inputA.getWidth(), (uint)inputA.getLength()); for( y = 0; y < inputA.getLength(); y++ ) { for( x = 0; x < inputA.getWidth(); x++ ) { float valueA, valueB, valueResult; valueA = inputA.readAt(x, y); valueB = inputB.readAt(x, y); valueResult = System.Math.Max(valueA, valueB); result.writeAt(x, y, valueResult); } } }
/** * * * * \param phi angle in radiants * \param spartialRatioAspect ellipticity of the support of the Gabor function */ public static Map2d<float> generateGaborKernel(int width, float phi, float lambda, float phaseOffset, float spartialRatioAspect) { float xTick, yTick; float sigma; int xInt, yInt; // constant from http://bmia.bmt.tue.nl/Education/Courses/FEV/course/pdf/Petkov_Gabor_functions2011.pdf sigma = 0.56f * lambda; Map2d<float> resultMap = new Map2d<float>((uint)width, (uint)width); for( yInt = 0; yInt < width; yInt++ ) { for( xInt = 0; xInt < width; xInt++ ) { float x, y; float insideExp, insideCos; float filterValue; x = ((float)(xInt - width / 2)/(float)width) * 2.0f; y = ((float)(yInt - width / 2)/(float)width) * 2.0f; xTick = x * (float)System.Math.Cos(phi) + y * (float)System.Math.Sin(phi); yTick = -x * (float)System.Math.Sin(phi) + y * (float)System.Math.Cos(phi); insideExp = - (xTick*xTick + spartialRatioAspect*spartialRatioAspect * yTick*yTick)/(2.0f*sigma*sigma); insideCos = 2.0f*(float)System.Math.PI * (xTick/lambda) + phaseOffset; filterValue = (float)System.Math.Exp(insideExp) * (float)System.Math.Cos(insideCos); resultMap.writeAt(xInt, yInt, filterValue); } } return resultMap; }
static private Map2d <float> createGaussianKernel(uint size) { Map2d <float> resultKernel = new Map2d <float>(new Vector2d <uint>(size, size)); float variance = (float)System.Math.Sqrt(0.15f); float normalisation = Gaussian.calculateGaussianDistribution(0.0f, 0.0f, variance); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { float relativeX = ((float)x / size) * 2.0f - 1.0f; float relativeY = ((float)y / size) * 2.0f - 1.0f; float distanceFromCenter = (float)System.Math.Sqrt(relativeX * relativeX + relativeY * relativeY); float gaussianResult = Gaussian.calculateGaussianDistribution(distanceFromCenter, 0.0f, variance); float normalizedGaussianResult = gaussianResult / normalisation; resultKernel.write(new Vector2d <uint>((uint)x, (uint)y), normalizedGaussianResult); } } return(resultKernel); }
public MappedMaterial(Map2d map, TextureWrap wrapMode, FilteringType filtering = FilteringType.Best) { this.Map = map; this.Sampler = new Sampler2d(map, wrapMode, filtering); }
private Map2d<float> doBlurMotionMap(ResourceMetric metric, OpenCl.ComputeContext computeContext) { Map2d<float> bluredMotionMap; metric.startTimer("visual attention", "blur motion map", ""); bluredMotionMap = new Map2d<float>(motionMap.getWidth(), motionMap.getLength()); blurMotionMap.inputMap = motionMap; blurMotionMap.outputMap = bluredMotionMap; blurMotionMap.calculate(computeContext); metric.stopTimer(); return bluredMotionMap; }
private void allocateDownsampledMaps(Vector2<int> mapSize) { int downsamplePowerCounter; int downsampleFactor; // allocate arrays motionDownsampled = new Map2d<float>[featureMapDownsamplePower]; // fill arrays with maps downsampleFactor = 2; for( downsamplePowerCounter = 0; downsamplePowerCounter < featureMapDownsamplePower; downsamplePowerCounter++ ) { System.Diagnostics.Debug.Assert((mapSize.x % downsampleFactor) == 0); System.Diagnostics.Debug.Assert((mapSize.y % downsampleFactor) == 0); motionDownsampled[downsamplePowerCounter] = new Map2d<float>((uint)(mapSize.x / downsampleFactor), (uint)(mapSize.y / downsampleFactor)); downsampleFactor *= 2; } }
public void calculateOperatorBlur(ResourceMetric resourceMetric, Map2d<float> inputMap, Map2d<float> outputMap) { operatorBlur.inputMap = inputMap; operatorBlur.outputMap = outputMap; resourceMetric.startTimer("visual", "blur", ""); operatorBlur.calculate(computeContext); resourceMetric.stopTimer(); }
/** \brief finds minimum in a map around a radius * * uses naive slow search * */ public static Vector2<int> findMinimum(Vector2<int> position, Map2d<float> image, uint radius) { return findLambdaUtilisation(position, image, radius, (old, compare) => compare < old); }
public void calculateOperatorFindNearest(ResourceMetric resourceMetric, Map2d<bool> inputMap, Vector2<int>[] inputPositions, ref bool[] foundNewPositions, ref Vector2<int>[] outputPositions) { operatorFindNearest.inputMap = inputMap; operatorFindNearest.inputPositions = inputPositions; resourceMetric.startTimer("visual", "retrack border pixels", ""); operatorFindNearest.calculate(computeContext); resourceMetric.stopTimer(); foundNewPositions = operatorFindNearest.foundNewPositions; outputPositions = operatorFindNearest.outputPositions; }
/** * * must be called before calculate is called * */ public void calculateKernel() { gaborKernel = Math.GaborKernel.generateGaborKernel(kernelWidth, kernelPhi, kernelLamda, kernelPhaseOffset, kernelSpartialRatioAspect); }
public void calculate(ComputationBackend.OpenCl.ComputeContext computeContext) { outputMap = Misc.Convolution2d.convolution(inputMap, gaborKernel); }
private static void downsampleMap(Map2d<float> source, Map2d<float> destination) { int destinationX; int destinationY; System.Diagnostics.Debug.Assert(source.getWidth() / 2 == destination.getWidth()); System.Diagnostics.Debug.Assert(source.getLength() / 2 == destination.getLength()); for( destinationY = 0; destinationY < destination.getLength(); destinationY++ ) { for( destinationX = 0; destinationX < destination.getWidth(); destinationX++ ) { float downsampledValue; downsampledValue = source.readAt(destinationX * 2, destinationY * 2); downsampledValue += source.readAt(destinationX * 2 + 1, destinationY * 2); downsampledValue += source.readAt(destinationX * 2, destinationY * 2 + 1); downsampledValue += source.readAt(destinationX * 2 + 1, destinationY * 2 + 1); downsampledValue *= 0.25f; destination.writeAt(destinationX, destinationY, downsampledValue); } } }
public static Map2d<bool> mapOr3(Map2d<bool> a, Map2d<bool> b, Map2d<bool> c) { return mapOr2(mapOr2(a, b), c); }
public void calculateAttentionModule(ResourceMetric resourceMetric, Map2d<float> motionMap) { attentionModule.motionMap = motionMap; attentionModule.calculate(resourceMetric, computeContext); }
private static Vector2<int> findLambdaUtilisation(Vector2<int> position, Map2d<float> image, uint radius, delegateCompare compare) { Vector2<int> borderMin; Vector2<int> borderMax; Vector2<int> min; Vector2<int> max; Vector2<int> rangeMin; Vector2<int> rangeMax; int x, y; float minimum; int minimumX, minimumY; Vector2<int> resultPosition; borderMin = new Vector2<int>(); borderMin.x = 0; borderMin.y = 0; borderMax = new Vector2<int>(); borderMax.x = (int)image.getWidth(); borderMax.y = (int)image.getLength(); rangeMin = new Vector2<int>(); rangeMin.x = position.x - (int)radius; rangeMin.y = position.y - (int)radius; rangeMax = new Vector2<int>(); rangeMax.x = position.x + (int)radius; rangeMax.y = position.y + (int)radius; // small hack because there is no overload for min = Vector2<int>.max(rangeMin, borderMin, borderMin, borderMin); max = Vector2<int>.min(rangeMax, borderMax, borderMax, borderMax); minimum = image.readAt(position.x, position.y); minimumX = position.x; minimumY = position.y; for( y = min.y; y < max.y; y++ ) { for( x = min.x; x < max.x; x++ ) { float sampled; sampled = image.readAt(x, y); if( compare(minimum, sampled) ) { minimumX = x; minimumY = y; } } } resultPosition = new Vector2<int>(); resultPosition.x = minimumX; resultPosition.y = minimumY; return resultPosition; }
public void calculateOperatorSkeletalize(ResourceMetric resourceMetric, Map2d<bool> inputMap, Map2d<bool> resultMap) { operatorSkeletalize.inputMap = inputMap; operatorSkeletalize.resultMap = resultMap; operatorSkeletalize.calculate(computeContext, resourceMetric); }
// // Zzzzz // zYyyz // zyXyz // zyyyz // zzzzz // public static Vector2<int> findNearestPositionWhereMapIs(bool value, Vector2<int> position, Map2d<bool> image, uint radius, out bool found) { Vector2<int> outwardIteratorOffsetUnbound; Vector2<int> borderMin; Vector2<int> borderMax; Vector2<int> one; Vector2<int> positionAsInt; found = false; outwardIteratorOffsetUnbound = new Vector2<int>(); outwardIteratorOffsetUnbound.x = 0; outwardIteratorOffsetUnbound.y = 0; borderMin = new Vector2<int>(); borderMin.x = 0; borderMin.y = 0; borderMax = new Vector2<int>(); borderMax.x = (int)image.getWidth(); borderMax.y = (int)image.getLength(); positionAsInt = new Vector2<int>(); positionAsInt.x = (int)position.x; positionAsInt.y = (int)position.y; one = new Vector2<int>(); one.x = 1; one.y = 1; for(;;) { Vector2<int> iteratorOffsetBoundMin; Vector2<int> iteratorOffsetBoundMax; int x, y; if (-outwardIteratorOffsetUnbound.x > radius) { break; } iteratorOffsetBoundMin = Vector2<int>.max(borderMin, outwardIteratorOffsetUnbound + positionAsInt, outwardIteratorOffsetUnbound + positionAsInt, outwardIteratorOffsetUnbound + positionAsInt); iteratorOffsetBoundMax = Vector2<int>.min(borderMax, outwardIteratorOffsetUnbound.getScaled(-1) + one + positionAsInt, borderMax, borderMax); for (y = (int)(iteratorOffsetBoundMin.y); y < iteratorOffsetBoundMax.y; y++ ) { for( x = (int)(iteratorOffsetBoundMin.x); x < iteratorOffsetBoundMax.x; x++ ) { // just find at the border if (y == (int)(iteratorOffsetBoundMin.y) || y == iteratorOffsetBoundMax.y - 1 || x == (int)(iteratorOffsetBoundMin.x) || x == iteratorOffsetBoundMax.x - 1) { bool valueAtPoint; valueAtPoint = image.readAt(x, y); if (valueAtPoint == value) { found = true; Vector2<int> result; result = new Vector2<int>(); result.x = x; result.y = y; return result; } } } } outwardIteratorOffsetUnbound.x--; outwardIteratorOffsetUnbound.y--; } found = false; return new Vector2<int>(); }
static void Main(string[] args) { // test ART2 //NeuralNetworks.AdaptiveResonanceTheory.Test0.test0(); // read programs and add them to the interpreter ProgramRepresentation.Parser.ProgramsParser programParser; ProgramRepresentation.Execution.FunctionalInterpreter functionalInterpreter; Datastructures.Dag<ProgramRepresentation.DagElementData> dag; List<ProgramRepresentation.Parser.ProgramsParser.Program> programs; ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState interpretationState; programParser = new ProgramRepresentation.Parser.ProgramsParser(); List<string> lines; lines = Misc.TextFile.readLinesFromFile("C:\\Users\\r0b3\\files\\backuped\\programmierung\\c#\\ai2" + "\\" + "ai2\\ai2\\usedSrc\\functionalPrograms" + "\\" + "matrix.txt"); programs = programParser.parse(lines); dag = new Datastructures.Dag<ProgramRepresentation.DagElementData>(); interpretationState = new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState(); // go though each program and // * add it to the dag // * add the path and the parameters to the interpreationState so it can invoke the program later foreach( ProgramRepresentation.Parser.ProgramsParser.Program currentProgram in programs ) { ProgramRepresentation.Parser.Functional.ParseTreeElement parseTree; int dagRootElementIndex; ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.InvokableProgram createdProgram; parseTree = ProgramRepresentation.Parser.Functional.parseRecursive(currentProgram.code); ProgramRepresentation.Parser.ConvertTreeToDag.convertRecursive(dag, parseTree, out dagRootElementIndex); createdProgram = new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.InvokableProgram(); createdProgram.dagIndex = dagRootElementIndex; createdProgram.path = currentProgram.path; // transcribe variablenames foreach( ProgramRepresentation.Parser.ProgramsParser.Parameter iterationParameter in currentProgram.parameters ) { createdProgram.variableNames.Add(iterationParameter.name); } interpretationState.invokablePrograms.Add(createdProgram); } //ProgramRepresentation.Parser.Functional.ParseTreeElement parseTree = ProgramRepresentation.Parser.Functional.parseRecursive("(fold (invoke [\"test\"] [#index]) [0 1 2])"); //ProgramRepresentation.Parser.Functional.ParseTreeElement parseTree = ProgramRepresentation.Parser.Functional.parseRecursive("(foreach (invoke [\"test\"] [#element]) [0])"); /* ProgramRepresentation.Parser.Functional.ParseTreeElement parseTree = ProgramRepresentation.Parser.Functional.parseRecursive("" + "(pass [" + "(invoke [\"math\" \"cos\"] [#rotation]) (* (invoke [\"math\" \"sin\"] [#rotation]) -1.0) 0.0 " + "(invoke [\"math\" \"sin\"] [#rotation]) (invoke [\"math\" \"cos\"] [#rotation]) 0.0 " + "0.0 0.0 1.0])"); */ // current test /* parseTree = ProgramRepresentation.Parser.Functional.parseRecursive("" + "(fold " + "(match (invoke [\"math\" \"equals\"] [(invoke [\"math\" \"mod\"] [(- #index 1) #nth]) 0]) " + "true (invoke [\"array\" \"append\"] [#accu #other]) " + "false #accu) " + "(invoke [\"array\" \"generate\"] [#])" + ")" ); */ functionalInterpreter = new ProgramRepresentation.Execution.FunctionalInterpreter(); functionalInterpreter.invokeDispatcher = new TestDispatcher(); interpretationState.currentScopeLevel = 1; interpretationState.scopeLevels.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel()); interpretationState.scopeLevels.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel()); interpretationState.scopeLevels[0].isTerminator = true; /* interpretationState.scopeLevels[1].dagElementIndex = 0; * * interpretationState.scopeLevels[1].variables.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel.Variable()); interpretationState.scopeLevels[1].variables[0].name = "input"; interpretationState.scopeLevels[1].variables[0].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.ARRAY); interpretationState.scopeLevels[1].variables[0].value.valueArray = new List<Datastructures.Variadic>(); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT)); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT)); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT)); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.FLOAT)); interpretationState.scopeLevels[1].variables[0].value.valueArray[0].valueFloat = 1.0f; interpretationState.scopeLevels[1].variables[0].value.valueArray[1].valueFloat = 2.0f; interpretationState.scopeLevels[1].variables[0].value.valueArray[2].valueFloat = 3.0f; interpretationState.scopeLevels[1].variables[0].value.valueArray[3].valueFloat = 4.0f; interpretationState.scopeLevels[1].variables.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel.Variable()); interpretationState.scopeLevels[1].variables[1].name = "nth"; interpretationState.scopeLevels[1].variables[1].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT); interpretationState.scopeLevels[1].variables[1].value.valueInt = 2; */ /* interpretationState.scopeLevels[1].dagElementIndex = 123; interpretationState.scopeLevels[1].variables.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel.Variable()); interpretationState.scopeLevels[1].variables[0].name = "a"; interpretationState.scopeLevels[1].variables[0].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.ARRAY); interpretationState.scopeLevels[1].variables[0].value.valueArray = new List<Datastructures.Variadic>(); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT)); interpretationState.scopeLevels[1].variables[0].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT)); interpretationState.scopeLevels[1].variables[0].value.valueArray[0].valueInt = 5; interpretationState.scopeLevels[1].variables[0].value.valueArray[1].valueInt = 6; interpretationState.scopeLevels[1].variables.Add(new ProgramRepresentation.Execution.FunctionalInterpreter.InterpretationState.ScopeLevel.Variable()); interpretationState.scopeLevels[1].variables[1].name = "b"; interpretationState.scopeLevels[1].variables[1].value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.ARRAY); interpretationState.scopeLevels[1].variables[1].value.valueArray = new List<Datastructures.Variadic>(); interpretationState.scopeLevels[1].variables[1].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT)); interpretationState.scopeLevels[1].variables[1].value.valueArray.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.INT)); interpretationState.scopeLevels[1].variables[1].value.valueArray[0].valueInt = 1; interpretationState.scopeLevels[1].variables[1].value.valueArray[1].valueInt = 2; */ interpretationState.scopeLevels[1].dagElementIndex = 199; for(;;) { bool terminatorReached; functionalInterpreter.interpreteStep(dag, interpretationState, out terminatorReached); if( terminatorReached ) { break; } } // output gabor kernel /* { Map2d<float> gaborKernel = Math.GaborKernel.generateGaborKernel(64, 0.0f, 10.0f/64.0f, (float)System.Math.PI*0.5f, 0.4f); Bitmap outputBitmap = new Bitmap(64, 64); int xp; int yp; for (xp = 0; xp < 64; xp++) { for (yp = 0; yp < 64; yp++) { Color color; float valueFloat; int valueInt; valueFloat = gaborKernel.readAt(xp, yp); valueFloat = System.Math.Min(1.0f, valueFloat); valueFloat = System.Math.Max(0.0f, valueFloat); valueInt = (int)(valueFloat * 255.0f); color = Color.FromArgb(valueInt, valueInt, valueInt); outputBitmap.SetPixel(xp, yp, color); } } outputBitmap.Save("C:\\Users\\r0b3\\temp\\aiExperiment0\\output\\gabor.png"); } */ // test fft /* double[,] kernelAsArray = new double[2, 2]; kernelAsArray[0,0] = 1.0; fft.FFT fftKernel = new fft.FFT(kernelAsArray); fftKernel.doFft(fft.FFT.EnumDirection.FORWARD); fft.ComplexNumber[,] fftOfKernel = fftKernel.Output; fft.FFT fftInverse = new fft.FFT(fftOfKernel); fftInverse.doFft(fft.FFT.EnumDirection.BACKWARD); double[,] inverseResult = fftInverse.inverseResult; int sdjisfdjiosfdjiosfd = 0; */ // calculate gabor kernel result /* { Map2d<float> inputMap; Map2d<float> resultMap; ComputationBackend.cs.OperatorGaborFilter gaborFilter; // generate test map inputMap = new Map2d<float>(128, 128); { int x, y; for( x = 0; x < 64; x++ ) { for( y = 0; y < 64; y++ ) { inputMap.writeAt(x + 32, y + 32, 1.0f); } } } gaborFilter = new ComputationBackend.cs.OperatorGaborFilter(); gaborFilter.inputMap = inputMap; gaborFilter.kernelLamda = 5.0f/16.0f; gaborFilter.kernelPhi = 0.0f; gaborFilter.kernelWidth = 16; gaborFilter.calculateKernel(); gaborFilter.calculate(null); resultMap = gaborFilter.outputMap; Bitmap outputBitmap = new Bitmap(128, 128); int xp; int yp; for (xp = 0; xp < 128; xp++) { for (yp = 0; yp < 128; yp++) { Color color; float valueFloat; int valueInt; valueFloat = resultMap.readAt(xp, yp); valueFloat = System.Math.Min(1.0f, valueFloat); valueFloat = System.Math.Max(0.0f, valueFloat); valueInt = (int)(valueFloat * 255.0f); color = Color.FromArgb(valueInt, valueInt, valueInt); outputBitmap.SetPixel(xp, yp, color); } } outputBitmap.Save("C:\\Users\\r0b3\\temp\\aiExperiment0\\output\\gabor.png"); } */ string pathToInputImages = "C:\\Users\\r0b3\\temp\\aiExperiment0\\input\\"; string pathToOutputImages = "C:\\Users\\r0b3\\temp\\aiExperiment0\\output\\"; int numberOfImages = 9240; //////////// string programLocation = Assembly.GetEntryAssembly().Location; string pathToLoad = Path.Combine(Path.GetDirectoryName(programLocation), "..\\..\\", "usedSrc\\programs\\cauchyDistribution.txt"); ProgramRepresentation.Program readProgram = ProgramRepresentation.ProgramReader.readProgram(pathToLoad); string CsSource = ProgramRepresentation.CsGenerator.generateSource(readProgram, 0, new Dictionary<string, int>()); string realSource = string.Format("// autogenerated\n\nclass CauchyDistributionClass{{\npublic static float cauchyDistribution(float x, float s, float t)\n{{\n{0}\n}}\n}}", CsSource); string pathToStoreSource = Path.Combine(Path.GetDirectoryName(programLocation), "..\\..\\", "usedSrc\\generated\\Cs\\cauchyDistribution.cs"); System.IO.File.WriteAllLines(pathToStoreSource, new String[] { realSource }); int imageNumber; Random random; random = new Random(); ResourceMetric metric; metric = new ResourceMetric(); MainContext mainContext; MainContext.Configuration mainContextConfiguration; int radialKernelPositionsLength; mainContextConfiguration = new MainContext.Configuration(); mainContextConfiguration.imageSize = new Vector2<int>(); mainContextConfiguration.imageSize.x = 1280; mainContextConfiguration.imageSize.y = 720; mainContextConfiguration.radialKernelSize = 3; mainContextConfiguration.attentionDownsamplePower = 4; mainContextConfiguration.attentionForgettingFactor = 0.7f; // like in the paper radialKernelPositionsLength = ((mainContextConfiguration.imageSize.x / 4) - 1) * (mainContextConfiguration.imageSize.y / 4); radialKernelPositionsLength += (32 - (radialKernelPositionsLength % 32)); mainContextConfiguration.radialKernelPositionsLength = radialKernelPositionsLength; mainContext = new MainContext(); mainContext.configure(mainContextConfiguration); mainContext.initialize(); // test neural stuff /* { NeuralNetworks.Neuroids.Neuroid<float, int> neuroidNetwork = new NeuralNetworks.Neuroids.Neuroid<float, int>(); neuroidNetwork.update = new NeuroidModels.Test0(6, 0.5f); neuroidNetwork.allocateNeurons(2, 1); neuroidNetwork.addConnection(0, 1, 1.0f); neuroidNetwork.input = new bool[1]; int step; neuroidNetwork.initialize(); neuroidNetwork.input[0] = true; for (step = 0; step < 20; step++ ) { neuroidNetwork.timestep(); neuroidNetwork.input[0] = false; neuroidNetwork.debugAllNeurons(); } int x = 0; } */ // test visual system { int frameNumber; ComputationBackend.cs.OperatorNeuroidVision neuroidVision; neuroidVision = new ComputationBackend.cs.OperatorNeuroidVision(); neuroidVision.configuration.cornerThreshold = 0.05f; neuroidVision.configuration.edgeThreshold = 0.05f; neuroidVision.configuration.imageSize = new Vector2<int>(); neuroidVision.configuration.imageSize.x = 64; neuroidVision.configuration.imageSize.y = 64; neuroidVision.configuration.directionCount = 2; // for testing neuroidVision.configuration.directionSampleDistance = 2; neuroidVision.configuration.layer0NeuroidLatencyAfterFiring = 6; neuroidVision.configuration.layer0NeuroidRandomFiringPropability = 0.001f; // for testing neuroidVision.initialize(mainContext.computeContext); for( frameNumber = 0; frameNumber < 200; frameNumber++) { neuroidVision.inputImage = new Map2d<float>((uint)neuroidVision.configuration.imageSize.x, (uint)neuroidVision.configuration.imageSize.y); { int x, y1, y; for (y1 = 0; y1 < 64-20; y1+=20) { for (y = y1; y < y1 + 10; y++ ) { for (x = 0; x < 64; x++) { neuroidVision.inputImage.writeAt(x, y, 1.0f); } } } } neuroidVision.calculate(mainContext.computeContext); // store image Bitmap outputBitmap = new Bitmap(neuroidVision.configuration.imageSize.x, neuroidVision.configuration.imageSize.y); int xp; int yp; for (xp = 0; xp < neuroidVision.configuration.imageSize.x; xp++) { for (yp = 0; yp < neuroidVision.configuration.imageSize.y; yp++) { Color color; ColorRgb readColor; readColor = neuroidVision.debugOutput.readAt(xp, yp); color = Color.FromArgb((int)(readColor.r * 255.0f), (int)(readColor.g * 255.0f), (int)(readColor.b * 255.0f)); outputBitmap.SetPixel(xp, yp, color); } } outputBitmap.Save(string.Format("C:\\Users\\r0b3\\temp\\aiExperiment0\\output\\visualDebug{0}.png", frameNumber)); } } return; ComputationBackend.cs.ParticleMotionTracker particleMotionTracker; // TODO< move into main context > particleMotionTracker = new ComputationBackend.cs.ParticleMotionTracker(); particleMotionTracker.initialize(mainContext.computeContext, mainContextConfiguration.imageSize); ComputationBackend.cs.OperatorColorTransform colorTransformForYellowBlue; ComputationBackend.cs.OperatorColorTransform colorTransformForRedGreen; colorTransformForYellowBlue = new ComputationBackend.cs.OperatorColorTransform(); colorTransformForYellowBlue.colorForZero = new ColorRgb(0.5f, 0.5f, 0.0f); colorTransformForYellowBlue.colorForOne = new ColorRgb(0.0f, 0.0f, 1.0f); colorTransformForRedGreen = new ComputationBackend.cs.OperatorColorTransform(); colorTransformForRedGreen.colorForZero = new ColorRgb(0.0f, 1.0f, 0.0f); colorTransformForRedGreen.colorForOne = new ColorRgb(1.0f, 0.0f, 0.0f); float repelMultiplicator = 0.01f; float fuseDistance = 0.05f; int forceIterations = 10; NeuralNetworks.AddaptiveParticle.AddaptiveParticle networkForImagePatches = new NeuralNetworks.AddaptiveParticle.AddaptiveParticle(repelMultiplicator, fuseDistance, forceIterations); //networkForImagePatches.distanceDelegate = getDistanceOfPatch; Map2d<float> channelRedGreen; Map2d<float> channelYellowBlue; channelRedGreen = new Map2d<float>((uint)mainContextConfiguration.imageSize.x, (uint)mainContextConfiguration.imageSize.y); channelYellowBlue = new Map2d<float>((uint)mainContextConfiguration.imageSize.x, (uint)mainContextConfiguration.imageSize.y); // test causal set algorithm if (false) { random = new Random(465654); // just for testing PartialOrderedSet.PartialOrderedSetAlgorithm causalSetAlgorithm; List<PartialOrderedSet.Relation> relations; causalSetAlgorithm = new PartialOrderedSet.PartialOrderedSetAlgorithm(random); relations = new List<PartialOrderedSet.Relation>(); string line; StreamReader file = null; file = new StreamReader(/*"C:\\Users\\r0b3\\temp\\causalSet.txt"*/ /*"C:\\Users\\r0b3\\temp\\causalSetLevel1.txt"*/"C:\\Users\\r0b3\\temp\\causalSetMyExperiment2.txt"); while ((line = file.ReadLine()) != null) { string[] relationStrings = line.Split(new char[] { ',' }); foreach (string relationString in relationStrings) { string relationString2 = relationString.Trim(); if (relationString2.Length == 0) { continue; } string[] causalStrings = relationString2.Split(new char[] { '<' }); Console.WriteLine(causalStrings[0].Trim()); Console.WriteLine(causalStrings[0].Trim()); int number0, number1; number0 = Convert.ToInt32(causalStrings[0].Trim()); number1 = Convert.ToInt32(causalStrings[1].Trim()); // order is correct relations.Add(new PartialOrderedSet.Relation(number0, number1)); } } // translate relations to constrains // because the relations are index based and not the actual element values List<CausalSets.Constraint> causalConstraints = new List<CausalSets.Constraint>(); foreach (PartialOrderedSet.Relation iterationRelation in relations) { CausalSets.Constraint newConstraint; // we assume here that the indices are equal to the element numbers newConstraint = new CausalSets.Constraint(); newConstraint.preiorElement = iterationRelation.sourceIndex; newConstraint.postierElement = iterationRelation.destinationIndex; causalConstraints.Add(newConstraint); } causalSetAlgorithm.fillCausalRelations(/*1322*/12, relations); causalSetAlgorithm.work(1); List<int> permutation = causalSetAlgorithm.getBestPermutation(); // reorder the preordered permutation to a better permutation PartialOrderedSet.NetworkAlgorithm2 causalNeuronBasedAlgorithm; causalNeuronBasedAlgorithm = new PartialOrderedSet.NetworkAlgorithm2(); causalNeuronBasedAlgorithm.random = random; causalNeuronBasedAlgorithm.triesUntilGiveUp = 50; causalNeuronBasedAlgorithm.maxIterations = 500000000; causalNeuronBasedAlgorithm.settingCommuteOnZero = false; causalNeuronBasedAlgorithm.poolMaxSize = 1; causalNeuronBasedAlgorithm.poolInitialSize = 1; List<int> reorderedPermutation = causalNeuronBasedAlgorithm.doWork(causalConstraints, permutation); /* CausalSets.IterativeBlockDeeping iterativeBlockDeepeningAlgorithm; iterativeBlockDeepeningAlgorithm = new CausalSets.IterativeBlockDeeping(); iterativeBlockDeepeningAlgorithm.causalNeuronBasedAlgorithm = causalNeuronBasedAlgorithm; List<int> reorderedPermutation = causalNeuronBasedAlgorithm.doWork(causalConstraints, permutation); //iterativeBlockDeepeningAlgorithm.doWork(permutation, causalConstraints); */ int skkdkd = 0; return; List<int> resultBestPermutation; List<int> bestPermutation = new List<int>(); file = new StreamReader("C:\\Users\\r0b3\\temp\\causalSetCorrectPermutation.txt"); while ((line = file.ReadLine()) != null) { string[] elementStrings = line.Split(new char[] { ' ' }); foreach (string elementString in elementStrings) { string trimedElementString = elementString.Trim(); if (trimedElementString.Length == 0) { continue; } bestPermutation.Add(Convert.ToInt32(trimedElementString)); } } //resultBestPermutation = bestPermutation; resultBestPermutation = reorderedPermutation;//permutation;//causalSetAlgorithm.getBestPermutation(); // read point coordinates List<float> pointCoordinates = new List<float>(); file = new StreamReader("C:\\Users\\r0b3\\temp\\orginalPointCoordinates.txt"); while ((line = file.ReadLine()) != null) { string[] elementStrings = line.Split(new char[] { ' ' }); List<string> nonvoidElements = new List<string>(); foreach (string elementString in elementStrings) { string trimedElementString = elementString.Trim(); if (trimedElementString.Length == 0) { continue; } nonvoidElements.Add(trimedElementString); } nonvoidElements[1] = nonvoidElements[1].Replace(".", ","); nonvoidElements[2] = nonvoidElements[2].Replace(".", ","); pointCoordinates.Add((float)Convert.ToDouble(nonvoidElements[1])); pointCoordinates.Add((float)Convert.ToDouble(nonvoidElements[2])); } Bitmap drawingBitmap = new Bitmap(500, 500); Graphics drawingGraphics = Graphics.FromImage(drawingBitmap); Pen penRed = new Pen(Brushes.Red); Pen penBlue = new Pen(Brushes.Blue); Vector2<int> lastPointPosition; lastPointPosition = new Vector2<int>(); lastPointPosition.x = 0; lastPointPosition.y = 0; // iterate throug the permutation and pick out the elements which are points, connect them with (colorcoded) lines int permutationIndex; for (permutationIndex = 0; permutationIndex < resultBestPermutation.Count; permutationIndex++) { int permutationElement; permutationElement = resultBestPermutation[permutationIndex]; // div by 2 because we store until now only single floats if (permutationElement < pointCoordinates.Count / 2) { Vector2<int> pointPosition; pointPosition = new Vector2<int>(); pointPosition.x = 0 + (int)((float)480 * (pointCoordinates[permutationElement * 2 + 0] / 10.0f)); pointPosition.y = 0 + (int)((float)480 * (pointCoordinates[permutationElement * 2 + 1] / 10.0f)); drawingGraphics.DrawLine(penBlue, lastPointPosition.x, lastPointPosition.y, pointPosition.x, pointPosition.y); lastPointPosition = pointPosition; } } drawingGraphics.Flush(); drawingBitmap.Save("C:\\Users\\r0b3\\temp\\connectedPoints.png"); int sdksfdjksfdjklsfdjkl = 0; } Map2d<ColorRgb> imageColor; Map2d<float> grayscaleImage; imageColor = new Map2d<ColorRgb>(1280, 720); for (imageNumber = 0; imageNumber < numberOfImages; imageNumber++) { // indicates if we need to reseed the points we track bool reseedTrackingPoints; Console.WriteLine(imageNumber.ToString()); Stopwatch stopwatch; stopwatch = new Stopwatch(); { Image readImage; metric.startTimer("visual", "read image", ""); readImage = Image.FromFile(pathToInputImages + (imageNumber + 1).ToString() + ".png"); metric.stopTimer(); // convert it to white/black image Bitmap workingBitmap = new Bitmap(readImage); grayscaleImage = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); //imageRComponent = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); //imageGComponent = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); //imageBComponent = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); int x, y; metric.startTimer("visual", "conversion", ""); var bitmapData = workingBitmap.LockBits(new Rectangle(new Point(0, 0), new Size(workingBitmap.Width, workingBitmap.Height)), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var length = bitmapData.Stride * bitmapData.Height; byte[] rawImage = new byte[length]; // Copy bitmap to byte[] Marshal.Copy(bitmapData.Scan0, rawImage, 0, length); workingBitmap.UnlockBits(bitmapData); for (y = 0; y < grayscaleImage.getLength(); y++) { for (x = 0; x < grayscaleImage.getWidth(); x++) { float grayscaleValue; Color readColor; float colorRed = (float)rawImage[x * 3 + 0 + y * bitmapData.Stride] / 255.0f; float colorGreen = (float)rawImage[x * 3 + 1 + y * bitmapData.Stride] / 255.0f; float colorBlue = (float)rawImage[x * 3 + 2 + y * bitmapData.Stride] / 255.0f; //readColor = workingBitmap.GetPixel(x, y); grayscaleValue = (colorRed + colorGreen + colorBlue/* + (float)readColor.G / 255.0f + (float)readColor.B / 255.0f*/) / 3.0f; grayscaleImage.writeAt(x, y, grayscaleValue); //imageRComponent.writeAt(x, y, colorRed); //imageGComponent.writeAt(x, y, colorGreen); //imageBComponent.writeAt(x, y, colorBlue); imageColor.writeAt(x, y, new ColorRgb(colorRed, colorGreen, colorBlue)); } } metric.stopTimer(); } metric.startTimer("visual", "transform color to red/green and yellow/blue", ""); colorTransformForRedGreen.inputRgb = imageColor; colorTransformForRedGreen.resultMap = new Map2d<float>(1280, 720); colorTransformForRedGreen.calculate(mainContext.computeContext); channelRedGreen = colorTransformForRedGreen.resultMap; colorTransformForYellowBlue.inputRgb = imageColor; colorTransformForYellowBlue.resultMap = new Map2d<float>(1280, 720); colorTransformForYellowBlue.calculate(mainContext.computeContext); channelYellowBlue = colorTransformForYellowBlue.resultMap; metric.stopTimer(); metric.startTimer("visual", "blurring", ""); Map2d<float> bluredGrayscaleImage = new Map2d<float>(grayscaleImage.getWidth(), grayscaleImage.getLength()); mainContext.calculateOperatorBlur(metric, grayscaleImage, bluredGrayscaleImage); metric.stopTimer(); metric.startTimer("visual", "edge detect", ""); Map2d<float> edgesAsFloat = Algorithm.Visual.EdgeDetector.detectEdgesFloat(bluredGrayscaleImage); metric.stopTimer(); metric.startTimer("visual", "threshold", ""); Map2d<bool> edgesImage2 = Algorithms.Visual.Binary.threshold(edgesAsFloat, 0.15f); metric.stopTimer(); /* metric.startTimer("visual", "skeletalize", ""); Map2d<bool> edgesImage = Algorithms.Visual.Binary.skeletalize(edgesImage2); metric.stopTimer(); */ Map2d<bool> edgesImage = new Map2d<bool>(edgesImage2.getWidth(), edgesImage2.getLength()); mainContext.calculateOperatorSkeletalize(metric, edgesImage2, edgesImage); // estimate edges int numberOfAngles = 20; Algorithms.Visual.RadialKernelDetector radialKernelDetector; radialKernelDetector = new Algorithms.Visual.RadialKernelDetector(); radialKernelDetector.configure(3); List<LinearBorderEntry> linearBorderEntries = new List<LinearBorderEntry>(); for (int angleI = 0; angleI < numberOfAngles; angleI++) { LinearBorderEntry createdLinearBorderEntry; int radialKernelArrayLength = (((int)edgesImage.getWidth() / 4) - 1) * ((int)edgesImage.getLength() / 4); radialKernelArrayLength += (32 - (radialKernelArrayLength % 32)); createdLinearBorderEntry = new LinearBorderEntry(); createdLinearBorderEntry.radialKernelResults = new float[radialKernelArrayLength]; createdLinearBorderEntry.radialKernelPositions = new Vector2<int>[radialKernelArrayLength]; createdLinearBorderEntry.borderCrossed = new bool[radialKernelArrayLength]; if (false /*angleI == 0*/ ) { /* // angle is 0 degrees int xp; int yp; for (xp = 0; xp < (edgesImage.getWidth() / 4); xp++) { for (yp = 0; yp < (edgesImage.getLength() / 4); yp++) { createdLinearBorderEntry.radialKernelPositions[xp + (edgesImage.getWidth() / 4) * yp] = new Vector2<int>(); createdLinearBorderEntry.radialKernelPositions[xp + (edgesImage.getWidth() / 4) * yp].x = xp * 4; createdLinearBorderEntry.radialKernelPositions[xp + (edgesImage.getWidth() / 4) * yp].y = yp * 4; } } createdLinearBorderEntry.indexOfOtherSide = createdLinearBorderEntry.radialKernelPositions.Length; */ } else if (angleI == numberOfAngles - 1) { int xp; int yp; for (xp = 0; xp < (edgesImage.getWidth() / 4) - 1; xp++) { for (yp = 0; yp < (edgesImage.getLength() / 4); yp++) { int currentXCoordinate; int currentYCoordinate; currentXCoordinate = xp * 4; currentYCoordinate = yp * 4; createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp] = new Vector2<int>(); createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp].x = currentXCoordinate; createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp].y = currentYCoordinate; } } } else { // angle is between -90 and 90 degrees float cosOfAngle; float sinOfAngle; float angle; int xp; int yp; angle = (((float)angleI / (float)numberOfAngles) * 2.0f - 1.0f) * (float)System.Math.PI * 0.5f; cosOfAngle = (float)System.Math.Cos(angle); sinOfAngle = (float)System.Math.Sin(angle); float tanOfAngle = sinOfAngle / cosOfAngle; int previousYCoordinate; for (yp = 0; yp < (edgesImage.getLength() / 4); yp++) { bool borderCrossed; borderCrossed = false; previousYCoordinate = 0; // - 1 because for (xp = 0; xp < (edgesImage.getWidth() / 4) - 1; xp++) { int currentXCoordinate; int currentYCoordinate; currentXCoordinate = (int)((float)(xp * 4)); currentYCoordinate = (int)(((int)((float)(yp * 4) + (float)(xp * 4) * tanOfAngle))); currentYCoordinate = Misc.Math.modi(currentYCoordinate, (int)edgesImage.getLength()); if (angle > 0.0f) { if (currentYCoordinate < previousYCoordinate) { borderCrossed = true; } } // todo< case for negative angle createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp] = new Vector2<int>(); createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp].x = currentXCoordinate; createdLinearBorderEntry.radialKernelPositions[xp + ((edgesImage.getWidth() / 4) - 1) * yp].y = currentYCoordinate; createdLinearBorderEntry.borderCrossed[xp + ((edgesImage.getWidth() / 4) - 1) * yp] = borderCrossed; previousYCoordinate = currentYCoordinate; } } } linearBorderEntries.Add(createdLinearBorderEntry); } // test code generation if (true) { /* ComputationBackend.OpenCl.OperatorRadialKernel operatorRadialKernel; ComputationBackend.OpenCl.ComputeContext computeContext; computeContext = new ComputationBackend.OpenCl.ComputeContext(); computeContext.initialize(); operatorRadialKernel = new ComputationBackend.OpenCl.OperatorRadialKernel(); //operatorRadialKernel.initialize(); operatorRadialKernel.createKernel(1); Misc.Vector2<int> imageSize; imageSize = new Vector2<int>(); imageSize.x = 1280; imageSize.y = 720; int kernelPositionsLength = ((imageSize.x / 4) - 1) * (imageSize.y / 4); operatorRadialKernel.initialize(computeContext, kernelPositionsLength, imageSize); operatorRadialKernel.inputMap = edgesAsFloat; operatorRadialKernel.kernelPositions = linearBorderEntries[0].radialKernelPositions; operatorRadialKernel.calculate(computeContext); // pull out the result operatorRadialKernel.kernelResults.CopyTo(linearBorderEntries[0].radialKernelResults, 0); */ int i; // for testing /* int x5, y5; for( x5 = 0; x5 < edgesAsFloat.getWidth(); x5++ ) { for( y5 = 0; y5 < edgesAsFloat.getLength(); y5++ ) { edgesAsFloat.writeAt(x5, y5, 1.0f); } }*/ for (i = 0; i < numberOfAngles; i++) { mainContext.calculateRadialKernel(metric, edgesAsFloat, linearBorderEntries[i].radialKernelPositions, ref linearBorderEntries[i].radialKernelResults); } } // gpu Vector2<int>[] inputPositionsForFindNearest = new Vector2<int>[particleMotionTracker.trackedBorderPixels.Count]; bool[] foundNewPositionsForFindNearest = new bool[particleMotionTracker.trackedBorderPixels.Count]; Vector2<int>[] outputPositionsFromFindNearest = new Vector2<int>[particleMotionTracker.trackedBorderPixels.Count]; int trackedPixelI; // translate to for (trackedPixelI = 0; trackedPixelI < particleMotionTracker.trackedBorderPixels.Count; trackedPixelI++) { inputPositionsForFindNearest[trackedPixelI] = particleMotionTracker.trackedBorderPixels[trackedPixelI].position; } mainContext.calculateOperatorFindNearest(metric, edgesImage, inputPositionsForFindNearest, ref foundNewPositionsForFindNearest, ref outputPositionsFromFindNearest); int resultPixelI; trackedPixelI = 0; // translate from for (resultPixelI = 0; resultPixelI < particleMotionTracker.trackedBorderPixels.Count; resultPixelI++) { if (foundNewPositionsForFindNearest[resultPixelI]) { particleMotionTracker.trackedBorderPixels[trackedPixelI].position = outputPositionsFromFindNearest[resultPixelI]; int xxx = 0; } else { particleMotionTracker.trackedBorderPixels.RemoveAt(trackedPixelI); trackedPixelI--; } trackedPixelI++; } // TODO< other strategy for reseeding > reseedTrackingPoints = true; particleMotionTracker.reseedTrackingPoints(metric, mainContext.computeContext, edgesImage); // should stay static // removes tracking points which are too close together // algorithm uses a (at the beginning empty) bool-map2d // for each point we sample the bitmap, if it is void we draw a small rectangle (with the radius as boundaries) // if it is not void we throw the sample away // this algorithm ensures that only the oldest points keep alive // algorithm outline // - sort each tracking point after age // - for each point // - check if map is true // - if yes, remove the point, continue // - if no, draw small rectangle and continue { Map2d<bool> usedMap; int iterationTrackedPixelI; List<ComputationBackend.cs.ParticleMotionTracker.TrackedPixel> sortedTrackedBorderPixels; int removedTrackedPointsCounter; usedMap = new Map2d<bool>(grayscaleImage.getWidth(), grayscaleImage.getLength()); removedTrackedPointsCounter = 0; metric.startTimer("visual", "remove too close tracking points", "sort"); sortedTrackedBorderPixels = particleMotionTracker.trackedBorderPixels.OrderByDescending(o => o.age).ToList(); metric.stopTimer(); metric.startTimer("visual", "remove too close tracking points", "map"); for (iterationTrackedPixelI = 0; iterationTrackedPixelI < sortedTrackedBorderPixels.Count; iterationTrackedPixelI++) { bool pointToRemove; ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel; iterationTrackedPixel = sortedTrackedBorderPixels[iterationTrackedPixelI]; pointToRemove = usedMap.readAt((int)iterationTrackedPixel.position.x, (int)iterationTrackedPixel.position.y); if (pointToRemove) { sortedTrackedBorderPixels.RemoveAt(iterationTrackedPixelI); iterationTrackedPixelI--; removedTrackedPointsCounter++; continue; } else { // draw rectangle Vector2<int> boxPosition; boxPosition = new Vector2<int>(); boxPosition.x = (int)iterationTrackedPixel.position.x; boxPosition.y = (int)iterationTrackedPixel.position.y; int x, y; Vector2<int> minBorder; Vector2<int> maxBorder; Vector2<int> boxRadius; boxRadius = new Vector2<int>(); boxRadius.x = 2; // box radius boxRadius.y = 2; // box radius minBorder = new Vector2<int>(); minBorder.x = 0; minBorder.y = 0; maxBorder = new Vector2<int>(); maxBorder.x = (int)grayscaleImage.getWidth() - 1; maxBorder.y = (int)grayscaleImage.getLength() - 1; minBorder = Vector2<int>.max(minBorder, boxPosition - boxRadius); maxBorder = Vector2<int>.min(maxBorder, boxPosition + boxRadius); for (y = minBorder.y; y < maxBorder.y; y++) { for (x = minBorder.x; x < maxBorder.x; x++) { usedMap.writeAt(x, y, true); } } } } // we can do this because we don't depend in any way until now on the order of the pixels particleMotionTracker.trackedBorderPixels = sortedTrackedBorderPixels; // = trackedBorderPixels.OrderByDescending(o => o.age).ToList(); Console.Write("removed tracked border pixels "); Console.WriteLine(removedTrackedPointsCounter); } metric.stopTimer(); // find neightbors of tracked pixels /* * uncommented because it is too slow * * but it works good { stopwatch = new Stopwatch(); stopwatch.Start(); int outerIndex; for( outerIndex = 0; outerIndex < trackedBorderPixels.Count; outerIndex++ ) { int innerIndex; trackedBorderPixels[outerIndex].neightborIndices.Clear(); for( innerIndex = 0; innerIndex < trackedBorderPixels.Count; innerIndex++ ) { float distance; Vector2<int> diff; diff = trackedBorderPixels[outerIndex].position - trackedBorderPixels[innerIndex].position; distance = (float)Math.Sqrt(diff.x * diff.x + diff.y * diff.y); // the distance calculation is a bit weird because we test against boxed distances in the algorithm to throw out points if( distance < 4.0f+1.001f ) { trackedBorderPixels[outerIndex].neightborIndices.Add(innerIndex); } } } stopwatch.Stop(); Console.WriteLine("neightborhoodsearch needed: {0}", stopwatch.Elapsed); } */ // ================================== // ================================== // attention algorithm // http://ilab.usc.edu/surprise/ metric.startTimer("visual attention", "motion drawing", ""); Bitmap motionBitmap; motionBitmap = new Bitmap(1280, 720); Graphics motionBitmapGraphics = Graphics.FromImage(motionBitmap); Pen whitePen = new Pen(Brushes.White); foreach (ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel in particleMotionTracker.trackedBorderPixels) { if ( iterationTrackedPixel.position.x == iterationTrackedPixel.oldPosition.x && iterationTrackedPixel.position.y == iterationTrackedPixel.oldPosition.y ) { continue; } motionBitmapGraphics.DrawLine( whitePen, iterationTrackedPixel.position.x, iterationTrackedPixel.position.y, iterationTrackedPixel.oldPosition.x, iterationTrackedPixel.oldPosition.y ); } motionBitmapGraphics.Flush(); metric.stopTimer(); metric.startTimer("visual attention", "convert motion to map", ""); Map2d<float> motionMap; motionMap = new Map2d<float>((uint)motionBitmap.Width, (uint)motionBitmap.Height); float[] motionMapArray = motionMap.unsafeGetValues(); var bitmapData2 = motionBitmap.LockBits(new Rectangle(new Point(0, 0), new Size(motionBitmap.Width, motionBitmap.Height)), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var length2 = bitmapData2.Stride * bitmapData2.Height; byte[] rawImage2 = new byte[length2]; // Copy bitmap to byte[] Marshal.Copy(bitmapData2.Scan0, rawImage2, 0, length2); motionBitmap.UnlockBits(bitmapData2); int x2, y2; for (x2 = 0; x2 < motionMap.getWidth(); x2++) { for (y2 = 0; y2 < motionMap.getLength(); y2++) { float value; value = (float)rawImage2[x2 * 3 + bitmapData2.Stride * y2] / 255.0f;//)(float)(motionBitmap.GetPixel(x2, y2).R / 255); motionMapArray[x2 + y2 * motionMap.getWidth()] = value; //motionMap.writeAt(x2, y2, value); } } metric.stopTimer(); /* metric.startTimer("visual attention", "blur motion map", ""); Map2d<float> bluredMotionMap = new Map2d<float>(motionMap.getWidth(), motionMap.getLength()); ComputationBackend.OpenCl.OperatorBlur blurMotionMap; blurMotionMap = new ComputationBackend.OpenCl.OperatorBlur(); blurMotionMap.inputMap = motionMap; blurMotionMap.outputMap = bluredMotionMap; Vector2<int> blurMapSize = new Vector2<int>(); blurMapSize.x = (int)motionMap.getWidth(); blurMapSize.y = (int)motionMap.getLength(); blurMotionMap.initialize(mainContext.computeContext, 5, blurMapSize); blurMotionMap.calculate(mainContext.computeContext); metric.stopTimer(); */ mainContext.calculateAttentionModule(metric, motionMap); // TODO< other stuff necessary > // now we try to remember small patches around the attention center // this gets later used by the whole higher visual stuff Vector2<int> positionOfMostAttention = mainContext.getAttentionModule().getPositionOfMostAttention(); int visualPatchSize = 32; // 32 pixel is the size of each patch int numberOfPatchesAroundAttention = 10; // must be even metric.startTimer("visual", "segment and store patches", ""); Vector2<int> visualPatchBegin; Vector2<int> visualPatchEnd; Vector2<int> visualPatchMin; Vector2<int> visualPatchMax; visualPatchMin = new Vector2<int>(); visualPatchMax = new Vector2<int>(); visualPatchMin.x = 0; visualPatchMin.y = 0; visualPatchMax.x = 1280; visualPatchMax.y = 720; Vector2<int> halfPatchWidth; halfPatchWidth = new Vector2<int>(); halfPatchWidth.x = (numberOfPatchesAroundAttention / 2) * visualPatchSize; halfPatchWidth.y = (numberOfPatchesAroundAttention / 2) * visualPatchSize; visualPatchBegin = positionOfMostAttention - halfPatchWidth; visualPatchEnd = positionOfMostAttention + halfPatchWidth; visualPatchBegin = Vector2<int>.max(visualPatchMin, visualPatchBegin); visualPatchEnd = Vector2<int>.min(visualPatchMax, visualPatchEnd); visualPatchBegin.x = visualPatchBegin.x - (visualPatchBegin.x % visualPatchSize); visualPatchBegin.y = visualPatchBegin.y - (visualPatchBegin.y % visualPatchSize); // NOTE< this does mean that the bottom/left most border are invisible for the visual system > visualPatchEnd.x = visualPatchEnd.x - (visualPatchEnd.x % visualPatchSize); visualPatchEnd.y = visualPatchEnd.y - (visualPatchEnd.y % visualPatchSize); int patchX, patchY; float[] patchDataYellowBlue; float[] patchDataRedGreen; int lastNeuronIndex; lastNeuronIndex = 0; patchDataYellowBlue = new float[visualPatchSize * visualPatchSize]; patchDataRedGreen = new float[visualPatchSize * visualPatchSize]; for (patchX = visualPatchBegin.x / visualPatchSize; patchX < visualPatchEnd.x / visualPatchSize; patchX++) { for (patchY = visualPatchBegin.y / visualPatchSize; patchY < visualPatchEnd.y / visualPatchSize; patchY++) { int patchI; int x, y; patchI = 0; for (y = patchY * visualPatchSize; y < (patchY + 1) * visualPatchSize; y++) { for (x = patchX * visualPatchSize; x < (patchX + 1) * visualPatchSize; x++) { patchDataRedGreen[patchI] = channelRedGreen.readAt(x, y); patchDataYellowBlue[patchI] = channelYellowBlue.readAt(x, y); patchI++; } } float[] patchVector = new float[2 * visualPatchSize * visualPatchSize]; int i; for (i = 0; i < visualPatchSize * visualPatchSize; i++) { patchVector[i] = patchDataRedGreen[i]; } for (i = 0; i < visualPatchSize * visualPatchSize; i++) { patchVector[i + visualPatchSize * visualPatchSize] = patchDataYellowBlue[i]; } int neuronIndex; networkForImagePatches.remember(patchVector, out neuronIndex); lastNeuronIndex = neuronIndex; } } metric.stopTimer(); System.Console.WriteLine(lastNeuronIndex); // store image for testing motionBitmap.Save(pathToOutputImages + "m" + (imageNumber + 1).ToString() + ".png"); // ================================== // ================================== // calculate (relative) velocity of tracked border pixels foreach (ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel in particleMotionTracker.trackedBorderPixels) { Vector2<int> absoluteVelocity; Vector2<float> velocity; absoluteVelocity = iterationTrackedPixel.position - iterationTrackedPixel.oldPosition; velocity = new Vector2<float>(); velocity.x = (float)absoluteVelocity.x / grayscaleImage.getWidth(); velocity.y = (float)absoluteVelocity.y / grayscaleImage.getWidth(); iterationTrackedPixel.velocity = velocity; iterationTrackedPixel.oldPosition = iterationTrackedPixel.position.clone(); } // first we group the points and then we cluster the groups // after that we try to fuse different clusters // we ignore the velocity of the points in our clustering process // translate the tracked points to points in the treenode Datastructures.TreeNode pointsTreeNode; pointsTreeNode = new Datastructures.TreeNode(); foreach (ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel in particleMotionTracker.trackedBorderPixels) { Vector2<float> normalizedPosition; Datastructures.TreeNode pointTreeNode; pointTreeNode = new Datastructures.TreeNode(); pointTreeNode.value = new Datastructures.Variadic(Datastructures.Variadic.EnumType.VECTOR2FLOAT); normalizedPosition = iterationTrackedPixel.getNormalizedPosition((int)grayscaleImage.getWidth()); pointTreeNode.value.valueVector2Float = normalizedPosition; pointsTreeNode.childNodes.Add(pointTreeNode); } Datastructures.Variadic pointsVariadic; pointsVariadic = new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE); pointsVariadic.valueTree = pointsTreeNode; Operators.Visual.GroupPointsGrid groupPointsGrid; groupPointsGrid = new Operators.Visual.GroupPointsGrid(); groupPointsGrid.sizeY = (float)grayscaleImage.getLength() / (float)grayscaleImage.getWidth(); Vector2<int> clusterWidth; clusterWidth = new Vector2<int>(); clusterWidth.x = 180; clusterWidth.y = 180; groupPointsGrid.cellSize = new Vector2<float>(); groupPointsGrid.cellSize.x = 1.0f / (float)clusterWidth.x; groupPointsGrid.cellSize.y = 1.0f / (float)clusterWidth.y; groupPointsGrid.cellOffset = new Vector2<float>(); List<Datastructures.Variadic> parameters; parameters = new List<Datastructures.Variadic>(); parameters.Add(pointsVariadic); Datastructures.Variadic resultGroupedPoints; groupPointsGrid.initialize(); GeneticProgramming.TypeRestrictedOperator.EnumResult calleeResult; groupPointsGrid.call(parameters, out resultGroupedPoints, out calleeResult); // call again with a 50% shifted grid groupPointsGrid.cellOffset.x = (1.0f / (float)clusterWidth.x) * 0.5f; groupPointsGrid.cellOffset.y = (1.0f / (float)clusterWidth.y) * 0.5f; Datastructures.Variadic resultGroupedPointsShifted; groupPointsGrid.call(parameters, out resultGroupedPointsShifted, out calleeResult); Operators.Visual.ConnectGroups connectGroups; connectGroups = new Operators.Visual.ConnectGroups(); connectGroups.gridsize = new Vector2<int>(); connectGroups.gridsize.x = clusterWidth.x; connectGroups.gridsize.y = clusterWidth.y; List<Datastructures.Variadic> parametersVariadic; parametersVariadic = new List<Datastructures.Variadic>(); parametersVariadic.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE)); parametersVariadic.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE)); parametersVariadic[0].valueTree = resultGroupedPoints.valueTree; parametersVariadic[1].valueTree = resultGroupedPoints.valueTree; Datastructures.Variadic connectedGroupsGraphVariadic; connectGroups.maxConnectionDistance = 0.011f * 0.85f; connectGroups.call(parametersVariadic, null, out connectedGroupsGraphVariadic, out calleeResult); System.Diagnostics.Debug.Assert(calleeResult == GeneticProgramming.TypeRestrictedOperator.EnumResult.OK); // call the operator for finding edges Operators.Visual.SearchScaffoldInGraph searchScaffoldInGraph; Datastructures.Variadic searchScaffoldResultVariadic; searchScaffoldInGraph = new Operators.Visual.SearchScaffoldInGraph(); searchScaffoldInGraph.random = random; searchScaffoldInGraph.searchingScaffoldRoot = new Scaffolds.Graph.ExtractLineScaffold(); parametersVariadic = new List<Datastructures.Variadic>(); parametersVariadic.Add(new Datastructures.Variadic(Datastructures.Variadic.EnumType.STORAGEALGORITHMICCONCEPTTREE)); parametersVariadic[0] = connectedGroupsGraphVariadic; stopwatch.Restart(); //searchScaffoldInGraph.call(parametersVariadic, null, out searchScaffoldResultVariadic, out calleeResult); stopwatch.Stop(); //Console.WriteLine("=visual find lines took {0} ms", stopwatch.Elapsed.Milliseconds); System.Diagnostics.Debug.Assert(calleeResult == GeneticProgramming.TypeRestrictedOperator.EnumResult.OK); // add one to age of particles foreach (ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel in particleMotionTracker.trackedBorderPixels) { iterationTrackedPixel.age++; } // drawing // just for testing stopwatch = new Stopwatch(); stopwatch.Start(); if (true) { Image readImage; readImage = Image.FromFile(pathToInputImages + (imageNumber + 1).ToString() + ".png"); // convert the image for showing to HSL and draw only the H component Bitmap bitmapX = new Bitmap(readImage); for (int y = 0; y < bitmapX.Height; y++) { for (int x = 0; x < bitmapX.Width; x++) { Color readColor = bitmapX.GetPixel(x, y); ColorRgb readRgb = new ColorRgb((float)readColor.R / 255.0f, (float)readColor.G / 255.0f, (float)readColor.B / 255.0f); ColorHsl hsl = ColorConversion.rgbToHsl(readRgb); Color writeColor; /* if (edgesImage.readAt(x, y)) { writeColor = Color.FromArgb(0, 0, 255); } else { writeColor = Color.FromArgb((int)(hsl.l * 255.0f), (int)(hsl.h * 255.0f), (int)(hsl.h * 255.0f)); } * */ float readEdgeValue = edgesAsFloat.readAt(x, y); int grayscaleValue = (int)((readEdgeValue / 10.0f) * 255.0f); grayscaleValue = System.Math.Min(grayscaleValue, 255); writeColor = Color.FromArgb(grayscaleValue, grayscaleValue, grayscaleValue); bitmapX.SetPixel(x, y, writeColor); } } bitmapX.Save(pathToOutputImages + "h" + (imageNumber + 1).ToString() + ".png"); } // draw the result into a new image and store it in the output folder { Image readImage; readImage = Image.FromFile(pathToInputImages + (imageNumber + 1).ToString() + ".png"); // convert it to white/black image Graphics drawingGraphics = Graphics.FromImage(readImage); Pen penRed = new Pen(Brushes.Red); Pen penGreen = new Pen(Brushes.Green); Pen penYellow = new Pen(Brushes.Yellow); Pen penWhite = new Pen(Brushes.White); /* foreach( TrackedPixel iterationTrackedPixel in trackedPixels ) { drawingGraphics.DrawLine(penRed, iterationTrackedPixel.position.x, (int)iterationTrackedPixel.position.y - 3, iterationTrackedPixel.position.x, iterationTrackedPixel.position.y + 3); drawingGraphics.DrawLine(penRed, (int)iterationTrackedPixel.position.x - 3, iterationTrackedPixel.position.y, iterationTrackedPixel.position.x + 3, iterationTrackedPixel.position.y); } */ /* // draw neightbor connections foreach (TrackedPixel iterationTrackedPixel in trackedBorderPixels) { foreach(int neightborIndex in iterationTrackedPixel.neightborIndices) { Vector2<int> neightborPosition; neightborPosition = trackedBorderPixels[neightborIndex].position; drawingGraphics.DrawLine(penGreen, iterationTrackedPixel.position.x, iterationTrackedPixel.position.y, neightborPosition.x, neightborPosition.y); } } */ if (false) { foreach (ComputationBackend.cs.ParticleMotionTracker.TrackedPixel iterationTrackedPixel in particleMotionTracker.trackedBorderPixels) { drawingGraphics.DrawLine(penRed, iterationTrackedPixel.position.x, (int)iterationTrackedPixel.position.y - 3, iterationTrackedPixel.position.x, iterationTrackedPixel.position.y + 3); drawingGraphics.DrawLine(penRed, (int)iterationTrackedPixel.position.x - 3, iterationTrackedPixel.position.y, iterationTrackedPixel.position.x + 3, iterationTrackedPixel.position.y); } } // draw point groups if (false) { foreach (Datastructures.TreeNode groupTreeNode in resultGroupedPoints.valueTree.childNodes) { Vector2<float> middle; float radius; Datastructures.TreeNode groupElementsTreeNode; groupElementsTreeNode = groupTreeNode.childNodes[0]; middle = new Vector2<float>(); radius = 0.0f; // calculate middlepoint foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { middle += pointTreeNode.value.valueVector2Float; } middle.scale(1.0f / (float)groupElementsTreeNode.childNodes.Count); // calculate radius foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { float iterationPointDistance; iterationPointDistance = (pointTreeNode.value.valueVector2Float - middle).magnitude(); if (iterationPointDistance > radius) { radius = iterationPointDistance; } } // draw it int circlePositionX = (int)(middle.x * (float)grayscaleImage.getWidth()); int circlePositionY = (int)(middle.y * (float)grayscaleImage.getWidth()); int circleRadius = (int)(radius * (float)grayscaleImage.getWidth()); drawingGraphics.DrawEllipse(penGreen, circlePositionX - circleRadius, circlePositionY - circleRadius, circleRadius * 2, circleRadius * 2); } foreach (Datastructures.TreeNode groupTreeNode in resultGroupedPointsShifted.valueTree.childNodes) { Vector2<float> middle; float radius; Datastructures.TreeNode groupElementsTreeNode; groupElementsTreeNode = groupTreeNode.childNodes[0]; middle = new Vector2<float>(); radius = 0.0f; // calculate middlepoint foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { middle += pointTreeNode.value.valueVector2Float; } middle.scale(1.0f / (float)groupElementsTreeNode.childNodes.Count); // calculate radius foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { float iterationPointDistance; iterationPointDistance = (pointTreeNode.value.valueVector2Float - middle).magnitude(); if (iterationPointDistance > radius) { radius = iterationPointDistance; } } // draw it int circlePositionX = (int)(middle.x * (float)grayscaleImage.getWidth()); int circlePositionY = (int)(middle.y * (float)grayscaleImage.getWidth()); int circleRadius = (int)(radius * (float)grayscaleImage.getWidth()); drawingGraphics.DrawEllipse(penYellow, circlePositionX - circleRadius, circlePositionY - circleRadius, circleRadius * 2, circleRadius * 2); } } // draw connected egde groups/(edges between edge point groups) Datastructures.TreeNode connectedEdgeGroupsGraph; connectedEdgeGroupsGraph = connectedGroupsGraphVariadic.valueTree; foreach (Datastructures.TreeNode graphEdgeTreeNode in connectedEdgeGroupsGraph.childNodes[1].childNodes) { int graphVertexAIndex; int graphVertexBIndex; graphVertexAIndex = graphEdgeTreeNode.childNodes[0].value.valueInt; graphVertexBIndex = graphEdgeTreeNode.childNodes[1].value.valueInt; Vector2<int> middleA; Vector2<int> middleB; middleA = new Vector2<int>(); middleB = new Vector2<int>(); // calculate middle for A { int vertexIndex = graphVertexAIndex; Datastructures.TreeNode graphVertices; graphVertices = connectedEdgeGroupsGraph.childNodes[0]; Datastructures.TreeNode groupElementsTreeNode; groupElementsTreeNode = graphVertices.childNodes[vertexIndex].childNodes[0]; Vector2<float> middle; middle = new Vector2<float>(); // calculate middlepoint foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { middle += pointTreeNode.value.valueVector2Float; } middle.scale(1.0f / (float)groupElementsTreeNode.childNodes.Count); middleA.x = (int)(middle.x * (float)grayscaleImage.getWidth()); middleA.y = (int)(middle.y * (float)grayscaleImage.getWidth()); } // calculate middleB { int vertexIndex = graphVertexBIndex; Datastructures.TreeNode graphVertices; graphVertices = connectedEdgeGroupsGraph.childNodes[0]; Datastructures.TreeNode groupElementsTreeNode; groupElementsTreeNode = graphVertices.childNodes[vertexIndex].childNodes[0]; Vector2<float> middle; middle = new Vector2<float>(); // calculate middlepoint foreach (Datastructures.TreeNode pointTreeNode in groupElementsTreeNode.childNodes) { middle += pointTreeNode.value.valueVector2Float; } middle.scale(1.0f / (float)groupElementsTreeNode.childNodes.Count); middleB.x = (int)(middle.x * (float)grayscaleImage.getWidth()); middleB.y = (int)(middle.y * (float)grayscaleImage.getWidth()); } // draw line drawingGraphics.DrawLine(penWhite, middleA.x, middleA.y, middleB.x, middleB.y); } // draw found lines /* foreach( Operators.Visual.SearchScaffoldInGraph.Line iterationLine in searchScaffoldInGraph.lines ) { Vector2<int> a; Vector2<int> b; a = new Vector2<int>(); b = new Vector2<int>(); a.x = (int)( iterationLine.a.x * (float)grayscaleImage.getWidth() ); a.y = (int)( iterationLine.a.y * (float)grayscaleImage.getWidth() ); b.x = (int)(iterationLine.b.x * (float)grayscaleImage.getWidth()); b.y = (int)(iterationLine.b.y * (float)grayscaleImage.getWidth()); drawingGraphics.DrawLine(penGreen, a.x, a.y, b.x, b.y); }*/ // estimate edges and draw if they are above threshold int i; for (i = 0; i < numberOfAngles; i++) { int xp; int yp; for (xp = 1; xp < (edgesImage.getWidth() / 4) - 1; xp++) { for (yp = 0; yp < (edgesImage.getLength() / 4); yp++) { float strength; strength = 0.0f; strength += linearBorderEntries[i].radialKernelResults[(xp - 1) + ((edgesImage.getWidth() / 4) - 1) * yp]; strength += linearBorderEntries[i].radialKernelResults[(xp) + ((edgesImage.getWidth() / 4) - 1) * yp]; strength += linearBorderEntries[i].radialKernelResults[(xp + 1) + ((edgesImage.getWidth() / 4) - 1) * yp]; if (strength > 5.5f) { Vector2<int> lineBegin; Vector2<int> lineEnd; if (linearBorderEntries[i].radialKernelPositions[xp + 1 + ((edgesImage.getWidth() / 4) - 1) * yp] == null) { continue; } lineBegin = linearBorderEntries[i].radialKernelPositions[xp - 1 + ((edgesImage.getWidth() / 4) - 1) * yp]; lineEnd = linearBorderEntries[i].radialKernelPositions[xp + 1 + ((edgesImage.getWidth() / 4) - 1) * yp]; drawingGraphics.DrawLine(penGreen, lineBegin.x, lineBegin.y, lineEnd.x, lineEnd.y); } } } } // draw line clusters /* int clusterColorCounter = 0; foreach( Algorithms.Visual.PointToLineSegmentation.ClusteredTrackedPixels iterationCluster in clusteredTrackedPixels ) { Pen penCluster; Vector2<int> lastPoint; if( (clusterColorCounter % 2) == 0 ) { penCluster = new Pen(Brushes.Yellow); } else { penCluster = new Pen(Brushes.Gray); } lastPoint = iterationCluster.trackedPixels[0].position; foreach( TrackedPixel iterationPixel in iterationCluster.trackedPixels ) { drawingGraphics.DrawLine(penCluster, lastPoint.x, lastPoint.y, iterationPixel.position.x, iterationPixel.position.y); lastPoint = iterationPixel.position; } clusterColorCounter++; } * */ drawingGraphics.Flush(); //Bitmap resultBitmap = GraphicsBitmapConverter.GraphicsToBitmap(drawingGraphics, Rectangle.Truncate(drawingGraphics.VisibleClipBounds)); //resultBitmap.Save(pathToOutputImages + (imageNumber + 1).ToString() + ".png"); readImage.Save(pathToOutputImages + (imageNumber + 1).ToString() + ".png"); } // draw attention map { Map2d<float> novelityMap; Bitmap outputBitmap; novelityMap = mainContext.attentionModuleGetMasterMap(); outputBitmap = new Bitmap((int)novelityMap.getWidth(), (int)novelityMap.getLength()); int xp; int yp; for (xp = 1; xp < novelityMap.getWidth(); xp++) { for (yp = 0; yp < novelityMap.getLength(); yp++) { Color color; float valueFloat; int valueInt; valueFloat = novelityMap.readAt(xp, yp); valueFloat = System.Math.Min(valueFloat, 1.0f); valueFloat /= 1.0f; valueInt = (int)(valueFloat * 255.0f); color = Color.FromArgb(valueInt, valueInt, valueInt); outputBitmap.SetPixel(xp, yp, color); } } outputBitmap.Save(pathToOutputImages + "a" + (imageNumber + 1).ToString() + ".png"); } stopwatch.Stop(); Console.WriteLine("writing needed: {0}", stopwatch.Elapsed); metric.report(); metric.reset(); } }
protected TextureEntry(Map2d map, string fileName) { this.Map = map; this.Name = IO.Path.GetFileName(fileName); this.Path = IO.Path.GetFullPath(fileName); }
// TODO< refactor the code so that the api doesn't leak out Candidate > public Candidate encode(Map2d <float> image) { List <Candidate> candidates = new List <Candidate>(); uint numberOfCandidatesToGenerate = 5000; // do this over and over again for (uint iterationCandidate = 0; iterationCandidate < numberOfCandidatesToGenerate; iterationCandidate++) { Candidate currentCandidate; Map2d <float> compressedCompare = new Map2d <float>(image.getSize()); int typeFromRandom = rand.Next(2); if (typeFromRandom == 0) { currentCandidate.type = Candidate.EnumType.LINE_HORIZONTAL; } else { currentCandidate.type = Candidate.EnumType.CIRCLE; } // draw candidate currentCandidate.startPosition = new Vector2d <uint>((uint)rand.Next((int)image.getSize().x), (uint)rand.Next((int)image.getSize().y)); currentCandidate.attribute = (uint)rand.Next(10); if (currentCandidate.type == Candidate.EnumType.LINE_HORIZONTAL) { drawer.drawHorizontalLine(compressedCompare, currentCandidate.startPosition, currentCandidate.attribute, 1.0f); } else // CIRLCE { drawer.drawCircle(compressedCompare, currentCandidate.startPosition, currentCandidate.attribute, 1.0f); } currentCandidate.distance = calcDistance(image, compressedCompare); candidates.Add(currentCandidate); } // search the best candidate { Candidate bestCandidate = candidates[0]; foreach (Candidate iterationCandidate in candidates) { if (iterationCandidate.distance < bestCandidate.distance) { bestCandidate = iterationCandidate; } } return(bestCandidate); } }
public void Generate(NonStandard.Data.Random.NumberGenerator random) { int width = ((stage + 2) * 2) + 1; mazeGenerationArguments.size = new Vector2(width, width); if (mazeSrc == null) { int seed = mazeGenerationArguments.seed; if (seed < 0) { seed = (int)GameClock.Time; } MazeGenerator mg = new MazeGenerator(random.Next); char[,] map = mg.Generate(mazeGenerationArguments.size, mazeGenerationArguments.start, mazeGenerationArguments.step, mazeGenerationArguments.wall); mg.Erode(map, mazeGenerationArguments.erosion); // generate ramps Coord size = map.GetSize(); List <Coord>[] ramp = new List <Coord> [4]; for (int r = 0; r < ramp.Length; ++r) { ramp[r] = new List <Coord>(); } const int W = 0, N = 1, E = 2, S = 3;// todo nwse size.ForEach(c => { if (c.row == 0 || c.row == size.row - 1 || c.col == 0 || c.col == size.col - 1) { return; } char letter = map.At(c); char n = map.At(c + Coord.Up), s = map.At(c + Coord.Down), e = map.At(c + Coord.Right), w = map.At(c + Coord.Left); bool createAtDeadEnds = true; bool createAtPeninsula = true; if (n == s && e != w) { if (letter == e && w == n) { if (letter == ' ' && createAtDeadEnds) { ramp[W].Add(c); } if (letter == '#' && createAtPeninsula) { ramp[E].Add(c); } } if (letter == w && e == n) { if (letter == ' ' && createAtDeadEnds) { ramp[E].Add(c); } if (letter == '#' && createAtPeninsula) { ramp[W].Add(c); } } } if (e == w && n != s) { if (letter == n && s == w) { if (letter == ' ' && createAtDeadEnds) { ramp[S].Add(c); } if (letter == '#' && createAtPeninsula) { ramp[N].Add(c); } } if (letter == s && n == e) { if (letter == ' ' && createAtDeadEnds) { ramp[N].Add(c); } if (letter == '#' && createAtPeninsula) { ramp[S].Add(c); } } } }); //ramp[W].ForEach(c => { map.SetAt(c, 'w'); }); //ramp[N].ForEach(c => { map.SetAt(c, 'n'); }); //ramp[E].ForEach(c => { map.SetAt(c, 'e'); }); //ramp[S].ForEach(c => { map.SetAt(c, 's'); }); int totalRamps = ramp.Sum(r => r.Count); for (int i = 0; i < totalRamps && i < stage + 1; ++i) { int[] r = ramp.GetNestedIndex(random.Next(totalRamps)); //Debug.Log(r.JoinToString(", ")); Coord loc = ramp[r[0]][r[1]]; ramp[r[0]].RemoveAt(r[1]); char ch = "wnes"[r[0]]; map.SetAt(loc, ch); --totalRamps; } this.map = new Map2d(map); //Debug.Log(this.map); } else { map.LoadFromString(mazeSrc.text); } seen.Reset(); int count = map.Height * map.Width; while (mazeTiles.Count < count) { mazeTiles.Add(Instantiate(prefab_mazeTile.gameObject).GetComponent <MazeTile>()); } //int index = 0; Vector3 off = new Vector3(map.Width / -2f * tileSize.x, 0, map.Height / -2f * tileSize.z); floorTiles = new List <MazeTile>(); floorTileNeighborHistogram.SetEach(0); map.GetSize().ForEach(c => { MazeTile mt = GetTile(c);//mazeTiles[index++]; mt.maze = this; mt.coord = c; Transform t = mt.transform; t.SetParent(_t); t.localPosition = mt.CalcLocalPosition(); MazeTile.Kind k = MazeTile.Kind.None; switch (map[c]) { case ' ': k = MazeTile.Kind.Floor; break; case '#': k = MazeTile.Kind.Wall; break; case 'w': k = MazeTile.Kind.RampWest; break; case 'n': k = MazeTile.Kind.RampNorth; break; case 's': k = MazeTile.Kind.RampSouth; break; case 'e': k = MazeTile.Kind.RampEast; break; } mt.kind = k; mt.SetDiscovered(false, null, this); if (mt.kind == MazeTile.Kind.Floor) { floorTiles.Add(mt); mt.goalScore = TileScorer(mt, map); int index = (int)mt.goalScore; if (index >= floorTileNeighborHistogram.Length) { Array.Resize(ref floorTileNeighborHistogram, index + 1); } ++floorTileNeighborHistogram[index]; } else { mt.goalScore = float.PositiveInfinity; } }); floorTiles.Sort((a, b) => a.goalScore.CompareTo(b.goalScore)); }
public static void learn() { string pathToImages = "C:\\Users\\r0b3\\temp\\aiExperiment0\\references\\"; string pathToOutputImages = "C:\\Users\\r0b3\\temp\\aiExperiment0\\output\\"; ComputationBackend.cs.OperatorColorTransformToHsl toHsl; ComputationBackend.cs.OperatorColorTransformFromHsl toRgb; toHsl = new ComputationBackend.cs.OperatorColorTransformToHsl(); toRgb = new ComputationBackend.cs.OperatorColorTransformFromHsl(); ComputationBackend.cs.OperatorColorTransform colorTransformForYellowBlue; ComputationBackend.cs.OperatorColorTransform colorTransformForRedGreen; colorTransformForYellowBlue = new ComputationBackend.cs.OperatorColorTransform(); colorTransformForYellowBlue.colorForZero = new ColorRgb(0.5f, 0.5f, 0.0f); colorTransformForYellowBlue.colorForOne = new ColorRgb(0.0f, 0.0f, 1.0f); colorTransformForRedGreen = new ComputationBackend.cs.OperatorColorTransform(); colorTransformForRedGreen.colorForZero = new ColorRgb(0.0f, 1.0f, 0.0f); colorTransformForRedGreen.colorForOne = new ColorRgb(1.0f, 0.0f, 0.0f); Image readImage = Image.FromFile(pathToImages + "StarCitizen0.jpg"); // convert it to white/black image Bitmap workingBitmap = new Bitmap(readImage); Map2d<ColorRgb> readMap = new Map2d<ColorRgb>((uint)workingBitmap.Width, (uint)workingBitmap.Height); Map2d<ColorHsl> temporaryHsl = new Map2d<ColorHsl>((uint)workingBitmap.Width, (uint)workingBitmap.Height); Map2d<ColorRgb> convertedMap = new Map2d<ColorRgb>((uint)workingBitmap.Width, (uint)workingBitmap.Height); int x, y; for( y = 0; y < workingBitmap.Height; y++ ) { for( x = 0; x < workingBitmap.Width; x++ ) { Color readColor = workingBitmap.GetPixel(x, y); float r, g, b; r = (float)readColor.R / 255.0f; g = (float)readColor.G / 255.0f; b = (float)readColor.B / 255.0f; ColorRgb color = new ColorRgb(r, g, b); readMap.writeAt(x, y, color); } } toHsl.inputRgb = readMap; toHsl.resultMap = temporaryHsl; toRgb.inputHsl = temporaryHsl; toRgb.resultMap = convertedMap; toHsl.calculate(null); for( y = 0; y < workingBitmap.Height; y++ ) { for( x = 0; x < workingBitmap.Width; x++ ) { ColorHsl hsl = temporaryHsl.readAt(x, y); hsl.l = 0.5f; // we don't want only the color without lighting information hsl.s = 1.0f; // same temporaryHsl.writeAt(x, y, hsl); } } toRgb.calculate(null); // convert to two channel representation Map2d<float> yellowBlueChannel = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); Map2d<float> redGreenChannel = new Map2d<float>((uint)workingBitmap.Width, (uint)workingBitmap.Height); colorTransformForYellowBlue.inputRgb = convertedMap; colorTransformForYellowBlue.resultMap = yellowBlueChannel; colorTransformForYellowBlue.calculate(null); colorTransformForRedGreen.inputRgb = convertedMap; colorTransformForRedGreen.resultMap = redGreenChannel; colorTransformForRedGreen.calculate(null); GeneticAlgorithm.VisualLowlevel.VisualLowLevel visualLowLevel; visualLowLevel = new GeneticAlgorithm.VisualLowlevel.VisualLowLevel(); visualLowLevel.patchWidth = 32; // TODO< read all tiles from two channels > int tileX, tileY; for( tileY = 0; tileY < workingBitmap.Height / 32 - 1; tileY++ ) { for (tileX = 0; tileX < workingBitmap.Height / 32 - 1; tileX++) { GeneticAlgorithm.VisualLowlevel.VisualLowLevel.PatchSample patchYellowBlue; GeneticAlgorithm.VisualLowlevel.VisualLowLevel.PatchSample patchRedGreen; patchYellowBlue = new VisualLowLevel.PatchSample(); patchYellowBlue.values = new float[32 * 32]; patchRedGreen = new VisualLowLevel.PatchSample(); patchRedGreen.values = new float[32 * 32]; for( y = 0; y < 32; y++ ) { for (x = 0; x < 32; x++) { int absoluteX, absoluteY; absoluteX = x + 32 * tileX; absoluteY = y + 32 * tileY; patchYellowBlue.values[x + y * 32] = yellowBlueChannel.readAt(absoluteX, absoluteY); patchRedGreen.values[x + y * 32] = redGreenChannel.readAt(absoluteX, absoluteY); } } visualLowLevel.patchSamples.Add(patchYellowBlue); visualLowLevel.patchSamples.Add(patchRedGreen); } } visualLowLevel.work(50000); List<float[]> templatesResult; templatesResult = new List<float[]>(); visualLowLevel.getBestTemplates(templatesResult); // store template as image { Bitmap outputBitmap = new Bitmap(32, 32); int xp; int yp; for (xp = 0; xp < 32; xp++) { for (yp = 0; yp < 32; yp++) { Color color; float valueFloat; int valueInt; valueFloat = templatesResult[0][xp + yp * 32]; valueFloat *= 100.0f; valueFloat = System.Math.Min(1.0f, valueFloat); valueInt = (int)(valueFloat * 255.0f); color = Color.FromArgb(valueInt, valueInt, valueInt); outputBitmap.SetPixel(xp, yp, color); } } outputBitmap.Save(pathToOutputImages + "template" + "0" + ".png"); } }