Example #1
0
        public Delaunay()
        {
            CvRect rect = new CvRect(0, 0, 600, 600);
            CvColor activeFacetColor = new CvColor(255, 0, 0);
            CvColor delaunayColor = new CvColor(0, 0, 0);
            CvColor voronoiColor = new CvColor(0, 180, 0);
            CvColor bkgndColor = new CvColor(255, 255, 255);
            Random rand = new Random();
            
            using (CvMemStorage storage = new CvMemStorage(0))
            using (IplImage img = new IplImage(rect.Size, BitDepth.U8, 3))
            using (CvWindow window = new CvWindow("delaunay"))
            {
                img.Set(bkgndColor);
                CvSubdiv2D subdiv = new CvSubdiv2D(rect, storage);
                for (int i = 0; i < 200; i++)
                {
                    CvPoint2D32f fp = new CvPoint2D32f
                    {
                        X = (float)rand.Next(5, rect.Width - 10),
                        Y = (float)rand.Next(5, rect.Height - 10)
                    };
                    LocatePoint(subdiv, fp, img, activeFacetColor);
                    window.Image = img;

                    if (CvWindow.WaitKey(100) >= 0)
                    {
                        break;
                    }
                    subdiv.Insert(fp);
                    subdiv.CalcVoronoi2D();
                    img.Set(bkgndColor);
                    DrawSubdiv(img, subdiv, delaunayColor, voronoiColor);
                    window.Image = img;
                    if (CvWindow.WaitKey(100) >= 0)
                    {
                        break;
                    }
                }
                img.Set(bkgndColor);
                PaintVoronoi(subdiv, img);
                window.Image = img;

                CvWindow.WaitKey(0);
            }
        }
Example #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="subdiv"></param>
        /// <param name="fp"></param>
        /// <param name="img"></param>
        /// <param name="active_color"></param>
        private void LocatePoint(CvSubdiv2D subdiv, CvPoint2D32f fp, IplImage img, CvScalar active_color)
        {
            CvSubdiv2DEdge e;
            CvSubdiv2DEdge e0 = 0;

            subdiv.Locate(fp, out e0);

            if (e0 != 0)
            {
                e = e0;
                do
                {
                    //Console.WriteLine(e);
                    DrawSubdivEdge(img, e, active_color);
                    e = e.GetEdge(CvNextEdgeType.NextAroundLeft);                    
                }
                while (e != e0);
            }

            DrawSubdivPoint(img, fp, active_color);
        }
Example #3
0
        /// <summary>
        /// 与えられた点に最も近い細分割の頂点を求める. 入力点を細分割内に配置するもう一つの関数である.
        /// この関数は入力された点に最も近い頂点を求める. 
        /// </summary>
        /// <param name="subdiv">ドロネー,または他の細分割</param>
        /// <param name="pt">入力点</param>
        /// <returns></returns>
#else
        /// <summary>
        /// Finds the closest subdivision vertex to given point
        /// </summary>
        /// <param name="subdiv">Delaunay or another subdivision. </param>
        /// <param name="pt">Input point. </param>
        /// <returns></returns>
#endif
        public static CvSubdiv2DPoint FindNearestPoint2D(CvSubdiv2D subdiv, CvPoint2D32f pt)
        {
            if (subdiv == null)
            {
                throw new ArgumentNullException("subdiv");
            }
            IntPtr result = NativeMethods.cvFindNearestPoint2D(subdiv.CvPtr, pt);
            return new CvSubdiv2DPoint(result);
        }
Example #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="subdiv"></param>
        /// <param name="delaunay_color"></param>
        /// <param name="voronoi_color"></param>
        private void DrawSubdiv(IplImage img, CvSubdiv2D subdiv, CvColor delaunay_color, CvColor voronoi_color)
        {
            CvSeqReader reader = new CvSeqReader();
            int total = subdiv.Edges.Total;
            int elem_size = subdiv.Edges.ElemSize;

            subdiv.Edges.StartRead(reader, false);

            for (int i = 0; i < total; i++)
            {
                //CvQuadEdge2D edge = (CvQuadEdge2D)reader.CvPtr;
                CvQuadEdge2D edge = CvQuadEdge2D.FromSeqReader(reader);

                if (Cv.IS_SET_ELEM(edge))
                {
                    DrawSubdivEdge(img, (CvSubdiv2DEdge)edge + 1, voronoi_color);
                    DrawSubdivEdge(img, (CvSubdiv2DEdge)edge, delaunay_color);
                }

                //reader.NextSeqElem(elem_size);
                Cv.NEXT_SEQ_ELEM(elem_size, reader);
            }
        }
Example #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="subdiv"></param>
        /// <param name="img"></param>
        private void PaintVoronoi(CvSubdiv2D subdiv, IplImage img)
        {
            CvSeqReader reader = new CvSeqReader();
            int total = subdiv.Edges.Total;
            int elem_size = subdiv.Edges.ElemSize;

            subdiv.CalcVoronoi2D();

            subdiv.Edges.StartRead(reader, false);

            for (int i = 0; i < total; i++)
            {
                CvQuadEdge2D edge = CvQuadEdge2D.FromSeqReader(reader);
                //CvQuadEdge2D edge = CvQuadEdge2D.FromPtr(reader.Ptr);

                if (Cv.IS_SET_ELEM(edge))
                {
                    CvSubdiv2DEdge e = edge.ToCvSubdiv2DEdge();
                    // left
                    DrawSubdivFacet(img, e.RotateEdge(RotateEdgeFlag.Rotate));
                    // right
                    DrawSubdivFacet(img, e.RotateEdge(RotateEdgeFlag.ReverseRotate));
                }
                reader.NextSeqElem(elem_size);
                //Cv.NEXT_SEQ_ELEM(elem_size, reader);
            }
        }