Beispiel #1
0
        public string ExtractArea(IntField image, int x, int y, out int mostFreqSymbol, out int secondFreqSymbol)
        {
            mostFreqSymbol = secondFreqSymbol = int.MinValue;

            if (x < _width - 1 - _xpos || x >= image.Width - _xpos || y < _height - 1)
            {
                return(null);
            }

            IntField area = image.Extract(x - _width + 1 + _xpos, y - _height + 1, _width, _height);

            int[] data = new int[area.Data.Length - 1 - _xpos];
            Array.Copy(area.Data, data, data.Length);

            // Find the smallest most frequent and second most frequent symbols
            var   counts     = CodecUtil.CountValues(data);
            ulong mostFreq   = ulong.MinValue;
            ulong secondFreq = ulong.MinValue;

            for (int i = 0; i < counts.Length; i++)
            {
                if (counts[i] > mostFreq)
                {
                    secondFreq       = mostFreq;
                    secondFreqSymbol = mostFreqSymbol;
                    mostFreq         = counts[i];
                    mostFreqSymbol   = i;
                }
                else if (counts[i] > secondFreq)
                {
                    secondFreq       = counts[i];
                    secondFreqSymbol = i;
                }
            }

            // Convert to string
            StringBuilder SB = new StringBuilder(data.Length);

            foreach (var sym in data)
            {
                SB.Append(sym == mostFreqSymbol ? "1" : "0");
            }
            return(SB.ToString());
        }