查找图像二进制对象的周长
Find perimeter of the image binary object
我正在寻找二进制对象的周长。
考虑下图
[ 0 0 0 0 1 1 0 ]
[ 0 0 1 0 0 0 0 ]
[ 0 1 1 0 1 1 0 ]
[ 0 1 1 0 0 1 0 ]
[ 0 1 1 1 0 0 0 ]
[ 0 1 0 0 1 1 0 ]
[ 0 0 0 0 1 1 0 ]
带标签的图像将如下所示
[ 0 0 0 0 1 1 0 ]
[ 0 0 2 0 0 0 0 ]
[ 0 2 2 0 3 3 0 ]
[ 0 2 2 0 0 3 0 ]
[ 0 2 2 0 0 0 0 ]
[ 0 2 0 0 4 4 0 ]
[ 0 0 0 0 4 4 0 ]
我还收集了数组列表中的每个对象像素
例如,对于 4 个标记的对象,列表将是
{ (5,4), (5,5) , (6,4), (6,5) }
面积只是每个对象像素阵列的大小,但是我怎样才能找到周长,我是否应该再次遍历整个图像找到细胞的邻居检查它是否是对象的角像素或者有更简单的方法仅根据坐标执行此操作。
请建议什么是最简单的查找周长的方法,任何代码示例将不胜感激
尝试对您的图像进行广度优先搜索,(或者遍历您的点列表),然后标记与不属于同一组的另一个像素相邻的每个像素。
我不清楚您想要的周长是所请求对象外边缘的每个像素,还是边界对象的每个像素。我暂时假设前者。
设置图像:
以下是您将如何执行此操作。首先,将图像设置为二维数组,每个像素都标有组号:
[ 0 0 0 0 1 1 0 ]
[ 0 0 2 0 0 0 0 ]
[ 0 2 2 0 3 3 0 ]
[ 0 2 2 0 0 3 0 ]
[ 0 2 2 0 0 0 0 ]
[ 0 2 0 0 4 4 0 ]
[ 0 0 0 0 4 4 0 ]
加载它的一个好方法是使用 Scanner
对象来逐个获取每个点:
List<Point> points = new ArrayList<>();
Scanner scanner = new Scanner( /* whatever your input source is */ );
String pointRegex = "\(\d,\d\)"; //looks for something like "(#,#)"
while(!scanner.hasNext(pointRegex)){
String pointText = scanner.next(pointRegex); //For example, "(5,4)"
Point point = getPointFromText(pointText); //turns a string into a point
points.add(point);
}
注意 Scanner.next(String pattern)
的使用。这种方法将 return 下一个 String
看起来像那个模式。 (如果您想了解有关其工作原理的更多信息,请阅读正则表达式。)
现在填充网格:
boolean[][] binaryImage = new boolean[width][height];
for(Point p : points){ //Iterate through each Point inside our List of Point objects
binaryImage[p.getX()][p.getY()] = true;
}
这会将我们的 Point
个对象“points
”集合表示的对象放入 boolean
个网格中。我们只需要担心这个对象,所以我们不需要加载任何其他对象。现在找出周边有哪些点。
递归方法:
boolean[][] visitedBefore = new boolean[width][height];
boolean[][] isOnPerimeter = new boolean[width][height];
int[] deltaX = {-1, 0, 1, -1, 1, -1, 0, 1},
deltaY = {-1, -1, -1, 0, 0, 1, 1, 1};
Queue<Point> searchNext = new LinkedList<>();
searchNext.add(points.get(0)); //Just need one point to get going
while(!searchNext.isEmpty()){
Point p = searchNext.remove(); //take what's waiting at the front of the queue
if(visitedBefore[p.getX()][p.getY()]){
continue; //already check this spot!
}
//mark that we've been here
visited[p.getX()][p.getY()] = true;
//look at all of this Point's neighbors
for(int i = 0 ; i < deltaX.length ; i++){
int newX = p.getX() + deltaX[i];
int newY = p.getY() + deltaY[i];
//make sure this isn't out of bounds
if(newX < 0 || newX >= width || newY<0 || newY>=height){
isOnPerimeter[p.getX()][p.getY()] = true; //if you decide bordering the edge of the image counts as being on the perimeter
continue;
}
//check if this new point we're considering isn't part of the image
if( binaryImage[p.getX()][p.getY()] != binaryImage[newX][newY] ){
//if it isn't, then this Point p must be on the perimeter
isOnPerimeter[p.getX()][p.getY()] = true;
} else {
/* otherwise, this new point we're considering is part of the
* same object, and could be part of the perimeter. */
searchNext.add(new Point(newX, newY));
}
}
}
现在您有一个网格,其周长上的每个点都标记为 true
。如果您需要这些作为列表,挑选这些点很容易:
List<Point> perimeter = new ArrayList<Point>();
for(int x = 0 ; x < isOnPerimeter.length ; x++)
for(int y = 0 ; y < isOnPerimeter[x].length ; y++)
perimeter.add( new Point(x,y) );
迭代法:
这与上面的非常相似,但直接跳到将周界点放入列表中。
int[] deltaX = {-1, 0, 1, -1, 1, -1, 0, 1},
deltaY = {-1, -1, -1, 0, 0, 1, 1, 1};
outer: for(Point p : points){
inner: for(int i = 0 ; i < deltaX.length ; i++){
int newX = p.getX() + deltaX[i];
int newY = p.getY() + deltaY[i];
//check if this new point we're considering is outside the image
if(newX < 0 || newX >= width || newY<0 || newY>=height){
perimeter.add(p); //if you decide bordering the edge of the image counts as being on the perimeter
continue outer;
}
//check if this new point we're considering isn't part of the image
if( binaryImage[p.getX()][p.getY()] != binaryImage[newX][newY] ){
//if it isn't, then this Point p must be on the perimeter
perimeter.add(p);
continue outer;
}
}
}
注意标签 outer:
和 inner:
。这让我们可以选择在说 continue outer;
.
时跳过哪个 for 循环
给你!这应该可以帮助您将任何对象的周长作为二进制图像或列表。
我正在寻找二进制对象的周长。
考虑下图
[ 0 0 0 0 1 1 0 ]
[ 0 0 1 0 0 0 0 ]
[ 0 1 1 0 1 1 0 ]
[ 0 1 1 0 0 1 0 ]
[ 0 1 1 1 0 0 0 ]
[ 0 1 0 0 1 1 0 ]
[ 0 0 0 0 1 1 0 ]
带标签的图像将如下所示
[ 0 0 0 0 1 1 0 ]
[ 0 0 2 0 0 0 0 ]
[ 0 2 2 0 3 3 0 ]
[ 0 2 2 0 0 3 0 ]
[ 0 2 2 0 0 0 0 ]
[ 0 2 0 0 4 4 0 ]
[ 0 0 0 0 4 4 0 ]
我还收集了数组列表中的每个对象像素
例如,对于 4 个标记的对象,列表将是
{ (5,4), (5,5) , (6,4), (6,5) }
面积只是每个对象像素阵列的大小,但是我怎样才能找到周长,我是否应该再次遍历整个图像找到细胞的邻居检查它是否是对象的角像素或者有更简单的方法仅根据坐标执行此操作。
请建议什么是最简单的查找周长的方法,任何代码示例将不胜感激
尝试对您的图像进行广度优先搜索,(或者遍历您的点列表),然后标记与不属于同一组的另一个像素相邻的每个像素。
我不清楚您想要的周长是所请求对象外边缘的每个像素,还是边界对象的每个像素。我暂时假设前者。
设置图像:
以下是您将如何执行此操作。首先,将图像设置为二维数组,每个像素都标有组号:
[ 0 0 0 0 1 1 0 ]
[ 0 0 2 0 0 0 0 ]
[ 0 2 2 0 3 3 0 ]
[ 0 2 2 0 0 3 0 ]
[ 0 2 2 0 0 0 0 ]
[ 0 2 0 0 4 4 0 ]
[ 0 0 0 0 4 4 0 ]
加载它的一个好方法是使用 Scanner
对象来逐个获取每个点:
List<Point> points = new ArrayList<>();
Scanner scanner = new Scanner( /* whatever your input source is */ );
String pointRegex = "\(\d,\d\)"; //looks for something like "(#,#)"
while(!scanner.hasNext(pointRegex)){
String pointText = scanner.next(pointRegex); //For example, "(5,4)"
Point point = getPointFromText(pointText); //turns a string into a point
points.add(point);
}
注意 Scanner.next(String pattern)
的使用。这种方法将 return 下一个 String
看起来像那个模式。 (如果您想了解有关其工作原理的更多信息,请阅读正则表达式。)
现在填充网格:
boolean[][] binaryImage = new boolean[width][height];
for(Point p : points){ //Iterate through each Point inside our List of Point objects
binaryImage[p.getX()][p.getY()] = true;
}
这会将我们的 Point
个对象“points
”集合表示的对象放入 boolean
个网格中。我们只需要担心这个对象,所以我们不需要加载任何其他对象。现在找出周边有哪些点。
递归方法:
boolean[][] visitedBefore = new boolean[width][height];
boolean[][] isOnPerimeter = new boolean[width][height];
int[] deltaX = {-1, 0, 1, -1, 1, -1, 0, 1},
deltaY = {-1, -1, -1, 0, 0, 1, 1, 1};
Queue<Point> searchNext = new LinkedList<>();
searchNext.add(points.get(0)); //Just need one point to get going
while(!searchNext.isEmpty()){
Point p = searchNext.remove(); //take what's waiting at the front of the queue
if(visitedBefore[p.getX()][p.getY()]){
continue; //already check this spot!
}
//mark that we've been here
visited[p.getX()][p.getY()] = true;
//look at all of this Point's neighbors
for(int i = 0 ; i < deltaX.length ; i++){
int newX = p.getX() + deltaX[i];
int newY = p.getY() + deltaY[i];
//make sure this isn't out of bounds
if(newX < 0 || newX >= width || newY<0 || newY>=height){
isOnPerimeter[p.getX()][p.getY()] = true; //if you decide bordering the edge of the image counts as being on the perimeter
continue;
}
//check if this new point we're considering isn't part of the image
if( binaryImage[p.getX()][p.getY()] != binaryImage[newX][newY] ){
//if it isn't, then this Point p must be on the perimeter
isOnPerimeter[p.getX()][p.getY()] = true;
} else {
/* otherwise, this new point we're considering is part of the
* same object, and could be part of the perimeter. */
searchNext.add(new Point(newX, newY));
}
}
}
现在您有一个网格,其周长上的每个点都标记为 true
。如果您需要这些作为列表,挑选这些点很容易:
List<Point> perimeter = new ArrayList<Point>();
for(int x = 0 ; x < isOnPerimeter.length ; x++)
for(int y = 0 ; y < isOnPerimeter[x].length ; y++)
perimeter.add( new Point(x,y) );
迭代法:
这与上面的非常相似,但直接跳到将周界点放入列表中。
int[] deltaX = {-1, 0, 1, -1, 1, -1, 0, 1},
deltaY = {-1, -1, -1, 0, 0, 1, 1, 1};
outer: for(Point p : points){
inner: for(int i = 0 ; i < deltaX.length ; i++){
int newX = p.getX() + deltaX[i];
int newY = p.getY() + deltaY[i];
//check if this new point we're considering is outside the image
if(newX < 0 || newX >= width || newY<0 || newY>=height){
perimeter.add(p); //if you decide bordering the edge of the image counts as being on the perimeter
continue outer;
}
//check if this new point we're considering isn't part of the image
if( binaryImage[p.getX()][p.getY()] != binaryImage[newX][newY] ){
//if it isn't, then this Point p must be on the perimeter
perimeter.add(p);
continue outer;
}
}
}
注意标签 outer:
和 inner:
。这让我们可以选择在说 continue outer;
.
给你!这应该可以帮助您将任何对象的周长作为二进制图像或列表。