/// <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(); } }
/// <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(); //} }
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); } } }
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); } } }
public IplImage BoundingRectangle() { bound = new IplImage(new CvSize(640, 480), BitDepth.U8, 3); int num = 7; CvRNG rng = new CvRNG(DateTime.Now); CvPoint[] points = new CvPoint[num]; for (int i = 0; i < num; i++) { points[i] = new CvPoint() { X = (int)(rng.RandInt() % (bound.Width)), Y = (int)(rng.RandInt() % (bound.Height)) }; Cv.DrawCircle(bound, points[i], 3, CvColor.Green, Cv.FILLED); } CvRect rect = Cv.BoundingRect(points); Cv.DrawRect(bound, rect, CvColor.Red, 2); return(bound); }