Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

105
LINES

< > BotCompany Repo | #1027204 // Image Recognition for Vector Spike [OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (7207L/38K).

1  
!7
2  
3  
// uses only one prototype image
4  
// goes through different granularities at once
5  
6  
cmodule ImageRecogSpike {
7  
  switchable S prototypeImageID = #1102884;
8  
  switchable S inputImageID = #1102883;
9  
  switchable bool showResizedProtos;
10  
  switchable int numberOfCandidatesToShow = 3;
11  
12  
  // how big do we think the prototype is in the whole image
13  
  // (percentage by height)
14  
  transient double assumedPrototypeHeightPercentage = 70;
15  
  
16  
  transient L<Int> widths = ll(1, 2, 4, 8, 16, 32, 64, 128);
17  
  transient new L<IBWIntegralImage> integralImages;
18  
  transient BWIntegralImage baseImage;
19  
  transient JTabbedPane tabs = jTabbedPane();
20  
  transient BWImage prototypeImage;
21  
  transient new L<OneLevel> levels; // recognizers for each granularity
22  
23  
  class OneLevel extends SteppableAndBest<Rect> {
24  
    IBWIntegralImage ii; // scaled integral image
25  
    BWImage image;       // scaled image
26  
    BWImage prototype;   // scaled prototype
27  
    float minSimilarity = 0.5f;
28  
    ImageSurface is;
29  
    new Map<Rect, Double> allScores;
30  
31  
    // candidates are top-left corner of rect to try in our coordinates
32  
    L<Pt> candidatesQueue = syncLinkedList();
33  
    new Set<Pt> candidatesTried;
34  
    Iterator<Pt> candidatesStream;
35  
36  
    *(IBWIntegralImage *ii) {
37  
      image = iBWIntegralImageToBWImage(ii);
38  
      // get assumed height of prototype in scaled-down image
39  
      int ph = iround(ii.getHeight()*assumedPrototypeHeightPercentage/100.0);
40  
      // resize prototype
41  
      prototype = bwResizeToHeightSmooth(prototypeImage, ph);
42  
      if (showResizedProtos)
43  
        addTab(tabs, "proto " + ii.getWidth(),
44  
          jFullCenterScroll(jPixelatedZoomedImageSurface(4.0, prototype)));
45  
      candidatesStream = mapI rectTopLeftCorner(allSubRectsOfSizeIterator(prototype.getWidth(), prototype.getHeight(),
46  
        imageRect(image)));
47  
    }
48  
    
49  
    public bool step() {
50  
      Pt p = nextCandidate();
51  
      if (p != null) ret true with tryCandidate(p);
52  
      false;
53  
    }
54  
55  
    Pt nextCandidate() {
56  
      try object Pt p = popFirst(candidatesQueue);
57  
      ret nextFromIterator(candidatesStream);
58  
    }
59  
60  
    void tryCandidate(Pt p) {
61  
      if (!candidatesTried.add(p)) ret;
62  
63  
      int x = p.x, y = p.y, wp = prototype.getWidth(), hp = prototype.getHeight();
64  
65  
      float maxError = (1f-minSimilarity)*wp*hp;
66  
      float diff = bwImageSectionsSimilarity(image, prototype, x, y, maxError);
67  
      if (diff <= maxError)
68  
        putInBestAndMap(best, allScores, new Rect(x, y, wp, hp), 1-diff/(wp*hp);
69  
    }
70  
71  
    void showBest() {
72  
      overlaySelectionsOnImageSurface(is, topTenKeysByValue(numberOfCandidatesToShow, allScores));
73  
    }
74  
  }
75  
  
76  
  start-thread {
77  
    dm_watchField numberOfCandidatesToShow(r showBest);
78  
    if (baseImage == null)
79  
      baseImage = BWIntegralImage(loadImage2(inputImageID));
80  
    if (prototypeImage == null)
81  
      prototypeImage = loadBWImage(prototypeImageID);
82  
    for (int w : widths) {
83  
      IBWIntegralImage ii = scaledIBWIntegralImage(baseImage, w);
84  
      integralImages.add(ii);
85  
      ImageSurface is = jPixelatedZoomedImageSurface(
86  
        doubleRatio(baseImage.getWidth(), w), iBWIntegralImageToBWImage(ii));
87  
      addTab(tabs, "w=" + w, jFullCenterScroll(is));
88  
      levels.add(setAll(new OneLevel(ii), +is));
89  
    }
90  
    addTab(tabs, "Prototype", jFullCenterScroll(jPixelatedZoomedImageSurface(4, prototypeImage)));
91  
92  
    time "Process" {
93  
      print("Steps: " + stepAll_roundRobin(levels));
94  
    }
95  
96  
    showBest();
97  
  }
98  
99  
  void showBest {  
100  
    for (OneLevel l : levels) l.showBest();
101  
  }
102  
103  
  visual withCenteredButtons(tabs,
104  
    withLabel("# of candidates to show:", dm_fieldSpinner numberOfCandidatesToShow(1, 100)));
105  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027204
Snippet name: Image Recognition for Vector Spike [OK]
Eternal ID of this version: #1027204/36
Text MD5: e9a37af2f45026bea78e36dc56b10f90
Transpilation MD5: 6f6e9ce84fe232a6790005dbf28b1e94
Author: stefan
Category: javax / image recognition
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-28 16:34:17
Source code size: 3720 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 253 / 3831
Version history: 35 change(s)
Referenced in: [show references]