|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Library to call models of the family of SAM (Segment Anything Model) from Java |
| 4 | + * %% |
| 5 | + * Copyright (C) 2024 SAMJ developers. |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package ai.nets.samj.annotation; |
| 21 | + |
| 22 | +import java.awt.geom.Point2D; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Comparator; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +public class PolygonToRLE { |
| 29 | + static class Edge { |
| 30 | + double x0, y0, x1, y1; |
| 31 | + double ymin, ymax; |
| 32 | + // slope‐inverse: dx/dy |
| 33 | + double dxdy; |
| 34 | + public Edge(Point2D a, Point2D b) { |
| 35 | + // ensure y0 <= y1 |
| 36 | + if (a.getY() <= b.getY()) { |
| 37 | + x0 = a.getX(); y0 = a.getY(); |
| 38 | + x1 = b.getX(); y1 = b.getY(); |
| 39 | + } else { |
| 40 | + x0 = b.getX(); y0 = b.getY(); |
| 41 | + x1 = a.getX(); y1 = a.getY(); |
| 42 | + } |
| 43 | + ymin = y0; ymax = y1; |
| 44 | + dxdy = (x1 - x0) / (y1 - y0); |
| 45 | + } |
| 46 | + /** x‐intersection at scanline y+0.5 */ |
| 47 | + public double intersectX(double scanY) { |
| 48 | + return x0 + dxdy * (scanY - y0); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param contour Must be closed (first==last) or will be treated as such. |
| 54 | + * @param width Image width in pixels. |
| 55 | + * @param height Image height in pixels. |
| 56 | + * @return COCO‐style RLE: alternating background/foreground run lengths. |
| 57 | + */ |
| 58 | + public static List<Integer> contourToRLE( |
| 59 | + List<Point2D> contour, int width, int height) { |
| 60 | + // 1) Build edge table |
| 61 | + List<Edge> edges = new ArrayList<>(); |
| 62 | + for (int i = 0; i < contour.size() - 1; i++) { |
| 63 | + Point2D a = contour.get(i), b = contour.get(i+1); |
| 64 | + if (!a.equals(b) && a.getY() != b.getY()) { |
| 65 | + edges.add(new Edge(a, b)); |
| 66 | + } |
| 67 | + } |
| 68 | + // Sort edges by ymin for activation |
| 69 | + edges.sort(Comparator.comparingDouble(e -> e.ymin)); |
| 70 | + |
| 71 | + List<Integer> runs = new ArrayList<>(); |
| 72 | + int currentVal = 0, currentRun = 0; |
| 73 | + // Active edges for this scan‐line |
| 74 | + List<Edge> active = new ArrayList<>(); |
| 75 | + int ei = 0; // pointer into edges |
| 76 | + |
| 77 | + // 2) For each scan‐line |
| 78 | + for (int y = 0; y < height; y++) { |
| 79 | + double scanY = y + 0.5; |
| 80 | + |
| 81 | + // Activate edges whose ymin <= scanY < ymax |
| 82 | + while (ei < edges.size() && edges.get(ei).ymin <= scanY) { |
| 83 | + if (edges.get(ei).ymax > scanY) { |
| 84 | + active.add(edges.get(ei)); |
| 85 | + } |
| 86 | + ei++; |
| 87 | + } |
| 88 | + // Remove edges that end at or below scanY |
| 89 | + active.removeIf(e -> e.ymax <= scanY); |
| 90 | + |
| 91 | + // 3) Compute intersections |
| 92 | + List<Double> xs = new ArrayList<>(active.size()); |
| 93 | + for (Edge e : active) { |
| 94 | + xs.add(e.intersectX(scanY)); |
| 95 | + } |
| 96 | + Collections.sort(xs); |
| 97 | + |
| 98 | + // 4) Walk spans and accumulate RLE |
| 99 | + int prevX = 0; |
| 100 | + for (int i = 0; i+1 < xs.size(); i += 2) { |
| 101 | + int x0 = (int)Math.ceil(xs.get(i)); |
| 102 | + int x1 = (int)Math.floor(xs.get(i+1)); |
| 103 | + if (x1 < x0) continue; |
| 104 | + // background run [prevX, x0) |
| 105 | + currentRun = appendRun(runs, currentVal, currentRun, x0 - prevX); |
| 106 | + currentVal = 1 - currentVal; |
| 107 | + // foreground run [x0, x1+1) |
| 108 | + currentRun = appendRun(runs, currentVal, currentRun, (x1+1) - x0); |
| 109 | + currentVal = 1 - currentVal; |
| 110 | + prevX = x1 + 1; |
| 111 | + } |
| 112 | + // final background run to end of row |
| 113 | + currentRun = appendRun(runs, currentVal, currentRun, width - prevX); |
| 114 | + // ensure we’re back to background for next row |
| 115 | + if (currentVal != 0) { |
| 116 | + currentVal = 0; |
| 117 | + if (currentRun > 0) { |
| 118 | + runs.add(currentRun); |
| 119 | + currentRun = 0; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + // 5) Flush last run |
| 124 | + if (currentRun > 0) { |
| 125 | + runs.add(currentRun); |
| 126 | + } |
| 127 | + return runs; |
| 128 | + } |
| 129 | + |
| 130 | + /** Helper: either extend or flush & start new run. */ |
| 131 | + private static int appendRun(List<Integer> runs, int currVal, int currRun, int length) { |
| 132 | + if (length <= 0) return currRun; |
| 133 | + if (currVal == 0) { |
| 134 | + // extending current background run |
| 135 | + return currRun + length; |
| 136 | + } else { |
| 137 | + // flush previous foreground run |
| 138 | + runs.add(currRun); |
| 139 | + // start new background run of size `length` |
| 140 | + return length; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments