其他分享
首页 > 其他分享> > tracingBorders

tracingBorders

作者:互联网

边界追踪。。

    public static List<Border> tracingBorders(double[][] S0, double[] X, double[] Y, int[][] S1, double undefData) {
        List<BorderLine> borderLines = new ArrayList();
        int m = S0.length;
        int n = S0[0].length;

        int i;
        int j;
        for(i = 0; i < m; ++i) {
            for(j = 0; j < n; ++j) {
                if (doubleEquals(S0[i][j], undefData)) {
                    S1[i][j] = 0;
                } else {
                    S1[i][j] = 1;
                }
            }
        }

        int l;
        int r;
        int b;
        int t;
        int lb;
        int rb;
        int lt;
        int rt;
        for(i = 1; i < m - 1; ++i) {
            for(j = 1; j < n - 1; ++j) {
                if (S1[i][j] == 1) {
                    l = S1[i][j - 1];
                    r = S1[i][j + 1];
                    b = S1[i - 1][j];
                    t = S1[i + 1][j];
                    lb = S1[i - 1][j - 1];
                    rb = S1[i - 1][j + 1];
                    lt = S1[i + 1][j - 1];
                    rt = S1[i + 1][j + 1];
                    if (l > 0 && r > 0 && b > 0 && t > 0 && lb > 0 && rb > 0 && lt > 0 && rt > 0) {
                        S1[i][j] = 2;
                    }

                    if (l + r + b + t + lb + rb + lt + rt <= 2) {
                        S1[i][j] = 0;
                    }
                }
            }
        }

        boolean isContinue;
        do {
            isContinue = false;

            for(i = 1; i < m - 1; ++i) {
                for(j = 1; j < n - 1; ++j) {
                    if (S1[i][j] == 1) {
                        l = S1[i][j - 1];
                        r = S1[i][j + 1];
                        b = S1[i - 1][j];
                        t = S1[i + 1][j];
                        lb = S1[i - 1][j - 1];
                        rb = S1[i - 1][j + 1];
                        lt = S1[i + 1][j - 1];
                        rt = S1[i + 1][j + 1];
                        if (l == 0 && r == 0 || b == 0 && t == 0) {
                            S1[i][j] = 0;
                            isContinue = true;
                        }

                        if (lt == 0 && r == 0 && b == 0 || rt == 0 && l == 0 && b == 0 || lb == 0 && r == 0 && t == 0 || rb == 0 && l == 0 && t == 0) {
                            S1[i][j] = 0;
                            isContinue = true;
                        }
                    }
                }
            }
        } while(isContinue);

        for(j = 0; j < n; ++j) {
            if (S1[0][j] == 1) {
                if (S1[1][j] == 0) {
                    S1[0][j] = 0;
                } else if (j == 0) {
                    if (S1[0][j + 1] == 0) {
                        S1[0][j] = 0;
                    }
                } else if (j == n - 1) {
                    if (S1[0][n - 2] == 0) {
                        S1[0][j] = 0;
                    }
                } else if (S1[0][j - 1] == 0 && S1[0][j + 1] == 0) {
                    S1[0][j] = 0;
                }
            }

            if (S1[m - 1][j] == 1) {
                if (S1[m - 2][j] == 0) {
                    S1[m - 1][j] = 0;
                } else if (j == 0) {
                    if (S1[m - 1][j + 1] == 0) {
                        S1[m - 1][j] = 0;
                    }
                } else if (j == n - 1) {
                    if (S1[m - 1][n - 2] == 0) {
                        S1[m - 1][j] = 0;
                    }
                } else if (S1[m - 1][j - 1] == 0 && S1[m - 1][j + 1] == 0) {
                    S1[m - 1][j] = 0;
                }
            }
        }

        for(i = 0; i < m; ++i) {
            if (S1[i][0] == 1) {
                if (S1[i][1] == 0) {
                    S1[i][0] = 0;
                } else if (i == 0) {
                    if (S1[i + 1][0] == 0) {
                        S1[i][0] = 0;
                    }
                } else if (i == m - 1) {
                    if (S1[m - 2][0] == 0) {
                        S1[i][0] = 0;
                    }
                } else if (S1[i - 1][0] == 0 && S1[i + 1][0] == 0) {
                    S1[i][0] = 0;
                }
            }

            if (S1[i][n - 1] == 1) {
                if (S1[i][n - 2] == 0) {
                    S1[i][n - 1] = 0;
                } else if (i == 0) {
                    if (S1[i + 1][n - 1] == 0) {
                        S1[i][n - 1] = 0;
                    }
                } else if (i == m - 1) {
                    if (S1[m - 2][n - 1] == 0) {
                        S1[i][n - 1] = 0;
                    }
                } else if (S1[i - 1][n - 1] == 0 && S1[i + 1][n - 1] == 0) {
                    S1[i][n - 1] = 0;
                }
            }
        }

        int[][] S2 = new int[m + 2][n + 2];

        for(i = 0; i < m + 2; ++i) {
            for(j = 0; j < n + 2; ++j) {
                if (i != 0 && i != m + 1) {
                    if (j != 0 && j != n + 1) {
                        S2[i][j] = S1[i - 1][j - 1];
                    } else {
                        S2[i][j] = 0;
                    }
                } else {
                    S2[i][j] = 0;
                }
            }
        }

        int[][] UNum = new int[m + 2][n + 2];

        for(i = 0; i < m + 2; ++i) {
            for(j = 0; j < n + 2; ++j) {
                if (S2[i][j] != 1) {
                    UNum[i][j] = 0;
                } else {
                    l = S2[i][j - 1];
                    r = S2[i][j + 1];
                    b = S2[i - 1][j];
                    t = S2[i + 1][j];
                    lb = S2[i - 1][j - 1];
                    rb = S2[i - 1][j + 1];
                    lt = S2[i + 1][j - 1];
                    rt = S2[i + 1][j + 1];
                    if (l != 1 || r != 1 || b != 1 || t != 1 || (lb != 0 || rt != 0) && (rb != 0 || lt != 0)) {
                        UNum[i][j] = 1;
                    } else {
                        UNum[i][j] = 2;
                    }
                }
            }
        }

        int i3 = 0;
        int j3 = 0;

        PointD aPoint;
        for(i = 1; i < m + 1; ++i) {
            for(j = 1; j < n + 1; ++j) {
                if (S2[i][j] == 1) {
                    List<PointD> pointList = new ArrayList();
                    List<IJPoint> ijPList = new ArrayList();
                    aPoint = new PointD();
                    aPoint.X = X[j - 1];
                    aPoint.Y = Y[i - 1];
                    IJPoint aijPoint = new IJPoint();
                    aijPoint.I = i - 1;
                    aijPoint.J = j - 1;
                    pointList.add(aPoint);
                    ijPList.add(aijPoint);
                    int sI = i;
                    int sJ = j;
                    int i2 = i;
                    int j2 = j;
                    int i1 = i;
                    int j1 = -1;

                    do {
                        int[] ij3 = new int[]{i3, j3};
                        if (!traceBorder(S2, i1, i2, j1, j2, ij3)) {
                            break;
                        }

                        i3 = ij3[0];
                        j3 = ij3[1];
                        i1 = i2;
                        j1 = j2;
                        i2 = i3;
                        j2 = j3;
                        --UNum[i3][j3];
                        if (UNum[i3][j3] == 0) {
                            S2[i3][j3] = 3;
                        }

                        aPoint = new PointD();
                        aPoint.X = X[j3 - 1];
                        aPoint.Y = Y[i3 - 1];
                        aijPoint = new IJPoint();
                        aijPoint.I = i3 - 1;
                        aijPoint.J = j3 - 1;
                        pointList.add(aPoint);
                        ijPList.add(aijPoint);
                    } while(i3 != sI || j3 != sJ);

                    --UNum[i][j];
                    if (UNum[i][j] == 0) {
                        S2[i][j] = 3;
                    }

                    if (pointList.size() > 1) {
                        BorderLine aBLine = new BorderLine();
                        aBLine.area = getExtentAndArea(pointList, aBLine.extent);
                        aBLine.isOutLine = true;
                        aBLine.isClockwise = true;
                        aBLine.pointList = pointList;
                        aBLine.ijPointList = ijPList;
                        borderLines.add(aBLine);
                    }
                }
            }
        }

        List<Border> borders = new ArrayList();

        BorderLine aLine;
        BorderLine bLine;
        for(i = 1; i < borderLines.size(); ++i) {
            aLine = (BorderLine)borderLines.get(i);

            for(j = 0; j < i; ++j) {
                bLine = (BorderLine)borderLines.get(j);
                if (aLine.area > bLine.area) {
                    borderLines.remove(i);
                    borderLines.add(j, aLine);
                    break;
                }
            }
        }

        Border aBorder;
        ArrayList lineList;
        if (borderLines.size() == 1) {
            aLine = (BorderLine)borderLines.get(0);
            if (!isClockwise(aLine.pointList)) {
                Collections.reverse(aLine.pointList);
                Collections.reverse(aLine.ijPointList);
            }

            aLine.isClockwise = true;
            lineList = new ArrayList();
            lineList.add(aLine);
            aBorder = new Border();
            aBorder.LineList = lineList;
            borders.add(aBorder);
        } else {
            for(i = 0; i < borderLines.size() && i != borderLines.size(); ++i) {
                aLine = (BorderLine)borderLines.get(i);
                if (!isClockwise(aLine.pointList)) {
                    Collections.reverse(aLine.pointList);
                    Collections.reverse(aLine.ijPointList);
                }

                aLine.isClockwise = true;
                lineList = new ArrayList();
                lineList.add(aLine);

                for(j = i + 1; j < borderLines.size() && j != borderLines.size(); ++j) {
                    bLine = (BorderLine)borderLines.get(j);
                    if (bLine.extent.xMin > aLine.extent.xMin && bLine.extent.xMax < aLine.extent.xMax && bLine.extent.yMin > aLine.extent.yMin && bLine.extent.yMax < aLine.extent.yMax) {
                        aPoint = (PointD)bLine.pointList.get(0);
                        if (pointInPolygon(aLine.pointList, aPoint)) {
                            bLine.isOutLine = false;
                            if (isClockwise(bLine.pointList)) {
                                Collections.reverse(bLine.pointList);
                                Collections.reverse(bLine.ijPointList);
                            }

                            bLine.isClockwise = false;
                            lineList.add(bLine);
                            borderLines.remove(j);
                            --j;
                        }
                    }
                }

                aBorder = new Border();
                aBorder.LineList = lineList;
                borders.add(aBorder);
            }
        }

        return borders;
    }

 

参考:https://blog.csdn.net/weixin_39633134/article/details/116031614

标签:bLine,aLine,int,new,borderLines,tracingBorders,pointList
来源: https://www.cnblogs.com/2008nmj/p/15846004.html