英文:
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
我建议实现轮廓跟踪算法。
- 该算法具有一个不变性,即您始终位于一个空单元格,右侧有一个非空单元格。从任何这样的单元格开始,在绿色分段所示的位置开始轮廓线段。 <br/>
- 您位于带有洋红箭头的像素上,朝着该方向。通过检查前面的两个像素,只有4种情况。
- 如果您回到起点位置,然后关闭轮廓(第一次时忽略此检查)。
- 如果前方的单元格非空,请在当前点结束线段并开始新的线段。向左转并重复(转到2)请注意右侧有一个非空单元格。 <br/>
- 如果前方的单元格为空,右侧的单元格非空,则向前移动并重复。不添加线段顶点,只需延伸当前线段。然后重复(转到2)。请注意右侧有一个非空单元格。 <br/>
- 如果前方的单元格为空,右侧的单元格也为空,请结束当前线段,向右转并重复(转到2)。请注意右侧有一个非空单元格。<br/>
- 如果前方的单元格非空,右侧的单元格为空,则您可以选择(按照自己的约定)将其解释为左转或右转(与步骤4或步骤6一样),这取决于您认为仅在角落相接的形状是否连接在一起。<br/>
当此算法结束时,您将获得一组线段,跟踪形状的轮廓,您可以将其添加到场景中作为矢量/多边形几何体。
英文:
I suggest implementing a contour tracing algorithm.
- 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/>
- You are in the pixel with the magenta arrow, facing in that direction. By examining the two pixels ahead, there are only 4 cases.
- If you are back to where you started, then close the contour (ignore this check the first time)
- 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/>
- 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/>
- 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/>
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论