Пример #1
0
        /// <summary>
        /// vraci region ktery je nejlepsi k slouceni s timto
        /// </summary>
        /// <param name="region"></param>
        /// <returns></returns>
        private RegionVO BestRegionToFuse(RegionVO region, CanvasPixel canvasPixel, int maxColorDiff, HashSet <RegionVO> fuseIgnore)
        {
            //regionsAround.Clear();
            Dictionary <RegionVO, int> regionsAround = new Dictionary <RegionVO, int>();

            foreach (int pixelIndex in region.Pixels)
            {
                int pixelIndexborder = pixelIndex - 1;

                BestRegionToFuse_DetectRegionFromPixel(pixelIndexborder, region, regionsAround, fuseIgnore);
                pixelIndexborder = pixelIndex + 1;
                BestRegionToFuse_DetectRegionFromPixel(pixelIndexborder, region, regionsAround, fuseIgnore);
                pixelIndexborder = pixelIndex - canvasPixel.Width;
                BestRegionToFuse_DetectRegionFromPixel(pixelIndexborder, region, regionsAround, fuseIgnore);
                pixelIndexborder = pixelIndex + canvasPixel.Width;
                BestRegionToFuse_DetectRegionFromPixel(pixelIndexborder, region, regionsAround, fuseIgnore);
            }

            //RegionVO result = null;
            //int resultCount = int.MaxValue;
            //foreach (var item in regionsAround)
            //{
            //    if (item.Value < resultCount)
            //    {
            //        resultCount = item.Value;
            //        result = item.Key;
            //    }
            //}

            RegionVO result      = null;
            int      resultDiff  = int.MaxValue;
            int      resultCount = -1;
            Pixel    re          = region.Color;

            re.ConvertRGBToHSL();
            foreach (var item in regionsAround)
            {
                // int diff = BasicHelpers.FastAbs(item.Key.Color.IntClearAlpha() - region.Color.IntClearAlpha());

                //int maxDiff = Pixel.MaxDiff(item.Key.Color, region.Color);
                //if (maxDiff > (maxColorDiff / 2) + 1) continue;
                Pixel it = item.Key.Color;
                it.ConvertRGBToHSL();

                int diff = Pixel.SumAbsDiff(item.Key.Color, region.Color);

                int diff2_min = BasicHelpers.Min(re.CH, it.CH);

                int  diff2     = BasicHelpers.FastAbs(re.CH - it.CH);
                bool firstLess = re.CH < it.CH;

                int diff2absl = BasicHelpers.FastAbs(re.CL - it.CL);

                Pixel p = new Pixel();
                p.CR = 255;
                p.CG = 255;
                p.CB = 255;

                p.ConvertRGBToHSL();

                if (diff <= maxColorDiff)
                {
                    if (!((re.CL < 50 && it.CL < 50) ||
                          (re.CL > 205 && it.CL > 205)))
                    {
                        if (diff2absl > 160)
                        {
                            continue;
                        }

                        if (!(((diff2_min + diff2 + 1) * item.Key.Pixels.Length <= (diff2_min + 1) * region.Pixels.Length && firstLess) ||
                              ((diff2 + 1) * item.Key.Pixels.Length <= (diff2_min + diff2_min + 1) * region.Pixels.Length && !firstLess)
                              ))
                        {
                            continue;
                        }
                    }

                    if (
                        //       ((diff2+1)*item.Key.Pixels.Count) < resultDiff
                        diff < resultDiff
                        //item.Value > resultCount
                        )
                    {
                        resultDiff =
                            diff;
                        //((diff2 + 1) * item.Key.Pixels.Count);
                        result      = item.Key;
                        resultCount = item.Value;
                    }
                }
            }

            return(result);
        }