【问题标题】:What does the mask parameter do in the threshold method of the BitmapData class?BitmapData类的threshold方法中的mask参数有什么作用?
【发布时间】:2011-10-29 11:46:09
【问题描述】:

我正在尝试在位图中替换一种颜色和它附近的颜色。

threshold() 似乎有效,但似乎您必须指定确切的颜色“==”或确切颜色“”之前或之后的所有颜色加上“ ="。我希望 mask 参数能帮助我找到一种方法来找到一种颜色以及在它被替换之前和之后的颜色动态范围。它的预期用途是什么?

根据示例 1 和 2 下面的评论:

bit.threshold(bit, bit.rect, point, ">", 0xff000000, 0xffff0000, 0x00FF0000); 

bit.threshold(bit, bit.rect, point, ">", 0xff000000, 0xffff0000, 0x00EE0000);

【问题讨论】:

    标签: actionscript-3 bitmap bitmapdata


    【解决方案1】:

    如果您尝试进行泛洪填充,我认为 mask 参数对您没有帮助。 mask 参数允许您忽略测试中的部分颜色。在您的情况下,您想考虑颜色的所有通道,您只希望匹配是模糊的。

    例如如果要替换红色分量为 0 的所有像素,可以将 mask 设置为 0x00FF0000,这样会忽略其他通道。

    实现的伪代码大概是这样的:

    input = readPixel()
    value = input & mask
    if(value operation threshold)
    {
        writePixel(color)
    }
    

    您的任何样本都不会产生任何结果,因为掩码将值限制在 0x00000000 和 0x00FF0000 之间,然后测试它们是否大于 0xFF000000。

    【讨论】:

    • 你能解释更多吗?你是说如果我将掩码设置为 100% 0x00FF0000 (红色通道),那么其中没有红色的像素会被忽略吗?如果我将掩码设置为 0x00EE0000 会怎样?我将在主要帖子中发布一个示例。
    • 不,0x00FF0000 将忽略除红色之外的所有内容。 0xEE = 1110 1110,所以它会忽略所有其他红色阴影和一些块......这对图像没有意义。我认为它的预期用途是过滤整个频道。
    • 我觉得我在推动自己的运气,但我仍在努力将其付诸实践。您能解释一下以下各项的作用吗? (将示例添加到问题中)
    【解决方案2】:

    我也这样做了,最终,我发现创建自己的阈值方法是最好的。你可以在下面找到它。一切都在评论中解释。

    //_snapshot is a bitmapData-object
    for(var i:int = 0; i <= _snapshot.width; i++)
    {
        for(var j:int = 0; j <= _snapshot.height; j++)
        {
            //We get the color of the current pixel.
            var _color:uint = _snapshot.getPixel(i, j);                     
    
            //If the color of the selected pixel is between certain values set by the user, 
            //set the filtered pixel data to green. 
            //Threshold is a number (can be quite high, up to 50000) to look for adjacent colors in the colorspace.
            //_colorToCompare is the color you want to look for.
            if((_colorToCompare - (100 * _threshold)) <= _color && _color <= (_colorToCompare + (100 * _threshold)))
            {
                //This sets the pixel value.
                _snapshot.setPixel(i, j, 0x00ff00);
            }
            else
            {
                //If the pixel color is not within the desired range, set it's value to black.
                _snapshot.setPixel(i, j, 0x000000);
            }
        }
    }
    

    【讨论】:

    • 谢谢 Michiel(评论文字不够长)
    • Adden:这得到了我想要的,但我仍然想了解参数的作用。
    • 对不起,我周末出去了,所以我不能回答;)我看到你关于参数的问题已经被 Sean 回答了 :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多