Skip to content

Commit

Permalink
refactor: refactored solver into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
PtrMan committed Nov 20, 2019
1 parent bb8f886 commit 0458988
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 229 deletions.
266 changes: 266 additions & 0 deletions src/main/java/ptrman/bpsolver/Solver2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/**
* Copyright 2019 The SymVision authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ptrman.bpsolver;

import ptrman.Datastructures.Dag;
import ptrman.Datastructures.IMap2d;
import ptrman.Datastructures.Vector2d;
import ptrman.Gui.IImageDrawer;
import ptrman.Showcases.TestClustering;
import ptrman.bindingNars.NarsBinding;
import ptrman.bindingNars.OpenNarsNarseseConsumer;
import ptrman.levels.retina.*;
import ptrman.levels.retina.helper.ProcessConnector;
import ptrman.levels.visual.*;
import ptrman.visualizationTests.VisualizeLinesegmentsAnnealing;

import java.awt.image.BufferedImage;

/**
* new solver which is incomplete but should be in a working state
*/
public class Solver2 {
public ProcessD processD;
public ProcessD[] processDEdge;
public ProcessConnector<ProcessA.Sample> connectorSamplesForEndosceleton;
public ProcessConnector<RetinaPrimitive> connectorDetectorsEndosceletonFromProcessD;

public ProcessConnector<ProcessA.Sample>[] connectorSamplesFromProcessAForEdge;
public ProcessConnector<RetinaPrimitive>[] connectorDetectorsFromProcessDForEdge;

public NarsBinding narsBinding;

public int annealingStep = 0;

// image drawer which is used as the source of the images, must be set to a image drawer before the solver is used!
public IImageDrawer imageDrawer;

public Solver2() {
processD = new ProcessD();
processD.maximalDistanceOfPositions = 5000.0;
processD.onlyEndoskeleton = true;

connectorSamplesForEndosceleton = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);

{ // create NARS-binding
narsBinding = new NarsBinding(new OpenNarsNarseseConsumer());
}
}

/**
* must be called before the frame method family
*/
public void preFrame() {
annealingStep = 0;

BufferedImage image = imageDrawer.apply(null);

Vector2d<Integer> imageSize = new Vector2d<>(image.getWidth(), image.getHeight());


IMap2d<ColorRgb> mapColor = TestClustering.translateFromImageToMap(image);





// setup the processing chain

VisualProcessor.ProcessingChain processingChain = new VisualProcessor.ProcessingChain();

Dag.Element newDagElement = new Dag.Element(
new VisualProcessor.ProcessingChain.ChainElementColorFloat(
new VisualProcessor.ProcessingChain.ConvertColorRgbToGrayscaleFilter(new ColorRgb(1.0f, 1.0f, 1.0f)),
"convertRgbToGrayscale",
imageSize
)
);
//newDagElement.childIndices.add(1);

processingChain.filterChainDag.elements.add(newDagElement);

/* commented because we don't dither
newDagElement = new Dag.Element(
new VisualProcessor.ProcessingChain.ChainElementFloatBoolean(
new VisualProcessor.ProcessingChain.DitheringFilter(),
"dither",
imageSize
)
);
processingChain.filterChainDag.elements.add(newDagElement);
*/


processingChain.filterChain(mapColor);

IMap2d<Float> mapGrayscale = ((VisualProcessor.ProcessingChain.ApplyChainElement) processingChain.filterChainDag.elements.get(processingChain.filterChainDag.elements.size()-1).content).result; // get from last element in the chain

int numberOfEdgeDetectorDirections = 8;

// convolution
Map2dApplyConvolution[] edgeDetectors = new Map2dApplyConvolution[numberOfEdgeDetectorDirections];
for(int i=0; i<numberOfEdgeDetectorDirections;i++) {
edgeDetectors[i] = new Map2dApplyConvolution(Convolution2dHelper.calcGaborKernel(8, (float)i/(float)numberOfEdgeDetectorDirections * 2.0f * (float)Math.PI, 10.0f/64.0f, (float)Math.PI*0.5f, 0.4f));
}

IMap2d<Float>[] edges = new IMap2d[numberOfEdgeDetectorDirections];
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // detect edges with filters
edges[i] = edgeDetectors[i].process(mapGrayscale);
}

ProcessA[] processAEdge = new ProcessA[numberOfEdgeDetectorDirections];
processDEdge = new ProcessD[numberOfEdgeDetectorDirections];
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // create processors for edges
processAEdge[i] = new ProcessA();
processDEdge[i] = new ProcessD();
processDEdge[i].maximalDistanceOfPositions = 5000.0;
}

connectorDetectorsFromProcessDForEdge = new ProcessConnector[numberOfEdgeDetectorDirections];
connectorSamplesFromProcessAForEdge = new ProcessConnector[numberOfEdgeDetectorDirections];
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // create connectors for edges
connectorDetectorsFromProcessDForEdge[i] = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
connectorSamplesFromProcessAForEdge[i] = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
}

for(int i=0; i<numberOfEdgeDetectorDirections;i++) {
// copy image because processA changes the image

IMap2d<Boolean> mapBoolean = Map2dBinary.threshold(edges[i], 0.01f); // convert from edges[0]

ProcessConnector<ProcessA.Sample> connectorSamplesFromProcessA = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
processAEdge[i].set(mapBoolean.copy(), null, connectorSamplesFromProcessAForEdge[i]);

processAEdge[i].setImageSize(imageSize);
processAEdge[i].setup();

processDEdge[i].setImageSize(imageSize);
processDEdge[i].set(connectorSamplesFromProcessAForEdge[i], connectorDetectorsFromProcessDForEdge[i]);

processAEdge[i].preProcessData();
processAEdge[i].processData(0.03f);
processAEdge[i].postProcessData();

processDEdge[i].preProcessData();
processDEdge[i].processData(1.0f);
processDEdge[i].postProcessData();
}

IMap2d<Boolean> mapBoolean = Map2dBinary.threshold(mapGrayscale, 0.1f); // convert from edges[0]

ProcessZFacade processZFacade = new ProcessZFacade();

final int processzNumberOfPixelsToMagnifyThreshold = 8;

final int processZGridsize = 8;

connectorSamplesForEndosceleton.workspace.clear();
processD.annealedCandidates.clear(); // TODO< cleanup in process with method >

processZFacade.setImageSize(imageSize);
processZFacade.preSetupSet(processZGridsize, processzNumberOfPixelsToMagnifyThreshold);
processZFacade.setup();

processZFacade.set(mapBoolean); // image doesn't need to be copied

processZFacade.preProcessData();
processZFacade.processData();
processZFacade.postProcessData();

IMap2d<Integer> notMagnifiedOutputObjectIdsMapDebug = processZFacade.getNotMagnifiedOutputObjectIds();

ProcessA processA = new ProcessA();
ProcessB processB = new ProcessB();
ProcessC processC = new ProcessC();

// copy image because processA changes the image
ProcessConnector<ProcessA.Sample> connectorSamplesFromProcessA = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
processA.set(mapBoolean.copy(), processZFacade.getNotMagnifiedOutputObjectIds(), connectorSamplesFromProcessA);
processA.setImageSize(imageSize);
processA.setup();

ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessB = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
processB.set(mapBoolean.copy(), connectorSamplesFromProcessA, conntrSamplesFromProcessB);
processB.setImageSize(imageSize);
processB.setup();


ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessC0 = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessC1 = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
processC.set(conntrSamplesFromProcessB, conntrSamplesFromProcessC0, conntrSamplesFromProcessC1);
processC.setImageSize(imageSize);
processC.setup();

connectorDetectorsEndosceletonFromProcessD = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);

processD.setImageSize(imageSize);
connectorSamplesForEndosceleton = conntrSamplesFromProcessC0;
processD.set(connectorSamplesForEndosceleton, connectorDetectorsEndosceletonFromProcessD);

processA.preProcessData();
processA.processData(0.03f);
processA.postProcessData();

processB.preProcessData();
processB.processData();
processB.postProcessData();

processC.preProcessData();
processC.processData();
processC.postProcessData();

processD.preProcessData();
processD.processData(1.0f);
processD.postProcessData();
}

/**
* does one processing step for the processing of the frame
*/
public void frameStep() {
// do annealing step of process D

processD.sampleNew();
processD.tryWiden();
processD.sortByActivationAndThrowAway();

for(int idx=0;idx<processDEdge.length;idx++) {
processDEdge[idx].sampleNew();
processDEdge[idx].tryWiden();
processDEdge[idx].sortByActivationAndThrowAway();
}

if (annealingStep >= 20) { // remove only in later phases
processD.removeCandidatesBelowActivation(1.1);

for(int idx=0;idx<processDEdge.length;idx++) {
processDEdge[idx].removeCandidatesBelowActivation(1.1);
}
}

annealingStep++;
}

/**
* must be called to "finilize" the processing of a frame
*/
public void postFrame() {
// then emit narsese to narsese consumer

processD.commitLineDetectors(); // split line detectors into "real" primitives

for(int idx=0;idx<processDEdge.length;idx++) {
processDEdge[idx].commitLineDetectors();
}

narsBinding.emitRetinaPrimitives(connectorDetectorsEndosceletonFromProcessD.workspace); // emit all collected primitives from process D
}
}
Loading

0 comments on commit 0458988

Please sign in to comment.