Example #1
0
        /// <summary>
        ///  填充断点
        /// </summary>
        /// <param name="data">二维数据</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="level">等级</param>
        protected void fileBreakpoint(ref Byte[] data, Int32 width, Int32 height, FilterLevelType level)
        {
            Byte[]    set      = null;
            Int16[][] indexSet = new Int16[][] {
                new Int16[] { 1, 4 }, new Int16[] { 1, 6 },
                new Int16[] { 3, 6 }, new Int16[] { 3, 8 },
                new Int16[] { 5, 2 }, new Int16[] { 5, 8 },
                new Int16[] { 7, 4 }, new Int16[] { 7, 2 }
            };
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    Int32 index = j * width + i;
                    if (data[index].Equals(Byte.MaxValue))
                    {
                        set = getFilterWindow3x3(ref data, i, j, width, height, Byte.MaxValue, DirectType.Clock);

                        if (level == FilterLevelType.Level01)
                        {
                            Int32 count = 0;
                            for (int x = 1; x < set.Length; x++)
                            {
                                if (set[x].Equals(Byte.MinValue))
                                {
                                    count++;
                                }
                            }

                            //if (count == 2)
                            //{
                            //    if (((set[1] == set[5] && set[5] == Byte.MinValue) && (set[3] == set[7]) && (set[7] == Byte.MaxValue)) ||
                            //        ((set[1] == set[5] && set[5] == Byte.MaxValue) && ((set[3] == set[7]) && (set[7] == Byte.MinValue))))
                            //    {
                            //        data[index] = Byte.MinValue;
                            //        continue;
                            //    }

                            //    for (int x = 0; x < indexSet.Length; x++)
                            //    {
                            //        if ((set[indexSet[x][0]] == set[indexSet[x][1]]) && set[indexSet[x][1]] == Byte.MinValue)
                            //        {
                            //            data[index] = Byte.MinValue;
                            //            break;
                            //        }
                            //    }
                            //}
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        ///  去除噪声
        /// </summary>
        /// <param name="data">二维数据</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="level">等级</param>
        protected void noiseKiller(ref Byte[] data, Int32 width, Int32 height, FilterLevelType level)
        {
            Int32[] set          = null;
            Int32   replaceValue = 1;

            Int32[] tempData = new Int32[data.Length];
            for (int i = 0; i < tempData.Length; i++)
            {
                tempData[i] = data[i];
            }

            #region 用不同编号标记独立的物体快
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    Int32 index = j * width + i;
                    if (data[index] == Byte.MinValue)
                    {
                        Byte untiData = (Byte)(Byte.MaxValue - data[index]);
                        if (level == FilterLevelType.Level01)
                        {
                            Int32 count = 0;
                            for (int x = 1; x < set.Length; x++)
                            {
                                if (set[x] == Byte.MaxValue)
                                {
                                    count++;
                                }
                            }
                            if (count == 8)
                            {
                                data[index] = Byte.MaxValue;
                            }
                        }
                        else
                        {
                            set = this.getExistFilterWindow3x3(index, width, height, DirectType.Clock);
                            Int32 value = -1;
                            for (int x = 1; x < set.Length; x++)
                            {
                                if (tempData[set[x]] != Byte.MaxValue && tempData[set[x]] != Byte.MinValue)
                                {
                                    value = tempData[set[x]];
                                }
                            }

                            replaceValue = value == -1 ? ++replaceValue : value;

                            tempData[index] = replaceValue;

                            //this.markObjectRecursion(ref data, index, width, height, Byte.MinValue, replaceValue);
                            //replaceValue++;
                        }
                    }
                }
            }
            #endregion

            if (level == FilterLevelType.Level01)
            {
                return;
            }
            #region 遍历所有标记的块, 判断是否满足条件
            Int32 border = (Int32)level;
            for (Byte x = 2; x <= replaceValue; x++)
            {
                Int32 count    = 0;
                Int32 matchRow = -1;

                #region 判断总个数是否超过边长平方
                for (int j = 0; j < height; j++)
                {
                    for (int i = 0; i < width; i++)
                    {
                        Int32 index = j * width + i;
                        if (tempData[index].Equals(x))
                        {
                            if (matchRow == -1)
                            {
                                matchRow = j;
                            }
                            count++;
                        }
                    }
                }

                // 如果总数超过边长平方 则排除
                if (count > border * border)
                {
                    continue;
                }
                #endregion

                Boolean match = true;
                #region 判断水平长度是否超过边长
                for (int j = matchRow; j < height; j++)
                {
                    count = 0;
                    for (int i = 0; i < width; i++)
                    {
                        Int32 index = j * width + i;
                        if (tempData[index].Equals(x))
                        {
                            count++;
                        }
                    }
                    if (count > border)
                    {
                        match = false;
                        break;
                    }
                }

                if (!match)
                {
                    continue;
                }
                #endregion

                #region 判断垂直宽度是否超过边长
                for (int i = 0; i < width; i++)
                {
                    count = 0;
                    for (int j = 0; j < height; j++)
                    {
                        Int32 index = j * width + i;
                        if (data[index].Equals(x))
                        {
                            count++;
                        }
                    }
                    if (count > border)
                    {
                        match = false;
                        break;
                    }
                }
                if (!match)
                {
                    continue;
                }
                #endregion

                for (int j = matchRow; j < height; j++)
                {
                    for (int i = 0; i < width; i++)
                    {
                        Int32 index = j * width + i;
                        if (tempData[index].Equals(x))
                        {
                            data[index] = Byte.MaxValue;
                        }
                    }
                }
            }
            #endregion
        }