Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果我知道背景色,如何创建图像的轮廓?_C#_Algorithm_Image Processing_Bitmap_Outline - Fatal编程技术网

C# 如果我知道背景色,如何创建图像的轮廓?

C# 如果我知道背景色,如何创建图像的轮廓?,c#,algorithm,image-processing,bitmap,outline,C#,Algorithm,Image Processing,Bitmap,Outline,我想创建一个简单的程序,打开任何给定的图像并选择两种颜色:BackgroundColor和OutlineColor。然后围绕“对象”画一个轮廓 以下是我目前的代码: for (int y = 1; y < height - 1; y++) //iterate trough every pixel { //in my bitmap for (int x = 1; x

我想创建一个简单的程序,打开任何给定的图像并选择两种颜色:
BackgroundColor
OutlineColor
。然后围绕“对象”画一个轮廓

以下是我目前的代码:

        for (int y = 1; y < height - 1; y++) //iterate trough every pixel 
        {                                    //in my bitmap
            for (int x = 1; x < width - 1; x++)
            {
                //i want to put a pixel only if the the curent pixel is background
                if (bitmap.GetPixel(x, y) != BackgroundColor)
                    continue;

                var right = bitmap.GetPixel(x + 1, y);
                var down = bitmap.GetPixel(x, y + 1);
                var up = bitmap.GetPixel(x, y - 1);
                var left = bitmap.GetPixel(x - 1, y);
                //get the nearby pixels
                var neibours = new List<Color> {up, down, left, right};

                var nonBackgroundPix = 0;
                //then count how many are not outline nor background color
                foreach (Color neibour in neibours)
                {
                    if (neibour != BackgroundColor && neibour != OutlineColor)
                    {
                        nonBackgroundPix++;
                    }
                }
                //finaly put an outline pixel only if there are 1,2 or 3 non bg pixels
                if (nonBackgroundPix > 0 && nonBackgroundPix < 4)
                {
                    bitmap.SetPixel(x, y, OutlineColor);
                }
            }
        }
for(int y=1;y0和非背景PIX<4)
{
位图.SetPixel(x、y、OutlineColor);
}
}
}
当我运行代码和输入时,问题来了 我明白了

我想要的是

如果您在我的代码中发现问题,请了解更好的算法,或者设法以某种方式解决问题,请告诉我。提前谢谢各位

问题:您正在将背景色更改为非背景色,然后将通过后续像素检查拾取该颜色

解决方案:
我建议存储一个新的数组,其中包含“待稍后更改”的像素,然后在映射完完整图像后,返回并设置这些像素。(此外,您可以立即更改它,然后在可以检查的像素上添加一个标志,但这是您需要实现的更多逻辑,例如检查布尔数组)

@z-Wow效果非常好。谢谢你,伙计!把它作为答案贴出来,否则我会自己回答