笔触冲击填充

时间:2018-05-18 23:46:49

标签: c# image-processing flood-fill

全部晕! 我试图从PhotoDirector重新创建剪辑 https://youtu.be/GLNNCrp650Y?t=77

经过多次重拍和接近,这就是我所处的位置。 Autofill

基本代码来自: http://wiki.unity3d.com/index.php/TextureFloodFill

基本代码只需单击一下,使用该颜色和增长测试将初始颜色样本与其他所有像素进行比较,如果不相似则停止增长。

由于我需要对多个像素进行采样(在单个画笔笔划上),我使用嵌套的bool类进行检查(尝试将颜色添加到List并使用List.Contains检查每个像素。超慢。这要快得多)< / p>

在当前附加的代码中我使用RGB(返回)。之前我使用过HSBColor但是尝试回RGB,因为在HSB中有些情况,如果Sat非常低,或者亮度太低/太高,则很难比较该像素的相邻Hue。 我即将恢复到HSB,并将添加2个嵌套bool,1个仅Sat&gt;亮度(对于饱和度

我还没试过这个(厌倦了从HSB回到RGB ......),因为我开始认为(部分)问题不在像素检查中,还有 GROW检查。 PhotoDirector(PD)也具有非常好的边缘检测功能,并且能够以某种方式消除我所拥有的碎片问题。 在我的代码中,我正在检查下一个 ONE 像素并根据它停止/继续。也许PD检查前面的 FEW 像素,如果它一直不同,那么它实际上就会停止,如果它只有几个不同的像素,那么它只会在它们上方增长,因此需要像素。

PD的另一个特点是增长限制边界框有点特征。我的代码,即使是一个小笔刷和小样本,如果它匹配并且可以增长到整个图像,它会。 PD获得了增长的最大宽度/高度限制。我尝试了一个统一的彩色图像,它确实填写了框。 我想也许PD的公差设置超过限制但是这个限制范围框,它可以产生一个实心填充,但在我的情况下,如果我的公差太高,那么它只会在明显的边缘上生长(并继续整个图像...) 所以我没有亲自添加这个限制框限制,等待实际填充更稳定,特别是在拉丝区域附近。

TL; DR 在&#34;刷子到样品中,刷子上的洪水填充点击&#34;像PhotoDirector这样的方法,他们是如何做到的?是否更好的采样/像素颜色测试?还是他们增加像素检查的方式?

ABC

感谢阅读! 欢呼声,

这是相关的代码,有点乱,因为它仍在不断变化......

public class GLookup
{
public bool[] bBools;

public GLookup(int num)
{
    bBools = new bool[num];
}

}

public class RLookup
{
public GLookup[] gLookup;

public RLookup(int num)
{
    gLookup = new GLookup[num];
    for(int i = 0; i < num; i++)
    {
        gLookup[i] = new GLookup(num);  
    }
}

}

 static void ResetLookup()
{
    for(int i = 0; i < Grid.Paint.Options.colorLookupRes; i++)
    {
        rgbLookup[i] = new RLookup(Grid.Paint.Options.colorLookupRes);
        rgbCheck[i] = new RLookup(Grid.Paint.Options.colorLookupRes);
    }
}

static void InsertLookup(Color32 rgb, float tol)
{
    int r = (int)(rgb.r/byteToLookup);// * Grid.Paint.Options.colorLookupRes);
    int g = (int)(rgb.g/ byteToLookup);// * Grid.Paint.Options.colorLookupRes);
    int b = (int)(rgb.b/ byteToLookup);// * Grid.Paint.Options.colorLookupRes);

    if(r == Grid.Paint.Options.colorLookupRes)
        r = Grid.Paint.Options.colorLookupRes - 1;
    if(g == Grid.Paint.Options.colorLookupRes)
        g = Grid.Paint.Options.colorLookupRes-1;
    if(b == Grid.Paint.Options.colorLookupRes)
        b = Grid.Paint.Options.colorLookupRes-1;

    if(rgbCheck[r].gLookup[g].bBools[b])
    {
        return;
    }
    rgbCheck[r].gLookup[g].bBools[b] = true;

    int tolNum = Grid.Paint.Options.maxTolNum;// (int)Mathf.Lerp(Grid.Paint.Options.minTolNum, Grid.Paint.Options.maxTolNum, tol);

    // Initial
    InsertLookupEntry(r, g, b);
    InsertNeighborSatBri(r, g, b, tolNum);

    // Hue fills
    int currentHue;
    // +- 5 hue if tol 1
    for(int i = 0; i < Grid.Paint.Options.hueFill; i++)
    {            
        // Higher hue
        currentHue = r + (i + 1);

        if(currentHue > Grid.Paint.Options.colorLookupRes-1)
            currentHue = Grid.Paint.Options.colorLookupRes-1;

        InsertLookupEntry(currentHue, g, b);
        InsertNeighborSatBri(currentHue, g, b, tolNum);

        // Lower hue
        currentHue = r - (i + 1);

        if(currentHue < 0)
            currentHue = 0;//            Grid.Paint.Options.colorLookupRes;

        InsertLookupEntry(currentHue, g, b);
        InsertNeighborSatBri(currentHue, g, b, tolNum);
    }
}
// This used to be for Hue/Sat/Brightness. But changed back to RGB, so this is similar as above's Hue tolerance fill

static void InsertNeighborSatBri(int r, int g, int b, int tolNum)
{
    int currentG;
    int currentB;
    for(int i = 0; i < tolNum; i++)
    {
        // Add the min plus from given value
        currentG = g + (i + 1);
        currentG = Mathf.Clamp(currentG, 0, Grid.Paint.Options.colorLookupRes - 1);
        for(int j = 0; j < tolNum; j++)
        {
            currentB = b + (j + 1);
            currentB = Mathf.Clamp(currentB, 0, Grid.Paint.Options.colorLookupRes - 1);
            InsertLookupEntry(r, currentG, currentB);

            currentB = b - (j + 1);
            currentB = Mathf.Clamp(currentB, 0, Grid.Paint.Options.colorLookupRes - 1);
            InsertLookupEntry(r, currentG, currentB);
        }

        currentG = g - (i + 1);
        currentG = Mathf.Clamp(currentG, 0, Grid.Paint.Options.colorLookupRes - 1);
        for(int j = 0; j < tolNum; j++)
        {
            currentB = b + (j + 1);
            currentB = Mathf.Clamp(currentB, 0, Grid.Paint.Options.colorLookupRes - 1);
            InsertLookupEntry(r, currentG, currentB);

            currentB = b - (j + 1);
            currentB = Mathf.Clamp(currentB, 0, Grid.Paint.Options.colorLookupRes - 1);
            InsertLookupEntry(r, currentG, currentB);
        }
    }
}

static void InsertLookupEntry(int r, int g, int b)
{
    rgbLookup[r].gLookup[g].bBools[b] = true;
}    

// Take in list of points from a single brush stroke
public static List<Point> FloodCutBrush(this Texture2D aTex, Color32[] refPixels, List<Point> growPoints, List<Point> lastDotSample, float tolerance, bool[] history, bool isAdd)
{
int w = aTex.width;

    int h = aTex.height;
    List<Point> changedPixel = new List<Point>();
    Color32[] photoPixels = new Color32[refPixels.Length];
    System.Array.Copy(refPixels, photoPixels, refPixels.Length);

    bool[] existCheck = new bool[history.Length];

    Queue<Point> nodes = new Queue<Point>();

    ResetLookup();

    // Setting up samples
    foreach(Point v in growPoints)
    {
        // Out of bounds is not valid
        if(v.x < 0 || v.y < 0 || w < 0)
            continue;

        int refId = (v.x) + ((v.y) * (w/2));
        if(refId >= photoPixels.Length)
            continue;

        Color32 hsb = photoPixels[refId];

        InsertLookup(hsb, tolerance);
        nodes.Enqueue(v);
    }

    // Fill search marks a pixel to alpha = 0. Then checks the top/bottom pixel and adds that to queue for future iteration
    while(nodes.Count > 0)
    {
        Point current = nodes.Dequeue();

//this goes right


for(int i = current.x; i < w / 2; i++)
        {
            int idx = i + current.y * (w / 2);
            if(idx >= history.Length)
                continue;

            Point fullCoord = IndexToPoint(w / 2, idx);
            // Because color sampling from brush is half resolution, and photopixel is full resolution..
            fullCoord.x *= 2;
            fullCoord.y *= 2;
            int fullIdx = fullCoord.x + (fullCoord.y * (w));

            Color32 C = photoPixels[fullIdx];

            if(history[idx] && (C.a == 0 || RGBTest(C) == false))// && (C.b < highBright && C.b > lowBright))
            {
                changedPixel.Add(current);
                existCheck[idx] = true;
                break;
            }

            C.a = 0;
            photoPixels[fullIdx] = C;
            // Queue up/down pix
            if(current.y + 1 < h / 2)
            {
                C = photoPixels[fullIdx + w];
                if(history[idx] && (C.a != 0 && RGBTest(C) == true))// || (C.b > highBright || C.b < lowBright))
                {
                    nodes.Enqueue(new Point(i, current.y + 1));

                }
            }

            if(current.y - 1 >= 0)
            {
                C = photoPixels[fullIdx - w];
                if(history[idx] && (C.a != 0 && RGBTest(C))) // || (C.b > highBright || C.b < lowBright))
                {
                    nodes.Enqueue(new Point(i, current.y - 1));

                }
            }

            changedPixel.Add(current);
            existCheck[idx] = true;

        }

        //this goes left, same as above but --x


    }

    return changedPixel;
}

public static bool RGBTest(Color32 c1)
    {
    int r = (int)(c1.r/ byteToLookup);
    int g = (int)(c1.g/ byteToLookup);
    int b = (int)(c1.b/ byteToLookup);

    if(rgbLookup[r].gLookup[g].bBools[b])
    {
        count++;
        return true;
    } else
        return false;

}

0 个答案:

没有答案