多维数组数据的边界提取

  • 本文关键字:边界 提取 数据 数组 c#
  • 更新时间 :
  • 英文 :


我想把图案中的"1"改为"0"。在给定的代码中,我已经尝试了所有可能的逻辑。不能正常工作。我评论了所有的逻辑。请逐一检查,找出问题所在。

致意。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a =    { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, 
                            { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },  
                            { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
                            { 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 }, 
                            { 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 }, 
                            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
                            { 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 }, 
                            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
            int r, c;
            for (r = 0; r < 10; r++)
            {
                for (c = 0; c < 10; c++)
                {
                    Console.Write("{0}  ", a[r, c]);
                }
                Console.Write("n");
            }//For Printing...

            r = 0;
            c = 0;
            for (r = 0; r < 10; r++)// it is coding for changing row position
            {
                for (c = 0; c < 10; c++)// it is coding for changing column position
                {
                    if (a[r, c] != 0)// here i check position in array is not equal to zero.
                    {
                        int q, w, e, t, g, s, d, f;
                        q = a[r - 1, c];//Up
                        w = a[r + 1, c];//down
                        e = a[r, c - 1];//left
                        t = a[r, c + 1];//right
                        g = a[r + 1, c + 1];//down,right/* These all condintions are design for check circular surround of arrays position.*/
                        s = a[r + 1, c - 1];//down,left
                        d = a[r - 1, c + 1];//Up,right
                        f = a[r - 1, c - 1];//up,left

                        /*These are all possible logics which should be possibly made for checking around the position of array. If you want to check it, active one by one by removing comment*/
                        //if (q != 0 || w != 0 || e != 0 || t != 0 || g != 0 || s != 0 || d != 0 || f != 0)
                        //if (q != 0 && w != 0 && e != 0 && t != 0 && g != 0 && s != 0 && d != 0 && f != 0)
                        //if (q != 0 || w != 0 || e != 0 || t != 0 )
                        //if (q != 0 && w != 0 && e != 0 && t != 0 )
                        //if (q == 0 || w == 0 || e == 0 || t == 0 || g == 0 || s == 0 || d == 0 || f == 0)
                        //if (q == 0 && w == 0 && e == 0 && t == 0 && g == 0 && s == 0 && d == 0 && f == 0)
                        //if (q == 0 || w == 0 || e == 0 || t == 0 )
                        //if (q == 0 && w == 0 && e == 0 && t == 0 )
                        //if (q == 1 || w == 1 || e == 1 || t == 1 || g == 1 || s == 1 || d == 1 || f == 1)
                        //if (q == 1 && w == 1 && e == 1 && t == 1 && g == 1 && s == 1 && d == 1 && f == 1)
                        //if (q == 1 || w == 1 || e == 1 || t == 1 )
                        //if (q == 1 && w == 1 && e == 1 && t == 1 )
                        //if (q != 1 || w != 1 || e != 1 || t != 1 || g != 1 || s != 1 || d != 1 || f != 1)
                        //if (q != 1 && w != 1 && e != 1 && t != 1 && g != 1 && s != 1 && d != 1 && f != 1)
                        //if (q != 1 || w != 1 || e != 1 || t != 1 )
                        //if (q != 1 && w != 1 && e != 1 && t != 1 )

                        {
                            a[r, c] = 0;
                        }
                    }
                }
            }
            /*Further below coding is for Output*/
            Console.Write("nn");
            for (r = 0; r < 10; r++)
            {
                for (c = 0; c < 10; c++)
                {
                    Console.Write("{0}  ", a[r, c]);
                }
                Console.Write("n");
            }
        }
    }
}

所需的结果…

            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
             0, 0, 0, 0, 0, 0, 0, 0, 1, 0 
             0, 0, 0, 0, 0, 0, 0, 1, 1, 0   
             0, 0, 0, 0, 0, 0, 1, 0, 1, 0 
             0, 0, 0, 0, 0, 1, 0, 0, 1, 0  
             0, 0, 0, 0, 1, 0, 0, 0, 1, 0  
             0, 0, 0, 1, 0, 0, 0, 0, 1, 0 
             0, 0, 1, 0, 0, 0, 0, 0, 1, 0  
             0, 1, 1, 1, 1, 1, 1, 1, 1, 0 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

为了从1开始清空矩形,我们应该确定包含该矩形的行的开始和结束。之后,我们从矩形的第3行开始迭代,因为前两行不需要更改,并且我们找到当前行的列的开始和结束。找到这两个之后,我们把1变换成0从开始到结束。我们继续迭代到最后一行之前。

int startI = 0, endI = 0;
for (int i = 0; i < a.GetLength(0); i++)//find start and end for rows
{
    for (int j = 0; j < a.GetLength(1); j++)
    {
       if (a[i, j] == 1)
       {
           endI = i;//whenever you see 1, update it
           if (startI == 0)//Update just the first time you see 1
               startI = i;
       }
    }
}
//Make changes to all rows except the first one and the last one
for (int i = startI + 1; i < endI ; i++)
{
    int startJ = 0, endJ = 0;
    for (int j = 0; j < a.GetLength(1); j++)//find start and end for columns
    {
        if (a[i, j] == 1)
        {
            endJ = j;
            if (startJ == 0)
               startJ = j;
        }
    }
    //set all 1 to 0, except the first and the last for this row
    for (int j = startJ + 1; j < endJ ; j++)
        a[i, j] = 0;
}

我从你的代码中理解,你试图在每个元素周围找到一个正方形,然后根据正方形元素的值来决定。像这样(x是当前元素)

f  q  d 
e  x  t
s  w  g

这些变量有许多组合,所以我注释了您检查的最后一个条件

if (q != 1 || w != 1 || e != 1 || t != 1 ) 
   a[r, c] = 0;

这个条件不成立,例如当我们输入(set the x = 0错误)

0  0  1
0 x=1 1
1  1  1

注意:此代码仅适用于凸形状