简体   繁体   English

在不触摸物体的情况下增加位掩码边框

[英]Grow bitmask border without touching objects

I've written a bitmask filter in C#, to grow a border, where found pixels should grow border if there is enough space, but the areas may not touch each other 我已经用C#编写了一个位掩码过滤器,以增加边框,如果有足够的空间,发现的像素应该增加边框,但是这些区域可能不会相互接触

input mask   output mask
..........    .....xxx...
.....x....    .....xxxx...
.....x....    xxx..xxxxxx.
..x..xxxx.    xxx..xxxxxx.
..........    xxx..xxxxxx.

Using a 5x5 binary convolution that checks the border. 使用5x5二进制卷积来检查边界。
The border consists of 8 pixels (x-2,y-2) , (x,y-2) , (x+2,y-2) ... ie corners and centers of borders. 边框由8个像素(x-2,y-2)(x,y-2)(x+2,y-2) ,即边框的角和中心。
If a pixel is found, I set its corresponding array P[i] to true . 如果找到像素,则将其对应的数组P[i]true
So these 8 pixels are represented in a boolean array starting from P[0] trough p[7] . 因此,这8个像素以从P[0] p[7]的布尔数组表示。

Next I count boolean true s. 接下来,我计算布尔值true

If the count of P[.] > 3 then there is another object nearby and I don't grow the border there. 如果P[.] > 3则附近还有另一个对象,并且我没有在此处扩大边框。

If the count is 1, so if for example (x-2,y)==true then write mask (x-1,y) . 如果计数为1,则例如(x-2,y)==true则写掩码(x-1,y)

If the count is 2, so if for example (x+2,y)==true && (x+2,y+2)==true then write mask (x+1,y+1) . 如果计数为2,则例如,如果(x+2,y)==true && (x+2,y+2)==true则写掩码(x+1,y+1)

I've got quite a lot of boolean comparisons to eventually make this work. 我有很多布尔比较最终可以使这项工作。 I won't bore you width the dull code, but I'm wondering if the principle I use is ideal for this, or if there exists other methods to do this. 我不会为您设置沉闷的代码宽度,但是我想知道我使用的原理是否适合此操作,或者是否存在其他方法可以执行此操作。

As I got a side effect (though not really a problem) that makes the shapes more cubic over every iteration. 当我遇到一个副作用(虽然不是真正的问题)时,会使每次迭代的形状更加立体。 Which seems to happen less in programs such as PhotoShop. 在PhotoShop等程序中,这种情况似乎较少发生。

===== =====
some additional info, the code had to run on a video stream, so i prefer fast ways. 一些其他信息,代码必须在视频流上运行,所以我更喜欢快速方法。 I wrote "dull" code, if i would print this its 80+ lines most is binary compare code and switch cases too make it fast, 80+ is a to long post for here, much easier to understand when explained in normal language. 我写了“沉闷”的代码,如果我将其打印出来的80多行代码大多数是二进制比较代码,并且切换情况也使其变得很快,那么80+在这里就太长了,用普通语言解释时更容易理解。

Have you heard of mathematical morphology? 您听说过数学形态吗? You more or less implemented a thickening operator (though thickening is defined differently, I think the result is more or less similar), but there are more efficient ways to do the same thing. 您或多或少地实现了增稠运算符 (尽管增稠的定义不同,但我认为结果或多或少相似),但是有更有效的方法可以完成相同的事情。

One alternative approach is to: 一种替代方法是:

  • Compute the distance transform of the inverse of your image. 计算图像逆距离的距离变换 This is an image where the further away from your objects you get, the larger the grey value is. 这是一张图像,离您的物体越远,灰度值越大。 This image will have ridges exactly half-way between the objects. 该图像将在对象之间恰好有一半的山脊。 Let's call this image distance . 我们将此图像distance称为。

  • Compute the watershed of distance . 计算distance分水岭 This is a binary image with lines that run along the ridges of distance . 这是一个二进制图像,其中的线条沿distance的脊延伸。 Let's call this separation . 我们称这种separation

  • Threshold distance at the appropriate value to get objects grown by the amount you want them grown. 阈值 distance为适当的值,以使对象以所需的数量增长。 If you want to grow objects by 10 pixels, threshold such that all pixels with a value less or equal to 10 are selected ( true ). 如果要将对象增加10个像素,请设置阈值,以选择所有值小于或等于10的像素( true )。

  • Subtract separation from distance . 减去separationdistance The pixels in distance that are set, and also set in separation , should be reset. 设置的distance像素以及separation设置的distance像素均应重置。 You now have your original objects grown by your chosen distance, but still separate. 现在,您的原始对象将按您选择的距离增长,但仍然分开。

I won't bore you width the dull code... 我不会厌烦您输入无聊的代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM