Skip to content

Commit 0458988

Browse files
committed
refactor: refactored solver into own class
1 parent bb8f886 commit 0458988

File tree

2 files changed

+281
-229
lines changed

2 files changed

+281
-229
lines changed
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
/**
2+
* Copyright 2019 The SymVision authors
3+
*
4+
* 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:
5+
*
6+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
*
8+
* 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.
9+
*/
10+
package ptrman.bpsolver;
11+
12+
import ptrman.Datastructures.Dag;
13+
import ptrman.Datastructures.IMap2d;
14+
import ptrman.Datastructures.Vector2d;
15+
import ptrman.Gui.IImageDrawer;
16+
import ptrman.Showcases.TestClustering;
17+
import ptrman.bindingNars.NarsBinding;
18+
import ptrman.bindingNars.OpenNarsNarseseConsumer;
19+
import ptrman.levels.retina.*;
20+
import ptrman.levels.retina.helper.ProcessConnector;
21+
import ptrman.levels.visual.*;
22+
import ptrman.visualizationTests.VisualizeLinesegmentsAnnealing;
23+
24+
import java.awt.image.BufferedImage;
25+
26+
/**
27+
* new solver which is incomplete but should be in a working state
28+
*/
29+
public class Solver2 {
30+
public ProcessD processD;
31+
public ProcessD[] processDEdge;
32+
public ProcessConnector<ProcessA.Sample> connectorSamplesForEndosceleton;
33+
public ProcessConnector<RetinaPrimitive> connectorDetectorsEndosceletonFromProcessD;
34+
35+
public ProcessConnector<ProcessA.Sample>[] connectorSamplesFromProcessAForEdge;
36+
public ProcessConnector<RetinaPrimitive>[] connectorDetectorsFromProcessDForEdge;
37+
38+
public NarsBinding narsBinding;
39+
40+
public int annealingStep = 0;
41+
42+
// image drawer which is used as the source of the images, must be set to a image drawer before the solver is used!
43+
public IImageDrawer imageDrawer;
44+
45+
public Solver2() {
46+
processD = new ProcessD();
47+
processD.maximalDistanceOfPositions = 5000.0;
48+
processD.onlyEndoskeleton = true;
49+
50+
connectorSamplesForEndosceleton = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
51+
52+
{ // create NARS-binding
53+
narsBinding = new NarsBinding(new OpenNarsNarseseConsumer());
54+
}
55+
}
56+
57+
/**
58+
* must be called before the frame method family
59+
*/
60+
public void preFrame() {
61+
annealingStep = 0;
62+
63+
BufferedImage image = imageDrawer.apply(null);
64+
65+
Vector2d<Integer> imageSize = new Vector2d<>(image.getWidth(), image.getHeight());
66+
67+
68+
IMap2d<ColorRgb> mapColor = TestClustering.translateFromImageToMap(image);
69+
70+
71+
72+
73+
74+
// setup the processing chain
75+
76+
VisualProcessor.ProcessingChain processingChain = new VisualProcessor.ProcessingChain();
77+
78+
Dag.Element newDagElement = new Dag.Element(
79+
new VisualProcessor.ProcessingChain.ChainElementColorFloat(
80+
new VisualProcessor.ProcessingChain.ConvertColorRgbToGrayscaleFilter(new ColorRgb(1.0f, 1.0f, 1.0f)),
81+
"convertRgbToGrayscale",
82+
imageSize
83+
)
84+
);
85+
//newDagElement.childIndices.add(1);
86+
87+
processingChain.filterChainDag.elements.add(newDagElement);
88+
89+
/* commented because we don't dither
90+
newDagElement = new Dag.Element(
91+
new VisualProcessor.ProcessingChain.ChainElementFloatBoolean(
92+
new VisualProcessor.ProcessingChain.DitheringFilter(),
93+
"dither",
94+
imageSize
95+
)
96+
);
97+
98+
processingChain.filterChainDag.elements.add(newDagElement);
99+
*/
100+
101+
102+
processingChain.filterChain(mapColor);
103+
104+
IMap2d<Float> mapGrayscale = ((VisualProcessor.ProcessingChain.ApplyChainElement) processingChain.filterChainDag.elements.get(processingChain.filterChainDag.elements.size()-1).content).result; // get from last element in the chain
105+
106+
int numberOfEdgeDetectorDirections = 8;
107+
108+
// convolution
109+
Map2dApplyConvolution[] edgeDetectors = new Map2dApplyConvolution[numberOfEdgeDetectorDirections];
110+
for(int i=0; i<numberOfEdgeDetectorDirections;i++) {
111+
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));
112+
}
113+
114+
IMap2d<Float>[] edges = new IMap2d[numberOfEdgeDetectorDirections];
115+
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // detect edges with filters
116+
edges[i] = edgeDetectors[i].process(mapGrayscale);
117+
}
118+
119+
ProcessA[] processAEdge = new ProcessA[numberOfEdgeDetectorDirections];
120+
processDEdge = new ProcessD[numberOfEdgeDetectorDirections];
121+
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // create processors for edges
122+
processAEdge[i] = new ProcessA();
123+
processDEdge[i] = new ProcessD();
124+
processDEdge[i].maximalDistanceOfPositions = 5000.0;
125+
}
126+
127+
connectorDetectorsFromProcessDForEdge = new ProcessConnector[numberOfEdgeDetectorDirections];
128+
connectorSamplesFromProcessAForEdge = new ProcessConnector[numberOfEdgeDetectorDirections];
129+
for(int i=0; i<numberOfEdgeDetectorDirections;i++) { // create connectors for edges
130+
connectorDetectorsFromProcessDForEdge[i] = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
131+
connectorSamplesFromProcessAForEdge[i] = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
132+
}
133+
134+
for(int i=0; i<numberOfEdgeDetectorDirections;i++) {
135+
// copy image because processA changes the image
136+
137+
IMap2d<Boolean> mapBoolean = Map2dBinary.threshold(edges[i], 0.01f); // convert from edges[0]
138+
139+
ProcessConnector<ProcessA.Sample> connectorSamplesFromProcessA = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
140+
processAEdge[i].set(mapBoolean.copy(), null, connectorSamplesFromProcessAForEdge[i]);
141+
142+
processAEdge[i].setImageSize(imageSize);
143+
processAEdge[i].setup();
144+
145+
processDEdge[i].setImageSize(imageSize);
146+
processDEdge[i].set(connectorSamplesFromProcessAForEdge[i], connectorDetectorsFromProcessDForEdge[i]);
147+
148+
processAEdge[i].preProcessData();
149+
processAEdge[i].processData(0.03f);
150+
processAEdge[i].postProcessData();
151+
152+
processDEdge[i].preProcessData();
153+
processDEdge[i].processData(1.0f);
154+
processDEdge[i].postProcessData();
155+
}
156+
157+
IMap2d<Boolean> mapBoolean = Map2dBinary.threshold(mapGrayscale, 0.1f); // convert from edges[0]
158+
159+
ProcessZFacade processZFacade = new ProcessZFacade();
160+
161+
final int processzNumberOfPixelsToMagnifyThreshold = 8;
162+
163+
final int processZGridsize = 8;
164+
165+
connectorSamplesForEndosceleton.workspace.clear();
166+
processD.annealedCandidates.clear(); // TODO< cleanup in process with method >
167+
168+
processZFacade.setImageSize(imageSize);
169+
processZFacade.preSetupSet(processZGridsize, processzNumberOfPixelsToMagnifyThreshold);
170+
processZFacade.setup();
171+
172+
processZFacade.set(mapBoolean); // image doesn't need to be copied
173+
174+
processZFacade.preProcessData();
175+
processZFacade.processData();
176+
processZFacade.postProcessData();
177+
178+
IMap2d<Integer> notMagnifiedOutputObjectIdsMapDebug = processZFacade.getNotMagnifiedOutputObjectIds();
179+
180+
ProcessA processA = new ProcessA();
181+
ProcessB processB = new ProcessB();
182+
ProcessC processC = new ProcessC();
183+
184+
// copy image because processA changes the image
185+
ProcessConnector<ProcessA.Sample> connectorSamplesFromProcessA = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
186+
processA.set(mapBoolean.copy(), processZFacade.getNotMagnifiedOutputObjectIds(), connectorSamplesFromProcessA);
187+
processA.setImageSize(imageSize);
188+
processA.setup();
189+
190+
ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessB = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
191+
processB.set(mapBoolean.copy(), connectorSamplesFromProcessA, conntrSamplesFromProcessB);
192+
processB.setImageSize(imageSize);
193+
processB.setup();
194+
195+
196+
ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessC0 = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
197+
ProcessConnector<ProcessA.Sample> conntrSamplesFromProcessC1 = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
198+
processC.set(conntrSamplesFromProcessB, conntrSamplesFromProcessC0, conntrSamplesFromProcessC1);
199+
processC.setImageSize(imageSize);
200+
processC.setup();
201+
202+
connectorDetectorsEndosceletonFromProcessD = ProcessConnector.createWithDefaultQueues(ProcessConnector.EnumMode.WORKSPACE);
203+
204+
processD.setImageSize(imageSize);
205+
connectorSamplesForEndosceleton = conntrSamplesFromProcessC0;
206+
processD.set(connectorSamplesForEndosceleton, connectorDetectorsEndosceletonFromProcessD);
207+
208+
processA.preProcessData();
209+
processA.processData(0.03f);
210+
processA.postProcessData();
211+
212+
processB.preProcessData();
213+
processB.processData();
214+
processB.postProcessData();
215+
216+
processC.preProcessData();
217+
processC.processData();
218+
processC.postProcessData();
219+
220+
processD.preProcessData();
221+
processD.processData(1.0f);
222+
processD.postProcessData();
223+
}
224+
225+
/**
226+
* does one processing step for the processing of the frame
227+
*/
228+
public void frameStep() {
229+
// do annealing step of process D
230+
231+
processD.sampleNew();
232+
processD.tryWiden();
233+
processD.sortByActivationAndThrowAway();
234+
235+
for(int idx=0;idx<processDEdge.length;idx++) {
236+
processDEdge[idx].sampleNew();
237+
processDEdge[idx].tryWiden();
238+
processDEdge[idx].sortByActivationAndThrowAway();
239+
}
240+
241+
if (annealingStep >= 20) { // remove only in later phases
242+
processD.removeCandidatesBelowActivation(1.1);
243+
244+
for(int idx=0;idx<processDEdge.length;idx++) {
245+
processDEdge[idx].removeCandidatesBelowActivation(1.1);
246+
}
247+
}
248+
249+
annealingStep++;
250+
}
251+
252+
/**
253+
* must be called to "finilize" the processing of a frame
254+
*/
255+
public void postFrame() {
256+
// then emit narsese to narsese consumer
257+
258+
processD.commitLineDetectors(); // split line detectors into "real" primitives
259+
260+
for(int idx=0;idx<processDEdge.length;idx++) {
261+
processDEdge[idx].commitLineDetectors();
262+
}
263+
264+
narsBinding.emitRetinaPrimitives(connectorDetectorsEndosceletonFromProcessD.workspace); // emit all collected primitives from process D
265+
}
266+
}

0 commit comments

Comments
 (0)