Esempio n. 1
0
        /// <summary>
        /// Does the internal processing to actually create the breaks.
        /// </summary>
        /// <param name="numBreaks">number of breaks</param>
        /// <param name="tuples">asc ordered input of values and their occurence counts</param>
        /// <returns>created breaks</returns>
        private static List <double> ClassifyByJenksFisher(int numBreaks, List <ValueCountTuple> tuples)
        {
            var breaksArray = new List <double>(numBreaks);

            if (numBreaks == 0)
            {
                return(breaksArray);
            }
            // avoid array <-> list conversations
            breaksArray.AddRange(Enumerable.Repeat(0.0d, numBreaks));

            var classificator = new JenksFisher(tuples, numBreaks);

            if (numBreaks > 1)
            {
                // runs the actual calculation
                classificator.CalculateAll();
                int lastClassBreakIndex = classificator.FindMaxBreakIndex(classificator._bufferSize - 1, 0, classificator._bufferSize);
                while (--numBreaks != 0)
                {
                    // assign the break values to the result
                    breaksArray[numBreaks] = tuples[lastClassBreakIndex + numBreaks].Value;

                    if (numBreaks > 1)
                    {
                        classificator._classBreaksIndex -= classificator._bufferSize;
                        lastClassBreakIndex              = classificator._classBreaks[classificator._classBreaksIndex + lastClassBreakIndex];
                    }
                }
            }

            breaksArray[0] = tuples[0].Value; // break for the first class is the minimum of the dataset.
            return(breaksArray);
        }
Esempio n. 2
0
        public bool TestFl()
        {
            //get fl contours
            var rootDir        = @"P:\Projects\N3 Imaging\Images\09042018_jewerly\test";
            var files          = Directory.GetFiles(rootDir, "*.bmp", SearchOption.TopDirectoryOnly);
            Mat combinedFlMask = null;

            foreach (var file in files)
            {
                Mat fl     = Cv2.ImRead(file);
                Mat fl_hls = new Mat();
                Cv2.CvtColor(fl, fl_hls, ColorConversionCodes.BGR2HLS);

                var fl_hls_split = Cv2.Split(fl_hls);
                Mat H = fl_hls_split[0].Clone(), L, S;
                H.ConvertTo(H, MatType.CV_16UC1);

                H = 2 * H;                         //opencv H is from 0 .. 180
                L = (100 * fl_hls_split[1]) / 255; //L and S range is from 0..255
                S = (100 * fl_hls_split[2]) / 255; //L and S range is from 0..255

                var l = (uint)Cv2.Mean(L)[0];
                var s = (uint)Cv2.Mean(S)[0];
                var h = (uint)Cv2.Mean(H)[0];

                Debug.WriteLine(H.Dump());

                var L_vector_ptr = (L.Reshape(1, L.Rows * L.Cols)).Data;
                var L_vector     = new byte[L.Rows * L.Cols];
                Marshal.Copy(L_vector_ptr, L_vector, 0, L.Rows * L.Cols);

                //Debug.WriteLine(string.Join("\n", L_vector));

                var jenksBreaks =
                    JenksFisher.CreateJenksFisherBreaksArray(L_vector.Select(p => (double)p).ToList(), 4);

                var jenksBreaks1 = JenksFisher.getJenksBreaks(L_vector.Select(p => (double)p).ToList(), 4);

                Mat threshold = new Mat();
                Cv2.InRange(fl_hls, new Scalar(0, 0, 0), new Scalar(255, jenksBreaks[2], 255), threshold);
                Cv2.ImShow("threshold", threshold);
                Cv2.WaitKey();
                Cv2.DestroyAllWindows();

                //Debug.WriteLine(fl_hls_split[1].Dump());
            }


            Cv2.WaitKey();
            Cv2.DestroyAllWindows();

            return(true);
        }