行进平方算法中的模棱两可案例 [英] Ambiguous cases in Marching square algorithm

查看:123
本文介绍了行进平方算法中的模棱两可案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们考虑行军广场上的Wikipedia文章,则会发现该案例#5和case#10被认为是模棱两可的案例.

If we take Wikipedia article on Marching square into account, we see that case#5 and case#10 are said to be ambiguous cases.

我已经按照以下方式实施了游行广场,但我不了解如何产生模棱两可的情况:

I have implemented Marching Square as follows and I am not understanding how an ambiguous case can arise:

public class LinesRectangle
{
    public Graphics Graphics { get; set; }
    public Color Color { get; set; }
    public Pen Pen { get; set; }
    public int Thickness { get; set; }

    public LinesRectangle()
    {
        Color = Color.Blue;
        Thickness = 2;
        Pen = new Pen(Color, Thickness);
    }

    public void DrawLines(int x, int y, int width, int code)
    {
        int height = width;

        Graphics.DrawRectangle(Pen, new System.Drawing.Rectangle(x, y, width, height));

        int x1 = 0, y1 = 0;
        int x2 = 0, y2 = 0;

        switch (code)
        {
            case 0:
            case 15:
                break;
            case 1:
            case 14:
                x1 = x; y1 = y + height/2;
                x2 = x + width/2; y2 = y + height;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 2:
            case 13:
                x1 = x + width/2; y1 = y + height;
                x2 = x + width; y2 = y + height/2;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 3:
            case 12:
                x1 = x; y1 = y + height / 2;
                x2 = x + width; y2 = y + height / 2;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 4:
            case 11:
                x1 = x+width/2; y1 = y;
                x2 = x + width; y2 = y + height / 2;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 5:
                x1 = x ; y1 = y + height/2;
                x2 = x + width/2; y2 = y;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                x1 = x + width / 2; y1 = y + height;
                x2 = x + width; y2 = y + height / 2;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 6:
            case 9:
                x1 = x + width / 2; y1 = y;
                x2 = x + width/2; y2 = y + height;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 7:
            case 8:
                x1 = x; y1 = y + height / 2;
                x2 = x + width / 2; y2 = y;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
            case 10:
                x1 = x + width / 2; y1 = y;
                x2 = x + width; y2 = y + height / 2;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                x1 = x; y1 = y + height / 2;
                x2 = x + width / 2; y2 = y + height;
                Graphics.DrawLine(Pen, x1, y1, x2, y2);
                break;
        }
    }
}
    

您可以在这里看到每种情况都是单独处理的.

You can see here each of the cases are taken care of individually.

输出:

谁能告诉我我想念的东西吗?

Can anyone tell me what I am missing?

驱动程序:

public enum What
{
    lines, surface, both
}

public partial class DrawingForm : System.Windows.Forms.Form
{
    public int [,] Data { get; set; }

    public void Print(int[,] data, int xn, int yn)
    {
        for (int j = 0; j < yn; j++)
        {
            for (int i = 0; i < xn; i++)
            {
                Console.Write(data[i, j] + ", ");
            }
            Console.WriteLine();
        }
    }
    public int[,] normalize(int[,] data, int xn, int yn)
    {

        for (int j = 0; j < yn; j++)
        {
            for (int i = 0; i < xn; i++)
            {
                if (data[i, j] > 1)
                {
                    data[i, j] = 0;
                }
                else
                {
                    data[i, j] = 1;
                }
            }
        }

        return data;
    }

    public int[,] marching_square(int x, int y, int[,] data, int isovalue, What what)
    {
        int xn = x;
        int yn = y;

        data = normalize(data, xn, yn);

        int[,] bitMask = new int[xn - 1, yn - 1];

        for (int j = 0; j < yn - 1; j++)
        {
            for (int i = 0; i < xn - 1; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(data[i, j]);
                sb.Append(data[i + 1, j]);
                sb.Append(data[i + 1, j + 1]);
                sb.Append(data[i, j + 1]);

                bitMask[i, j] = Convert.ToInt32(sb.ToString(), 2);
            }
        }

        return bitMask;
    }

    public DrawingForm()
    {
        InitializeComponent();
    }

    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        int[,] data = new int[,] {
                                  { 1,1,1,1,1 },
                                  { 1,2,3,2,1 },
                                  { 1,3,1,3,1 },
                                  { 1,2,3,2,1 },
                                  { 1,1,1,1,1 }
                                  };

        int[,] bitMask = marching_square(5, 5, data, 0, What.lines);

        Graphics g = this.CreateGraphics();

        LinesRectangle rect = new LinesRectangle();
        rect.Graphics = g;
        
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0; i < 4; i++)
            {
                rect.DrawLines(i*50, j*50, 50, bitMask[i,j]);
            }
        } 
    }
}

编辑:如果有以下数据( @JeremyLakeman 指出):

In case of the following data (as pointed out by @JeremyLakeman):

{ 2,1,2,1,2 },
{ 1,2,1,2,1 },
{ 2,1,2,1,2 },
{ 1,2,1,2,1 },
{ 2,1,2,1,2 }

我的程序产生了以下输出:

my program produced the following output:

推荐答案

哦,我理解你.令人惊讶的是,这是一个好问题!

Oh man, I understand you. Surprisingly thats a good question!

当您确定值高于等值"时,就会清楚地看到歧义.是黑色或白色,等值线以下等值线"相反.

Ambiguity is seen clearly in a moment when you decide if "value above the isovalue" is black or white and opposite for the "value below the isovalue".

让我解释一下我的意思. 如果您手动执行算法,则可以获得以下结果.在遵循Wiki中描述的算法时,您要做的唯一选择-决定在绘制节点时使用哪种颜色.

Let me explain what I mean. If you do algorithm by hand you can get following results. The only choice you do while following algorithm described on wiki - is to decide what color to use when painting nodes.

{ 1, 1, 1 },
{ 1, 2, 1 },
{ 1, 1, 1 }

没有无歧义的情况,因此 选择无关紧要 -无论"1"是";黑点"或白点".

has no ambiguous cases so the choice does not matter - resulting image will be the same no matter if '1' is a "black dot" or a "white dot".

但是,请查看示例具有不明确情况的情况:

{ 1, 2, 1 },
{ 2, 1, 2 },
{ 1, 2, 1 }

如果"1"为白色,则算法将在中间点周围提供一个圆,如果将"1"选择为黑色,则相同的算法将在中间点附近提供4条圆弧.

algorith would provide a circle around the middle point if '1's are white, and same algorithm would provide 4 arcs near the middle points if '1's are chosen to be black.

我认为选择时刻在normalize函数中

I think moment of choice is in normalize function at

 if (data[i, j] > 1)

如果您更改"> ",到"< "如果情况不明,您将可以更改图片.对于无歧义的情况,它什么也不会改变.如果您看方法思想而不是算法,那么歧义更容易理解.查看鞍点-在绘制轮廓时存在歧义,因为从一方面来看,鞍点是一个最小值,而最大值是最大值-取决于测量方向.

If you change ">" to "<" you will get change of image for ambigous cases. And it would change nothing for non-ambigous cases. Ambiguity is easier to understand if you look at methods idea not algorithm. Look at saddle point - there is ambiguity in drawing contour because from the one hand saddle point is a minimum and from the other its a maximum - depends on direction of measurements.

希望能消除混乱.

编辑:我在评论中进行了详细说明,但为了便于查看,我将其复制在此处

I elaborated in comments but for visibility I'm duplicating it here

这篇关于行进平方算法中的模棱两可案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆