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

110
LINES

< > BotCompany Repo | #1024537 // Image Segmenter v2 [with size and position filtering and blur and distance to color]

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

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

1  
!7
2  
3  
cmodule ImageSegmenter > DynImageSurface {
4  
  // parameters
5  
  int blur;
6  
  bool distanceFromColor;
7  
  int dc_r, dc_g, dc_b;
8  
  float dc_gain = 5f, maxGain = 25f;
9  
  float contrastThreshold = 0.5f;
10  
  int gridSize = 2;
11  
  DoubleRange widthRange = new(0, 1);
12  
  DoubleRange heightRange = new(0, 1);
13  
  DoubleRange xRange = new(0, 1);
14  
  DoubleRange yRange = new(0, 1);
15  
  
16  
  // output
17  
  L<Rect> segments;
18  
  
19  
  transient BufferedImage blurredImage;
20  
  transient JLabel lblFound;
21  
  transient ReliableSingleThread rst = dm_rst(this, r segmentIt);
22  
  
23  
  visualize {
24  
    JSlider slider = main.onChange(jLiveValueSlider(dm_fieldLiveValue('contrastThreshold)), rst);
25  
    JSpinner spinner = main.onChange(dm_spinner gridSize(2, 10), rst);
26  
    JSpinner blurSpinner = main.onChange(dm_spinner blur(0, 10), rst);
27  
    
28  
    afterwards {
29  
      imageSurfaceOnHover(imageSurface, voidfunc(Pt p) {
30  
        RGB rgb = bufferedImage_getRGB(blurredImage, p);
31  
        setToolTip(imageSurface, rgb == null ? null : "Color: " + rgbToIntTriple(rgb));
32  
      });
33  
      updateLblFound(); rst.trigger();
34  
    }
35  
    ret withCenteredLinesBelow(super.visualize(),
36  
      ll(jCenteredSection(" Preprocessing ", withSideMargin(20, jline(
37  
        withLabel("Blur:", blurSpinner),
38  
        main.onChange(dm_checkBox("Distance from color:", 'distanceFromColor), rst),
39  
        withLabel("R:", main.onChange(dm_spinner dc_r(0, 255), rst)),
40  
        withLabel("G:", main.onChange(dm_spinner dc_g(0, 255), rst)),
41  
        withLabel("B:", main.onChange(dm_spinner dc_b(0, 255), rst)),
42  
        withLabel("Gain:", main.onChange(jMinWidth(100, jLiveValueSliderZeroToX(maxGain, dm_fieldLiveValue('dc_gain))), rst)))))),
43  
      ll(jCenteredSection(" Segmenting ", withSideMargin(20, jline(
44  
        withLabel("Contrast Threshold:", jMinWidth(100, slider)),
45  
        withLabel("Grid size:", spinner),
46  
        jbutton("Segment", rst)))),
47  
        jCenteredSection(" Result ", withSideMargin(20, lblFound = jboldlabel(" "))),
48  
        jVerticalCenter(jPopDownButton_noText(
49  
          "Show segments", rThread showSegments
50  
        ))),
51  
      ll(
52  
        withLabel("Width range:", main.onChange(doubleRangeLiveValueSlider(dm_fieldLiveValue('widthRange)), rst)),
53  
        withLabel("Height range:", main.onChange(doubleRangeLiveValueSlider(dm_fieldLiveValue('heightRange)), rst))),
54  
      ll(
55  
        withLabel("X position:", main.onChange(doubleRangeLiveValueSlider(dm_fieldLiveValue('xRange)), rst)),
56  
        withLabel("Y position:", main.onChange(doubleRangeLiveValueSlider(dm_fieldLiveValue('yRange)), rst))));
57  
  }
58  
  
59  
  start {
60  
    if (!hasImage()) setImage(whiteImage(100, 100));
61  
    dm_vmBus_onMessage('newClipboardContents, voidfunc(O o) {
62  
      if (o cast BufferedImage) thread { setImage(o); rst.trigger(); }
63  
    });
64  
  }
65  
  
66  
  void segmentIt enter {
67  
    if (!hasImage()) ret;
68  
    int w = imageWidth(), h = imageHeight();
69  
    
70  
    BufferedImage image = getImage();
71  
    BufferedImage imageToDraw = null;
72  
    if (blur != 0)
73  
      image = imageToDraw = new BoxBlurFilter(blur).filter(image, null);
74  
    blurredImage = image;
75  
    
76  
    new AutoSegmenter as;
77  
    as.g = gridSize;
78  
    as.contrastThreshold = contrastThreshold;
79  
    BWImage bw;
80  
    if (distanceFromColor) {
81  
      bw = img_distanceFromColor_withGain(image, rgbFromInts(dc_r, dc_g, dc_b), dc_gain);
82  
      imageToDraw = bw.getBufferedImage();
83  
    } else
84  
      bw = BWImage(image);
85  
    L<Rect> segments = as.go(bw);
86  
    IntRange wr = doubleToIntRange_endPlus1(w, widthRange);
87  
    IntRange hr = doubleToIntRange_endPlus1(h, heightRange);
88  
    IntRange xr = doubleToIntRange_endPlus1(w, xRange);
89  
    IntRange yr = doubleToIntRange_endPlus1(h, yRange);
90  
    printVars_str(+wr, +hr, +xr, +yr);
91  
    segments = filterRectsByWidthRange(segments, wr);
92  
    segments = filterRectsByHeightRange(segments, hr);
93  
    segments = filterRectsByCenterXRange(segments, xr);
94  
    segments = filterRectsByCenterYRange(segments, yr);
95  
    setField(+segments);
96  
    updateLblFound();
97  
    imageSurface.imageToDraw = imageToDraw;
98  
    overlaySelectionsOnImageSurface(imageSurface, segments);
99  
  }
100  
  
101  
  void showSegments {
102  
    L<Rect> segments = this.segments;
103  
    showImage_centered(n2(segments, "segment") + " found",
104  
      mergeBufferedImagesVertically(map(segments, r -> clipBufferedImage(getImage(), r))));
105  
  }
106  
  
107  
  void updateLblFound() {
108  
    setText(lblFound, nSegments(segments) + " found");
109  
  }
110  
}

Author comment

Began life as a copy of #1018876

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1024537
Snippet name: Image Segmenter v2 [with size and position filtering and blur and distance to color]
Eternal ID of this version: #1024537/48
Text MD5: 9e7a7263de1bff0cae6ad4c56ba0d8e1
Transpilation MD5: 7a3933eff461234a65aede11a759befa
Author: stefan
Category: javax / image analysis
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-18 15:01:42
Source code size: 4465 bytes / 110 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 873
Version history: 47 change(s)
Referenced in: [show references]