Beispiel #1
0
        public Contour()
        {
            // cvContourArea, cvArcLength
            // 輪郭によって区切られた領域の面積と,輪郭の長さを求める
            
            const int SIZE = 500;

            // (1)画像を確保し初期化する
            using (CvMemStorage storage = new CvMemStorage())
            using (IplImage img = new IplImage(SIZE, SIZE, BitDepth.U8, 3))
            {
                img.Zero();
                // (2)点列を生成する 
                CvSeq<CvPoint> points = new CvSeq<CvPoint>(SeqType.PolyLine, storage);
                CvRNG rng = new CvRNG((ulong)DateTime.Now.Ticks);
                double scale = rng.RandReal() + 0.5;
                CvPoint pt0 = new CvPoint
                {
                    X = (int)(Math.Cos(0) * SIZE / 4 * scale + SIZE / 2),
                    Y = (int)(Math.Sin(0) * SIZE / 4 * scale + SIZE / 2)
                };
                img.Circle(pt0, 2, CvColor.Green);
                points.Push(pt0);
                for (int i = 1; i < 20; i++)
                {
                    scale = rng.RandReal() + 0.5;
                    CvPoint pt1 = new CvPoint
                    {
                        X = (int)(Math.Cos(i * 2 * Math.PI / 20) * SIZE / 4 * scale + SIZE / 2),
                        Y = (int)(Math.Sin(i * 2 * Math.PI / 20) * SIZE / 4 * scale + SIZE / 2)
                    };
                    img.Line(pt0, pt1, CvColor.Green, 2);
                    pt0.X = pt1.X;
                    pt0.Y = pt1.Y;
                    img.Circle(pt0, 3, CvColor.Green, Cv.FILLED);
                    points.Push(pt0);
                }
                img.Line(pt0, points.GetSeqElem(0).Value, CvColor.Green, 2);
                // (3)包含矩形,面積,長さを求める
                CvRect rect = points.BoundingRect(false);
                double area = points.ContourArea();
                double length = points.ArcLength(CvSlice.WholeSeq, 1);
                // (4)結果を画像に書き込む
                img.Rectangle(new CvPoint(rect.X, rect.Y), new CvPoint(rect.X + rect.Width, rect.Y + rect.Height), CvColor.Red, 2);
                string text_area = string.Format("Area:   wrect={0}, contour={1}", rect.Width * rect.Height, area);
                string text_length = string.Format("Length: rect={0}, contour={1}", 2 * (rect.Width + rect.Height), length);
                using (CvFont font = new CvFont(FontFace.HersheySimplex, 0.7, 0.7, 0, 1, LineType.AntiAlias))
                {
                    img.PutText(text_area, new CvPoint(10, img.Height - 30), font, CvColor.White);
                    img.PutText(text_length, new CvPoint(10, img.Height - 10), font, CvColor.White);
                }
                // (5)画像を表示,キーが押されたときに終了 
                using (CvWindow window = new CvWindow("BoundingRect", WindowMode.AutoSize))
                {
                    window.Image = img;
                    CvWindow.WaitKey(0);
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="_modelPoints"></param>
 /// <param name="_modelSize"></param>
 /// <param name="_maxBasicSolutions"></param>
 public CvModelEstimator2(int _modelPoints, CvSize _modelSize, int _maxBasicSolutions)
 {
     modelPoints = _modelPoints;
     modelSize = _modelSize;
     maxBasicSolutions = _maxBasicSolutions;
     checkPartialSubsets = true;
     rng = new CvRNG(-1);
 }
Beispiel #3
0
        public BoundingRect()
        {
            // cvBoundingRect 
            // 点列を包含する矩形を求める

            // (1)画像とメモリストレージを確保し初期化する
            // (メモリストレージは、CvSeqを使わないのであれば不要)
            using (IplImage img = new IplImage(640, 480, BitDepth.U8, 3))
            using (CvMemStorage storage = new CvMemStorage(0))
            {
                img.Zero();
                CvRNG rng = new CvRNG(DateTime.Now);
                // (2)点列を生成する
                ///*
                // お手軽な方法 (普通の配列を使う)
                CvPoint[] points = new CvPoint[50];
                for (int i = 0; i < 50; i++)
                {
                    points[i] = new CvPoint()
                    {
                        X = (int)(rng.RandInt() % (img.Width / 2) + img.Width / 4),
                        Y = (int)(rng.RandInt() % (img.Height / 2) + img.Height / 4)
                    };
                    img.Circle(points[i], 3, new CvColor(0, 255, 0), Cv.FILLED);
                }
                //*/
                /*
                // サンプルに準拠した方法 (CvSeqを使う)
                CvSeq points = new CvSeq(SeqType.EltypePoint, CvSeq.SizeOf, CvPoint.SizeOf, storage);
                for (int i = 0; i < 50; i++) {
                    CvPoint pt = new CvPoint();
                    pt.X = (int)(rng.RandInt() % (img.Width / 2) + img.Width / 4);
                    pt.Y = (int)(rng.RandInt() % (img.Height / 2) + img.Height / 4);
                    points.Push(pt);
                    img.Circle(pt, 3, new CvColor(0, 255, 0), Cv.FILLED);
                }
                //*/
                // (3)点列を包含する矩形を求めて描画する
                CvRect rect = Cv.BoundingRect(points);
                img.Rectangle(new CvPoint(rect.X, rect.Y), new CvPoint(rect.X + rect.Width, rect.Y + rect.Height), new CvColor(255, 0, 0), 2);
                // (4)画像の表示,キーが押されたときに終了 
                using (CvWindow w = new CvWindow("BoundingRect", WindowMode.AutoSize, img))
                {
                    CvWindow.WaitKey(0);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す.
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(CvArr samples, int clusterCount, CvArr labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr centers, out double compactness)
        {
            if (samples == null)
            {
                throw new ArgumentNullException("samples");
            }
            if (labels == null)
            {
                throw new ArgumentNullException("labels");
            }

            if (rng == null)
            {
                rng = new CvRNG();
            }
            IntPtr centersPtr = (centers != null) ? centers.CvPtr : IntPtr.Zero;

            UInt64 rngValue = rng.Seed;
            int    result   = NativeMethods.cvKMeans2(samples.CvPtr, clusterCount, labels.CvPtr, termcrit, attemps, ref rngValue, flag, centersPtr, out compactness);

            rng.Seed = rngValue;
            KeepAlive(samples, labels, centers);
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す. 
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="cluster_count">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="_centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="cluster_count">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="_centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(CvArr samples, int cluster_count, CvArr labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr _centers, out double compactness)
        {
            if (samples == null)
                throw new ArgumentNullException("samples");
            if (labels == null)
                throw new ArgumentNullException("labels");

            if (rng == null)
            {
                rng = new CvRNG();
            }
            IntPtr centersPtr = (_centers != null) ? _centers.CvPtr : IntPtr.Zero;

            UInt64 rngValue = rng.Seed;
            int result = CvInvoke.cvKMeans2(samples.CvPtr, cluster_count, labels.CvPtr, termcrit, attemps, ref rngValue, flag, centersPtr, out compactness);
            rng.Seed = rngValue;

            return result;
        }
Beispiel #6
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す. 
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="cluster_count">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="_centers"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="cluster_count">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="_centers"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(CvArr samples, int cluster_count, CvArr labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr _centers)
        {
            double compactness;
            return KMeans2(samples, cluster_count, labels, termcrit, attemps, rng, flag, _centers, out compactness);
        }
Beispiel #7
0
        /// <summary>
        /// 一様または正規分布の乱数で出力配列を埋める 
        /// </summary>
        /// <param name="rng">cvRNGによって初期化されたRNGの状態</param>
        /// <param name="distType">分布のタイプ</param>
        /// <param name="param1">分布の第一パラメータ.一様分布では,発生する乱数の下限値(この値を含む)である. 正規分布では,乱数の平均値である.</param>
        /// <param name="param2">分布の第二パラメータ.一様分布では,発生する乱数の上限値(この値は含まない)である. 正規分布では,乱数の標準偏差である.</param>
#else
        /// <summary>
        /// Fills array with random numbers and updates the RNG state
        /// </summary>
        /// <param name="rng">RNG state initialized by cvRNG. </param>
        /// <param name="distType">Distribution type.</param>
        /// <param name="param1">The first parameter of distribution. In case of uniform distribution it is the inclusive lower boundary of random numbers range. In case of normal distribution it is the mean value of random numbers. </param>
        /// <param name="param2">The second parameter of distribution. In case of uniform distribution it is the exclusive upper boundary of random numbers range. In case of normal distribution it is the standard deviation of random numbers. </param>
#endif
        public void RandArr(CvRNG rng, DistributionType distType, CvScalar param1, CvScalar param2)
        {
            Cv.RandArr(rng, this, distType, param1, param2);
        }
Beispiel #8
0
        /// <summary>
        /// 一様または正規分布の乱数で出力配列を埋める
        /// </summary>
        /// <param name="rng">cvRNGによって初期化されたRNGの状態</param>
        /// <param name="distType">分布のタイプ</param>
        /// <param name="param1">分布の第一パラメータ.一様分布では,発生する乱数の下限値(この値を含む)である. 正規分布では,乱数の平均値である.</param>
        /// <param name="param2">分布の第二パラメータ.一様分布では,発生する乱数の上限値(この値は含まない)である. 正規分布では,乱数の標準偏差である.</param>
#else
        /// <summary>
        /// Fills array with random numbers and updates the RNG state
        /// </summary>
        /// <param name="rng">RNG state initialized by cvRNG. </param>
        /// <param name="distType">Distribution type.</param>
        /// <param name="param1">The first parameter of distribution. In case of uniform distribution it is the inclusive lower boundary of random numbers range. In case of normal distribution it is the mean value of random numbers. </param>
        /// <param name="param2">The second parameter of distribution. In case of uniform distribution it is the exclusive upper boundary of random numbers range. In case of normal distribution it is the standard deviation of random numbers. </param>
#endif
        public void RandArr(CvRNG rng, DistributionType distType, CvScalar param1, CvScalar param2)
        {
            Cv.RandArr(rng, this, distType, param1, param2);
        }
Beispiel #9
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す.
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(Array samples, MatrixType samplesType, int clusterCount, int[] labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr centers, out double compactness)
        {
            if (samples == null)
            {
                throw new ArgumentNullException("samples");
            }

            using (CvMat samplesMat = new CvMat(labels.Length, 1, samplesType, samples, false))
                using (CvMat labelsMat = new CvMat(labels.Length, 1, MatrixType.S32C1, labels, false))
                {
                    return(KMeans2(samplesMat, clusterCount, labelsMat, termcrit, attemps, rng, flag, centers, out compactness));
                }
        }
Beispiel #10
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す.
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(CvArr samples, int clusterCount, CvArr labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag)
        {
            double compactness;

            return(KMeans2(samples, clusterCount, labels, termcrit, attemps, rng, flag, null, out compactness));
        }
Beispiel #11
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す. 
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <param name="compactness"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(Array samples, MatrixType samplesType, int clusterCount, int[] labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr centers, out double compactness)
        {
            if (samples == null)
                throw new ArgumentNullException("samples");

            using (CvMat samplesMat = new CvMat(labels.Length, 1, samplesType, samples, false))
            using (CvMat labelsMat = new CvMat(labels.Length, 1, MatrixType.S32C1, labels, false))
            {
                return KMeans2(samplesMat, clusterCount, labelsMat, termcrit, attemps, rng, flag, centers, out compactness);
            }
        }
Beispiel #12
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す. 
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(Array samples, MatrixType samplesType, int clusterCount, int[] labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr centers)
        {
            double compactness;
            return KMeans2(samples, samplesType, clusterCount, labels, termcrit, attemps, rng, flag, centers, out compactness);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="seed"></param>
 public virtual void SetSeed(long seed)
 {
     rng = new CvRNG(seed);
 }
Beispiel #14
0
        /// <summary>
        /// マップのシーケンスのファイルストレージへの書き込み
        /// </summary>
        /// <param name="fileName">書きこむXML or YAMLファイル</param>
        private static void SampleFileStorageWriteSeq(string fileName)
        {
            // cvStartWriteStruct, cvEndWriteStruct
            // 二つのエントリを持つマップのシーケンスをファイルに保存する

            const int size = 20;
            CvRNG rng = new CvRNG((ulong)DateTime.Now.Ticks);
            CvPoint[] pt = new CvPoint[size];
            // (1)点列の作成
            for (int i = 0; i < pt.Length; i++)
            {
                pt[i].X = (int)rng.RandInt(100);
                pt[i].Y = (int)rng.RandInt(100);
            }
            // (2)マップのシーケンスとして点列を保存
            using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write))
            {
                fs.StartWriteStruct("points", NodeType.Seq);
                for (int i = 0; i < pt.Length; i++)
                {
                    fs.StartWriteStruct(null, NodeType.Map | NodeType.Flow);
                    fs.WriteInt("x", pt[i].X);
                    fs.WriteInt("y", pt[i].Y);
                    fs.EndWriteStruct();
                }
                fs.EndWriteStruct();
            }
            // (3)書きこんだyamlファイルを開く
            //using (Process p = Process.Start(fileName)) {
            //    p.WaitForExit();
            //} 
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="seed"></param>
 public virtual void SetSeed(long seed)
 {
     rng = new CvRNG(seed);
 }
Beispiel #16
0
        /// <summary>
        /// ベクトル集合を,与えられたクラスタ数に分割する.
        /// 入力サンプルを各クラスタに分類するために cluster_count 個のクラスタの中心を求める k-means 法を実装する.
        /// 出力 labels(i) は,配列 samples のi番目の行のサンプルが属するクラスタのインデックスを表す.
        /// </summary>
        /// <param name="samples">浮動小数点型の入力サンプル行列.1行あたり一つのサンプル.</param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">集合を分割するクラスタ数</param>
        /// <param name="labels">出力の整数ベクトル.すべてのサンプルについて,それぞれがどのクラスタに属しているかが保存されている.</param>
        /// <param name="termcrit">最大繰り返し数と(または),精度(1ループでの各クラスタ中心位置移動距離)の指定</param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <returns></returns>
#else
        /// <summary>
        /// Splits set of vectors by given number of clusters
        /// </summary>
        /// <param name="samples">Floating-point matrix of input samples, one row per sample. </param>
        /// <param name="samplesType"></param>
        /// <param name="clusterCount">Number of clusters to split the set by. </param>
        /// <param name="labels">Output integer vector storing cluster indices for every sample. </param>
        /// <param name="termcrit">Specifies maximum number of iterations and/or accuracy (distance the centers move by between the subsequent iterations). </param>
        /// <param name="attemps"></param>
        /// <param name="rng"></param>
        /// <param name="flag"></param>
        /// <param name="centers"></param>
        /// <returns></returns>
#endif
        public static int KMeans2(Array samples, MatrixType samplesType, int clusterCount, int[] labels, CvTermCriteria termcrit, int attemps, CvRNG rng, KMeansFlag flag, CvArr centers)
        {
            double compactness;

            return(KMeans2(samples, samplesType, clusterCount, labels, termcrit, attemps, rng, flag, centers, out compactness));
        }
Beispiel #17
0
        public SVM()
        {
            // CvSVM
            // SVMを利用して2次元ベクトルの3クラス分類問題を解く

            const int S = 1000;
            const int SIZE = 400;
            CvRNG rng = new CvRNG((ulong)DateTime.Now.Ticks);

            // (1)画像領域の確保と初期化
            using (IplImage img = new IplImage(SIZE, SIZE, BitDepth.U8, 3))
            {
                img.Zero();
                // (2)学習データの生成
                CvPoint[] pts = new CvPoint[S];
                int[] res = new int[S];
                for (int i = 0; i < S; i++)
                {
                    pts[i].X = (int)(rng.RandInt() % SIZE);
                    pts[i].Y = (int)(rng.RandInt() % SIZE);
                    if (pts[i].Y > 50 * Math.Cos(pts[i].X * Cv.PI / 100) + 200)
                    {
                        img.Line(new CvPoint(pts[i].X - 2, pts[i].Y - 2), new CvPoint(pts[i].X + 2, pts[i].Y + 2), new CvColor(255, 0, 0));
                        img.Line(new CvPoint(pts[i].X + 2, pts[i].Y - 2), new CvPoint(pts[i].X - 2, pts[i].Y + 2), new CvColor(255, 0, 0));
                        res[i] = 1;
                    }
                    else
                    {
                        if (pts[i].X > 200)
                        {
                            img.Line(new CvPoint(pts[i].X - 2, pts[i].Y - 2), new CvPoint(pts[i].X + 2, pts[i].Y + 2), new CvColor(0, 255, 0));
                            img.Line(new CvPoint(pts[i].X + 2, pts[i].Y - 2), new CvPoint(pts[i].X - 2, pts[i].Y + 2), new CvColor(0, 255, 0));
                            res[i] = 2;
                        }
                        else
                        {
                            img.Line(new CvPoint(pts[i].X - 2, pts[i].Y - 2), new CvPoint(pts[i].X + 2, pts[i].Y + 2), new CvColor(0, 0, 255));
                            img.Line(new CvPoint(pts[i].X + 2, pts[i].Y - 2), new CvPoint(pts[i].X - 2, pts[i].Y + 2), new CvColor(0, 0, 255));
                            res[i] = 3;
                        }
                    }
                }

                // (3)学習データの表示
                Cv.NamedWindow("SVM", WindowMode.AutoSize);
                Cv.ShowImage("SVM", img);
                Cv.WaitKey(0);

                // (4)学習パラメータの生成
                float[] data = new float[S * 2];
                for (int i = 0; i < S; i++)
                {
                    data[i * 2] = ((float)pts[i].X) / SIZE;
                    data[i * 2 + 1] = ((float)pts[i].Y) / SIZE;
                }

                // (5)SVMの学習
                using (CvSVM svm = new CvSVM())
                {
                    CvMat data_mat = new CvMat(S, 2, MatrixType.F32C1, data);
                    CvMat res_mat = new CvMat(S, 1, MatrixType.S32C1, res);
                    CvTermCriteria criteria = new CvTermCriteria(1000, float.Epsilon);
                    CvSVMParams param = new CvSVMParams(SVMType.CSvc, SVMKernelType.Rbf, 10.0, 8.0, 1.0, 10.0, 0.5, 0.1, null, criteria);
                    svm.Train(data_mat, res_mat, null, null, param);

                    // (6)学習結果の描画
                    for (int i = 0; i < SIZE; i++)
                    {
                        for (int j = 0; j < SIZE; j++)
                        {
                            float[] a = { (float)j / SIZE, (float)i / SIZE };
                            CvMat m = new CvMat(1, 2, MatrixType.F32C1, a);
                            float ret = svm.Predict(m);
                            CvColor color = new CvColor();
                            switch ((int)ret)
                            {
                                case 1:
                                    color = new CvColor(100, 0, 0); break;
                                case 2:
                                    color = new CvColor(0, 100, 0); break;
                                case 3:
                                    color = new CvColor(0, 0, 100); break;
                            }
                            img[i, j] = color;
                        }
                    }

                    // (7)トレーニングデータの再描画
                    for (int i = 0; i < S; i++)
                    {
                        CvColor color = new CvColor();
                        switch (res[i])
                        {
                            case 1:
                                color = new CvColor(255, 0, 0); break;
                            case 2:
                                color = new CvColor(0, 255, 0); break;
                            case 3:
                                color = new CvColor(0, 0, 255); break;
                        }
                        img.Line(new CvPoint(pts[i].X - 2, pts[i].Y - 2), new CvPoint(pts[i].X + 2, pts[i].Y + 2), color);
                        img.Line(new CvPoint(pts[i].X + 2, pts[i].Y - 2), new CvPoint(pts[i].X - 2, pts[i].Y + 2), color);
                    }

                    // (8)サポートベクターの描画
                    int sv_num = svm.GetSupportVectorCount();
                    for (int i = 0; i < sv_num; i++)
                    {
                        var support = svm.GetSupportVector(i);
                        img.Circle(new CvPoint((int)(support[0] * SIZE), (int)(support[1] * SIZE)), 5, new CvColor(200, 200, 200));
                    }

                    // (9)画像の表示
                    Cv.NamedWindow("SVM", WindowMode.AutoSize);
                    Cv.ShowImage("SVM", img);
                    Cv.WaitKey(0);
                    Cv.DestroyWindow("SVM");

                }
            }

        }
Beispiel #18
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">書きこむXML or YAMLファイル</param>
        private static void SampleFileStorageWriteSeq(string fileName)
        {
            // cvStartWriteStruct, cvEndWriteStruct

            const int size = 20;
            CvRNG rng = new CvRNG((ulong)DateTime.Now.Ticks);
            CvPoint[] pt = new CvPoint[size];

            for (int i = 0; i < pt.Length; i++)
            {
                pt[i].X = (int)rng.RandInt(100);
                pt[i].Y = (int)rng.RandInt(100);
            }

            using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write))
            {
                fs.StartWriteStruct("points", NodeType.Seq);
                for (int i = 0; i < pt.Length; i++)
                {
                    fs.StartWriteStruct(null, NodeType.Map | NodeType.Flow);
                    fs.WriteInt("x", pt[i].X);
                    fs.WriteInt("y", pt[i].Y);
                    fs.EndWriteStruct();
                }
                fs.EndWriteStruct();
            }
        }