如何在图像中找到一个简单矩形的尺寸?

huangapple go评论74阅读模式
英文:

How can i find the dimensions of a simple rectangle in an image?

问题

如何在位图图像中找到矩形

我正在进行测试的图像,白色是原点,灰色是墙](https://i.stack.imgur.com/b1hoa.png),大多数情况下会是这样我正在尝试读取图像,并根据像素的颜色将墙壁放置在二维空间中,使用C++和SFML。在图像中,有一组像素形成了一个矩形。我首先尝试为每个像素添加一个正方形墙壁,但这显然是一个低效的解决方案。然后我尝试创建更长矩形的行。这个方法效果还可以,但仍然不是最优的。我该如何找到这个矩形的尺寸,以便根据像素位置和颜色使用一个墙壁形状?

英文:

How can i find rectangles in a bitmap image

Image i'm testing with, white is the origin and grey are walls, This is what most will look likeI'm trying to read an image and place walls in a 2d space depending on the colours of the pixels, using c++ and sfml. In the image, there's a rectangle shape of pixels. I first tried adding a square wall for each pixel, but this is obviously an ineffective solution. I then tried creating rows of longer rectangles. This works ok but it still isn't optimal. How can i find the dimensions of this rectangle given pixel positions and colours so i'm able to use one wall shape?

答案1

得分: 1

我建议实现轮廓跟踪算法。

  1. 该算法具有一个不变性,即您始终位于一个空单元格,右侧有一个非空单元格。从任何这样的单元格开始,在绿色分段所示的位置开始轮廓线段。 <br/>如何在图像中找到一个简单矩形的尺寸?
  2. 您位于带有洋红箭头的像素上,朝着该方向。通过检查前面的两个像素,只有4种情况。
  3. 如果您回到起点位置,然后关闭轮廓(第一次时忽略此检查)。
  4. 如果前方的单元格非空,请在当前点结束线段并开始新的线段。向左转并重复(转到2)请注意右侧有一个非空单元格。 <br/>如何在图像中找到一个简单矩形的尺寸?
  5. 如果前方的单元格为空,右侧的单元格非空,则向前移动并重复。不添加线段顶点,只需延伸当前线段。然后重复(转到2)。请注意右侧有一个非空单元格。 <br/>如何在图像中找到一个简单矩形的尺寸?
  6. 如果前方的单元格为空,右侧的单元格也为空,请结束当前线段,向右转并重复(转到2)。请注意右侧有一个非空单元格。<br/>如何在图像中找到一个简单矩形的尺寸?
  7. 如果前方的单元格非空,右侧的单元格为空,则您可以选择(按照自己的约定)将其解释为左转或右转(与步骤4或步骤6一样),这取决于您认为仅在角落相接的形状是否连接在一起。<br/>如何在图像中找到一个简单矩形的尺寸? 如何在图像中找到一个简单矩形的尺寸?

当此算法结束时,您将获得一组线段,跟踪形状的轮廓,您可以将其添加到场景中作为矢量/多边形几何体。

英文:

I suggest implementing a contour tracing algorithm.

  1. This algorithm has an invariant that you are always in an empty cell, with a non-empty cell on your right. Begin in any such cell and begin a contour line segment where the green segment is indicated. <br/>如何在图像中找到一个简单矩形的尺寸?
  2. You are in the pixel with the magenta arrow, facing in that direction. By examining the two pixels ahead, there are only 4 cases.
  3. If you are back to where you started, then close the contour (ignore this check the first time)
  4. If the cell ahead is non-empty, conclude the line segment at the current point and begin a new one. Turn left and repeat (goto 2) Note there is a non-empty cell on your right. <br/>如何在图像中找到一个简单矩形的尺寸?
  5. If the cell ahead is empty and the cell to its right is non-empty, move forward and repeat. Do not add a line segment vertex, just keep extending the current line segment. Then repeat (goto 2). Note that there is a non-empty cell on your right. <br/>如何在图像中找到一个简单矩形的尺寸?
  6. If the cell ahead is empty and the cell to its right is empty, conclude the current line segment, turn right and repeat (goto 2). Note that there is a non-empty cell on your right.<br/>如何在图像中找到一个简单矩形的尺寸?
  7. If the cell ahead is non-empty and the cell to its right is empty, then you may choose (by your own convention) to interpret this as a left turn or right turn (as in step 4 or step 6) Depending on whether you think that shapes that touch only at a corner are connected or not. <br/>如何在图像中找到一个简单矩形的尺寸? 如何在图像中找到一个简单矩形的尺寸?

When this algorithm concludes, you will have a set of line segments that trace the contour of the shape that you can add as vector/polygonal geometry into your scene.

huangapple
  • 本文由 发表于 2023年6月1日 00:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定