/// <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="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"></param> private static void SampleFileStorageReadSeq(string fileName) { // cvGetHashedKey, cvGetFileNode using (CvFileStorage fs = new CvFileStorage("sequence.yml", null, FileStorageMode.Read)) { CvStringHashNode xKey = fs.GetHashedKey("x", true); CvStringHashNode yKey = fs.GetHashedKey("y", true); CvFileNode points = fs.GetFileNodeByName(null, "points"); if ((points.Tag & NodeType.Seq) != 0) { CvSeq seq = points.DataSeq; int total = seq.Total; CvSeqReader reader = new CvSeqReader(); seq.StartRead(reader, false); for (int i = 0; i < total; i++) { CvFileNode pt = CvFileNode.FromPtr(reader.Ptr); int x = fs.ReadIntByName(pt, "x", 0); int y = fs.ReadIntByName(pt, "y", 0); Cv.NEXT_SEQ_ELEM(seq.ElemSize, reader); Console.WriteLine("{0}: ({1}, {2})", i, x, y); } } } Console.ReadKey(); }
public Undistort() { using (IplImage srcImg = new IplImage(FilePath.Image.Distortion, LoadMode.Color)) using (IplImage dstImg = srcImg.Clone()) { CvMat intrinsic, distortion; using (CvFileStorage fs = new CvFileStorage(FilePath.Text.Camera, null, FileStorageMode.Read)) { CvFileNode param = fs.GetFileNodeByName(null, "intrinsic"); intrinsic = fs.Read <CvMat>(param); param = fs.GetFileNodeByName(null, "distortion"); distortion = fs.Read <CvMat>(param); } Cv.Undistort2(srcImg, dstImg, intrinsic, distortion); using (new CvWindow("Distortion", WindowMode.AutoSize, srcImg)) using (new CvWindow("Undistortion", WindowMode.AutoSize, dstImg)) { CvWindow.WaitKey(0); } intrinsic.Dispose(); distortion.Dispose(); } }
/// <summary> /// 画像データのファイルストレージへの書き込み /// </summary> /// <param name="fileName">書きこむXML or YAMLファイル</param> private static void SampleFileStorageWriteImage(string fileName) { // cvWrite, cvWriteComment // IplImage構造体の情報をファイルに保存する // (1)画像を読み込む using (IplImage colorImg = new IplImage(Const.ImageLenna, LoadMode.Color)) using (IplImage grayImg = new IplImage(colorImg.Size, BitDepth.U8, 1)) { // (2)ROIの設定と二値化処理 colorImg.CvtColor(grayImg, ColorConversion.BgrToGray); CvRect roi = new CvRect(0, 0, colorImg.Width / 2, colorImg.Height / 2); grayImg.SetROI(roi); colorImg.SetROI(roi); grayImg.Threshold(grayImg, 90, 255, ThresholdType.Binary); // (3)xmlファイルへの書き出し using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write)) { fs.WriteComment("This is a comment line.", false); fs.Write("color_img", colorImg); fs.StartNextStream(); fs.Write("gray_img", grayImg); } // (4)書きこんだxmlファイルを開く //using (Process p = Process.Start(fileName)) { // p.WaitForExit(); //} } }
public Undistort() { // cvUndistort2 // キャリブレーションデータを利用して,歪みを補正する // (1)補正対象となる画像の読み込み using (IplImage srcImg = new IplImage(Const.ImageDistortion, LoadMode.Color)) using (IplImage dstImg = srcImg.Clone()) { // (2)パラメータファイルの読み込み CvMat intrinsic, distortion; using (CvFileStorage fs = new CvFileStorage(Const.XmlCamera, null, FileStorageMode.Read)) { CvFileNode param = fs.GetFileNodeByName(null, "intrinsic"); intrinsic = fs.Read <CvMat>(param); param = fs.GetFileNodeByName(null, "distortion"); distortion = fs.Read <CvMat>(param); } // (3)歪み補正 Cv.Undistort2(srcImg, dstImg, intrinsic, distortion); // (4)画像を表示,キーが押されたときに終了 using (CvWindow w1 = new CvWindow("Distortion", WindowMode.AutoSize, srcImg)) using (CvWindow w2 = new CvWindow("Undistortion", WindowMode.AutoSize, dstImg)) { CvWindow.WaitKey(0); } intrinsic.Dispose(); distortion.Dispose(); } }
public Undistort() { // cvUndistort2 // キャリブレーションデータを利用して,歪みを補正する // (1)補正対象となる画像の読み込み using (IplImage srcImg = new IplImage(Const.ImageDistortion, LoadMode.Color)) using (IplImage dstImg = srcImg.Clone()) { // (2)パラメータファイルの読み込み CvMat intrinsic, distortion; using (CvFileStorage fs = new CvFileStorage(Const.XmlCamera, null, FileStorageMode.Read)) { CvFileNode param = fs.GetFileNodeByName(null, "intrinsic"); intrinsic = fs.Read<CvMat>(param); param = fs.GetFileNodeByName(null, "distortion"); distortion = fs.Read<CvMat>(param); } // (3)歪み補正 Cv.Undistort2(srcImg, dstImg, intrinsic, distortion); // (4)画像を表示,キーが押されたときに終了 using (CvWindow w1 = new CvWindow("Distortion", WindowMode.AutoSize, srcImg)) using (CvWindow w2 = new CvWindow("Undistortion", WindowMode.AutoSize, dstImg)) { CvWindow.WaitKey(0); } intrinsic.Dispose(); distortion.Dispose(); } }
public Undistort() { using (IplImage srcImg = new IplImage(FilePath.Image.Distortion, LoadMode.Color)) using (IplImage dstImg = srcImg.Clone()) { CvMat intrinsic, distortion; using (CvFileStorage fs = new CvFileStorage(FilePath.Text.Camera, null, FileStorageMode.Read)) { CvFileNode param = fs.GetFileNodeByName(null, "intrinsic"); intrinsic = fs.Read<CvMat>(param); param = fs.GetFileNodeByName(null, "distortion"); distortion = fs.Read<CvMat>(param); } Cv.Undistort2(srcImg, dstImg, intrinsic, distortion); using (new CvWindow("Distortion", WindowMode.AutoSize, srcImg)) using (new CvWindow("Undistortion", WindowMode.AutoSize, dstImg)) { CvWindow.WaitKey(0); } intrinsic.Dispose(); distortion.Dispose(); } }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="fs"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="fs"></param> #endif public virtual void Write(CvFileStorage fs) { if (fs == null) { throw new ArgumentNullException(nameof(fs)); } NativeMethods.ml_CvDTree_write(ptr, fs.CvPtr); }
/// <summary> /// The full constructor wrapping CvFileNode structure. /// </summary> /// <param name="fs"></param> /// <param name="node"></param> public FileNode(CvFileStorage fs, CvFileNode node) { if (fs == null) throw new ArgumentNullException("fs"); if (node == null) throw new ArgumentNullException("node"); NativeMethods.core_FileNode_new2(fs.CvPtr, node.CvPtr); }
/// <summary> /// Returns pointer to the underlying C FileStorage structure /// </summary> /// <returns></returns> public CvFileStorage ToLegacy() { IntPtr p = NativeMethods.core_FileStorage_toLegacy(ptr); var ret = new CvFileStorage(p) { IsEnabledDispose = false }; return(ret); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage fs, CvFileNode node) { if (fs == null) { throw new ArgumentNullException("fs"); } if (node == null) { throw new ArgumentNullException("node"); } NativeMethods.ml_CvRTrees_read(ptr, fs.CvPtr, node.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="fs"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="fs"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage fs, string name) { if (fs == null) { throw new ArgumentNullException(nameof(fs)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } NativeMethods.ml_CvDTree_write(ptr, fs.CvPtr, name); }
/// <summary> /// The full constructor wrapping CvFileNode structure. /// </summary> /// <param name="fs"></param> /// <param name="node"></param> public FileNode(CvFileStorage fs, CvFileNode node) { if (fs == null) { throw new ArgumentNullException(nameof(fs)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } NativeMethods.core_FileNode_new2(fs.CvPtr, node.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage storage, string name) { if (storage == null) { throw new ArgumentNullException(nameof(storage)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } NativeMethods.ml_CvNormalBayesClassifier_write(ptr, storage.CvPtr, name); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage storage, CvFileNode node) { if (storage == null) { throw new ArgumentNullException("storage"); } if (node == null) { throw new ArgumentNullException("node"); } NativeMethods.ml_CvBoost_read(ptr, storage.CvPtr, node.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage storage, string name) { if (storage == null) { throw new ArgumentNullException("storage"); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } NativeMethods.ml_CvBoost_write(ptr, storage.CvPtr, name); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage fs, CvFileNode node) { if (fs == null) { throw new ArgumentNullException(nameof(fs)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } NativeMethods.ml_CvANN_MLP_read(ptr, fs.CvPtr, node.CvPtr); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage storage, CvFileNode node) { if (storage == null) { throw new ArgumentNullException(nameof(storage)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } NativeMethods.ml_CvNormalBayesClassifier_read(ptr, storage.CvPtr, node.CvPtr); }
public TreeNodeIterator() { using (CvMemStorage storage = new CvMemStorage(0)) using (IplImage srcImg = new IplImage(Const.ImageLenna, LoadMode.Color)) using (IplImage srcImgGray = new IplImage(srcImg.Size, BitDepth.U8, 1)) using (IplImage tmpImg = new IplImage(srcImg.Size, BitDepth.U8, 1)) { Cv.CvtColor(srcImg, srcImgGray, ColorConversion.BgrToGray); // (1)画像の二値化と輪郭の検出 Cv.Threshold(srcImgGray, tmpImg, 120, 255, ThresholdType.Binary); CvSeq <CvPoint> contours; Cv.FindContours(tmpImg, storage, out contours, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple); /* 輪郭シーケンスから座標を取得 */ using (CvFileStorage fs = new CvFileStorage("contours.yaml", null, FileStorageMode.Write)) { // (2)ツリーノードイテレータの初期化 CvTreeNodeIterator <CvSeq <CvPoint> > it = new CvTreeNodeIterator <CvSeq <CvPoint> >(contours, 1); // (3)各ノード(輪郭)を走査 //CvSeq<CvPoint> contour; //while ((contour = it.NextTreeNode()) != null) foreach (CvSeq <CvPoint> contour in it) { fs.StartWriteStruct("contour", NodeType.Seq); // (4)輪郭を構成する頂点座標を取得 CvPoint tmp = contour[-1].Value; for (int i = 0; i < contour.Total; i++) { CvPoint point = contour[i].Value; srcImg.Line(tmp, point, CvColor.Blue, 2); fs.StartWriteStruct(null, NodeType.Map | NodeType.Flow); fs.WriteInt("x", point.X); fs.WriteInt("y", point.Y); fs.EndWriteStruct(); tmp = point; } fs.EndWriteStruct(); } } Console.WriteLine(File.ReadAllText("contours.yaml")); using (new CvWindow("Contours", srcImg)) { Cv.WaitKey(0); } } }
public TreeNodeIterator() { using (CvMemStorage storage = new CvMemStorage(0)) using (IplImage srcImg = new IplImage(Const.ImageLenna, LoadMode.Color)) using (IplImage srcImgGray = new IplImage(srcImg.Size, BitDepth.U8, 1)) using (IplImage tmpImg = new IplImage(srcImg.Size, BitDepth.U8, 1)) { Cv.CvtColor(srcImg, srcImgGray, ColorConversion.BgrToGray); // (1)画像の二値化と輪郭の検出 Cv.Threshold(srcImgGray, tmpImg, 120, 255, ThresholdType.Binary); CvSeq<CvPoint> contours; Cv.FindContours(tmpImg, storage, out contours, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple); /* 輪郭シーケンスから座標を取得 */ using (CvFileStorage fs = new CvFileStorage("contours.yaml", null, FileStorageMode.Write)) { // (2)ツリーノードイテレータの初期化 CvTreeNodeIterator<CvSeq<CvPoint>> it = new CvTreeNodeIterator<CvSeq<CvPoint>>(contours, 1); // (3)各ノード(輪郭)を走査 //CvSeq<CvPoint> contour; //while ((contour = it.NextTreeNode()) != null) foreach(CvSeq<CvPoint> contour in it) { fs.StartWriteStruct("contour", NodeType.Seq); // (4)輪郭を構成する頂点座標を取得 CvPoint tmp = contour[-1].Value; for (int i = 0; i < contour.Total; i++) { CvPoint point = contour[i].Value; srcImg.Line(tmp, point, CvColor.Blue, 2); fs.StartWriteStruct(null, NodeType.Map | NodeType.Flow); fs.WriteInt("x", point.X); fs.WriteInt("y", point.Y); fs.EndWriteStruct(); tmp = point; } fs.EndWriteStruct(); } } Console.WriteLine(File.ReadAllText("contours.yaml")); using (new CvWindow("Contours", srcImg)) { Cv.WaitKey(0); } } }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public virtual void Read(CvFileStorage storage, CvFileNode node) { if (disposed) { throw new ObjectDisposedException("CvStatModel"); } if (storage == null) { throw new ArgumentNullException("storage"); } if (node == null) { throw new ArgumentNullException("node"); } NativeMethods.ml_CvStatModel_read(ptr, storage.CvPtr, node.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public virtual void Write(CvFileStorage storage, string name) { if (disposed) { throw new ObjectDisposedException("CvStatModel"); } if (storage == null) { throw new ArgumentNullException("storage"); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } NativeMethods.ml_CvStatModel_write(ptr, storage.CvPtr, name); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="data"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="data"></param> #endif public virtual void Read(CvFileStorage fs, CvFileNode node, CvDTreeTrainData data) { if (fs == null) { throw new ArgumentNullException(nameof(fs)); } if (node == null) { throw new ArgumentNullException(nameof(node)); } if (data == null) { throw new ArgumentNullException(nameof(data)); } NativeMethods.ml_CvDTree_read(ptr, fs.CvPtr, node.CvPtr, data.CvPtr); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> private static void SampleFileStorageWriteImage(string fileName) { // cvWrite, cvWriteComment using (IplImage colorImg = new IplImage(FilePath.Image.Lenna, LoadMode.Color)) using (IplImage grayImg = new IplImage(colorImg.Size, BitDepth.U8, 1)) { colorImg.CvtColor(grayImg, ColorConversion.BgrToGray); CvRect roi = new CvRect(0, 0, colorImg.Width / 2, colorImg.Height / 2); grayImg.SetROI(roi); colorImg.SetROI(roi); grayImg.Threshold(grayImg, 90, 255, ThresholdType.Binary); using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Write)) { fs.WriteComment("This is a comment line.", false); fs.Write("color_img", colorImg); fs.StartNextStream(); fs.Write("gray_img", grayImg); } } }
/// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="forest"></param> /// <param name="data"></param> #else /// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="forest"></param> /// <param name="data"></param> #endif public virtual void Read(CvFileStorage fs, CvFileNode node, CvRTrees forest, CvDTreeTrainData data) { if (fs == null) { throw new ArgumentNullException("fs"); } if (node == null) { throw new ArgumentNullException("node"); } if (forest == null) { throw new ArgumentNullException("forest"); } if (data == null) { throw new ArgumentNullException("data"); } NativeMethods.ml_CvForestTree_read( ptr, fs.CvPtr, node.CvPtr, forest.CvPtr, data.CvPtr); }
/// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="ensemble"></param> /// <param name="data"></param> #else /// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="ensemble"></param> /// <param name="data"></param> #endif public virtual void Read(CvFileStorage fs, CvFileNode node, CvBoost ensemble, CvDTreeTrainData data) { if (fs == null) { throw new ArgumentNullException("fs"); } if (node == null) { throw new ArgumentNullException("node"); } if (ensemble == null) { throw new ArgumentNullException("ensemble"); } if (data == null) { throw new ArgumentNullException("data"); } NativeMethods.ml_CvBoostTree_read( ptr, fs.CvPtr, node.CvPtr, ensemble.CvPtr, data.CvPtr); }
/// <summary> /// 画像データのファイルストレージからの読み込み /// <param name="fileName">読み込むXML or YAMLファイル</param> private static void SampleFileStorageReadImage(string fileName) { IplImage colorImg, grayImg; // (1)ファイルを読み込む using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Read)) { CvFileNode node; node = fs.GetFileNodeByName(null, "color_img"); colorImg = fs.Read <IplImage>(node); node = fs.GetFileNodeByName(null, "gray_img"); grayImg = fs.Read <IplImage>(node); } // (2)ROI情報を取得し矩形を描いた後,解放 CvRect roiColor = colorImg.GetROI(); CvRect roiGray = grayImg.GetROI(); colorImg.ResetROI(); grayImg.ResetROI(); colorImg.Rectangle( new CvPoint(roiColor.X, roiColor.Y), new CvPoint(roiColor.X + roiColor.Width, roiColor.Y + roiColor.Height), CvColor.Red ); grayImg.Rectangle( new CvPoint(roiGray.X, roiGray.Y), new CvPoint(roiGray.X + roiGray.Width, roiGray.Y + roiGray.Height), CvColor.Black ); // (3)画像を描画 using (new CvWindow("Color Image", WindowMode.AutoSize, colorImg)) using (new CvWindow("Grayscale Image", WindowMode.AutoSize, grayImg)) { Cv.WaitKey(0); } colorImg.Dispose(); grayImg.Dispose(); }
/// <summary> /// 画像データのファイルストレージからの読み込み /// <param name="fileName">読み込むXML or YAMLファイル</param> private static void SampleFileStorageReadImage(string fileName) { IplImage colorImg, grayImg; // (1)ファイルを読み込む using (CvFileStorage fs = new CvFileStorage(fileName, null, FileStorageMode.Read)) { CvFileNode node; node = fs.GetFileNodeByName(null, "color_img"); colorImg = fs.Read<IplImage>(node); node = fs.GetFileNodeByName(null, "gray_img"); grayImg = fs.Read<IplImage>(node); } // (2)ROI情報を取得し矩形を描いた後,解放 CvRect roiColor = colorImg.GetROI(); CvRect roiGray = grayImg.GetROI(); colorImg.ResetROI(); grayImg.ResetROI(); colorImg.Rectangle( new CvPoint(roiColor.X, roiColor.Y), new CvPoint(roiColor.X + roiColor.Width, roiColor.Y + roiColor.Height), CvColor.Red ); grayImg.Rectangle( new CvPoint(roiGray.X, roiGray.Y), new CvPoint(roiGray.X + roiGray.Width, roiGray.Y + roiGray.Height), CvColor.Black ); // (3)画像を描画 using (new CvWindow("Color Image", WindowMode.AutoSize, colorImg)) using (new CvWindow("Grayscale Image", WindowMode.AutoSize, grayImg)) { Cv.WaitKey(0); } colorImg.Dispose(); grayImg.Dispose(); }
public Camera(int num, string calFilePath, int width, int height) { this.CameraNumber = num; this.WIDTH = width; this.HEIGHT = height; imageForHsv = new IplImage(WIDTH, HEIGHT, BitDepth.U8, 3); imageForTest = new IplImage(WIDTH, HEIGHT, BitDepth.U8, 3); BinLevel = 0; using (var fs = new CvFileStorage(calFilePath, null, FileStorageMode.Read)) { var param = fs.GetFileNodeByName(null, "intrinsic"); _fileIntrinsic = fs.Read <CvMat>(param); param = fs.GetFileNodeByName(null, "distortion"); _fileDistortion = fs.Read <CvMat>(param); } TmTimeOut = new System.Timers.Timer(); TmTimeOut.Elapsed += (sender, e) => { TmTimeOut.Stop(); FlagTimeout = true; }; }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage storage, String name) { if (storage == null) throw new ArgumentNullException("storage"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); MLInvoke.CvSVM_write(ptr, storage.CvPtr, name); }
/// <summary> /// ファイルストレージへの構造体の書き込みを終了する. /// </summary> /// <param name="fs">ファイルストレージ</param> #else /// <summary> /// Ends writing a structure /// </summary> /// <param name="fs">File storage. </param> #endif public static void EndWriteStruct(CvFileStorage fs) { if (fs == null) { throw new ArgumentNullException("fs"); } CvInvoke.cvEndWriteStruct(fs.CvPtr); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public virtual void Read(CvFileStorage storage, CvFileNode node) { // OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::read", "" ); throw new NotImplementedException(); }
/// <summary> /// マップのシーケンスのファイルストレージからの読み込み /// </summary> /// <param name="fileName">書きこむXML or YAMLファイル</param> private static void SampleFileStorageReadSeq(string fileName) { // cvGetHashedKey, cvGetFileNode // 二つのエントリを持つマップのシーケンスをファイルから読み込む // (1)ファイルストレージのオープン,ハッシュドキーの計算,シーケンスノードの取得 using (CvFileStorage fs = new CvFileStorage("sequence.yml", null, FileStorageMode.Read)) { CvStringHashNode xKey = fs.GetHashedKey("x", true); CvStringHashNode yKey = fs.GetHashedKey("y", true); CvFileNode points = fs.GetFileNodeByName(null, "points"); // (2)シーケンスリーダを初期化,各ノードを順次取得 if ((points.Tag & NodeType.Seq) != 0) { CvSeq seq = points.DataSeq; int total = seq.Total; CvSeqReader reader = new CvSeqReader(); seq.StartRead(reader, false); for (int i = 0; i < total; i++) { CvFileNode pt = CvFileNode.FromPtr(reader.Ptr); // (3)高速バージョン /* CvFileNode xnode = fs.GetFileNode(pt, x_key, false); CvFileNode ynode = fs.GetFileNode(pt, y_key, false); Debug.Assert( xnode != null && (xnode.Tag & NodeType.Integer) != 0 && ynode != null && (ynode.Tag & NodeType.Integer) != 0 ); int x = xnode.DataI; int i = ynode.DataI; //*/ // (4)低速バージョン.x_keyとy_keyを使わない /* CvFileNode xnode = fs.GetFileNodeByName(pt, "x"); CvFileNode ynode = fs.GetFileNodeByName(pt, "i"); Debug.Assert( xnode != null && (xnode.Tag & NodeType.Integer) != 0 && ynode != null && (ynode.Tag & NodeType.Integer) != 0 ); int x = xnode.DataI; int i = ynode.DataI; //*/ // (5)さらに低速だが,使いやすいバージョン ///* int x = fs.ReadIntByName(pt, "x", 0); int y = fs.ReadIntByName(pt, "y", 0); //*/ // (6)データを表示し,次のシーケンスノードを取得 Cv.NEXT_SEQ_ELEM(seq.ElemSize, reader); Console.WriteLine("{0}: ({1}, {2})", i, x, y); } } } Console.ReadKey(); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public virtual void Read(CvFileStorage storage, CvFileNode node) { if (storage == null) throw new ArgumentNullException("storage"); if (node == null) throw new ArgumentNullException("node"); MLInvoke.CvStatModel_read(ptr, storage.CvPtr, node.CvPtr); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage fs, CvFileNode node) { if (fs == null) throw new ArgumentNullException("fs"); if (node == null) throw new ArgumentNullException("node"); NativeMethods.ml_CvANN_MLP_read(ptr, fs.CvPtr, node.CvPtr); }
public CalibrateCamera() { const int ImageNum = 3; const int PatRow = 7; const int PatCol = 10; const int PatSize = PatRow * PatCol; const int AllPoints = ImageNum * PatSize; const float ChessSize = 24.0f; IplImage[] srcImg = new IplImage[ImageNum]; for (int i = 0; i < ImageNum; i++) { srcImg[i] = new IplImage(string.Format(FilePath.Image.Calibration, i), LoadMode.Color); } CvPoint3D32f[,,] objects = new CvPoint3D32f[ImageNum, PatRow, PatCol]; for (int i = 0; i < ImageNum; i++) { for (int j = 0; j < PatRow; j++) { for (int k = 0; k < PatCol; k++) { objects[i, j, k] = new CvPoint3D32f { X = j * ChessSize, Y = k * ChessSize, Z = 0.0f }; } } } CvMat objectPoints = new CvMat(AllPoints, 3, MatrixType.F32C1, objects); CvSize patternSize = new CvSize(PatCol, PatRow); int foundNum = 0; List <CvPoint2D32f> allCorners = new List <CvPoint2D32f>(AllPoints); int[] pointCountsValue = new int[ImageNum]; using (CvWindow window = new CvWindow("Calibration", WindowMode.AutoSize)) { for (int i = 0; i < ImageNum; i++) { CvPoint2D32f[] corners; bool found = Cv.FindChessboardCorners(srcImg[i], patternSize, out corners); Debug.Print("{0:D2}...", i); if (found) { Debug.Print("ok"); foundNum++; } else { Debug.Print("fail"); } using (IplImage srcGray = new IplImage(srcImg[i].Size, BitDepth.U8, 1)) { Cv.CvtColor(srcImg[i], srcGray, ColorConversion.BgrToGray); Cv.FindCornerSubPix(srcGray, corners, corners.Length, new CvSize(3, 3), new CvSize(-1, -1), new CvTermCriteria(20, 0.03)); Cv.DrawChessboardCorners(srcImg[i], patternSize, corners, found); pointCountsValue[i] = corners.Length; window.ShowImage(srcImg[i]); Cv.WaitKey(0); } allCorners.AddRange(corners); } if (foundNum != ImageNum) { Debug.Assert(false); } } CvMat imagePoints = new CvMat(AllPoints, 1, MatrixType.F32C2, allCorners.ToArray()); CvMat pointCounts = new CvMat(ImageNum, 1, MatrixType.S32C1, pointCountsValue); CvMat intrinsic = new CvMat(3, 3, MatrixType.F64C1); CvMat distortion = new CvMat(1, 4, MatrixType.F64C1); CvMat rotation = new CvMat(ImageNum, 3, MatrixType.F64C1); CvMat translation = new CvMat(ImageNum, 3, MatrixType.F64C1); Cv.CalibrateCamera2(objectPoints, imagePoints, pointCounts, srcImg[0].Size, intrinsic, distortion, rotation, translation, CalibrationFlag.Default); CvMat subImagePoints, subObjectPoints; Cv.GetRows(imagePoints, out subImagePoints, 0, PatSize); Cv.GetRows(objectPoints, out subObjectPoints, 0, PatSize); CvMat rotation_ = new CvMat(1, 3, MatrixType.F32C1); CvMat translation_ = new CvMat(1, 3, MatrixType.F32C1); Cv.FindExtrinsicCameraParams2(subObjectPoints, subImagePoints, intrinsic, distortion, rotation_, translation_, false); //Cv.FindExtrinsicCameraParams2_(subObjectPoints, subImagePoints, intrinsic, distortion, rotation_, translation_, false); using (var fs = new CvFileStorage("camera.xml", null, OpenCvSharp.FileStorageMode.Write)) { fs.Write("intrinsic", intrinsic); fs.Write("rotation", rotation_); fs.Write("translation", translation_); fs.Write("distortion", distortion); } foreach (IplImage img in srcImg) { img.Dispose(); } // 書き込んだファイルを表示 Console.WriteLine(File.ReadAllText("camera.xml")); Console.Read(); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="fs"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="fs"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage fs, string name) { if (fs == null) throw new ArgumentNullException("fs"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); MLInvoke.CvDTree_write(ptr, fs.CvPtr, name); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="fs"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="fs"></param> #endif public virtual void Write(CvFileStorage fs) { if (fs == null) throw new ArgumentNullException("fs"); MLInvoke.CvDTree_write(ptr, fs.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="fs"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="fs"></param> #endif public virtual void Write(CvFileStorage fs) { if (fs == null) throw new ArgumentNullException("fs"); NativeMethods.ml_CvDTree_write(ptr, fs.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public virtual void Write(CvFileStorage storage, string name) { // OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::write", "" ); throw new NotImplementedException(); }
/// <summary> /// マップのシーケンスのファイルストレージからの読み込み /// </summary> /// <param name="fileName">書きこむXML or YAMLファイル</param> private static void SampleFileStorageReadSeq(string fileName) { // cvGetHashedKey, cvGetFileNode // 二つのエントリを持つマップのシーケンスをファイルから読み込む // (1)ファイルストレージのオープン,ハッシュドキーの計算,シーケンスノードの取得 using (CvFileStorage fs = new CvFileStorage("sequence.yml", null, FileStorageMode.Read)) { CvStringHashNode xKey = fs.GetHashedKey("x", true); CvStringHashNode yKey = fs.GetHashedKey("y", true); CvFileNode points = fs.GetFileNodeByName(null, "points"); // (2)シーケンスリーダを初期化,各ノードを順次取得 if ((points.Tag & NodeType.Seq) != 0) { CvSeq seq = points.DataSeq; int total = seq.Total; CvSeqReader reader = new CvSeqReader(); seq.StartRead(reader, false); for (int i = 0; i < total; i++) { CvFileNode pt = CvFileNode.FromPtr(reader.Ptr); // (3)高速バージョン /* * CvFileNode xnode = fs.GetFileNode(pt, x_key, false); * CvFileNode ynode = fs.GetFileNode(pt, y_key, false); * Debug.Assert( * xnode != null && * (xnode.Tag & NodeType.Integer) != 0 && * ynode != null && * (ynode.Tag & NodeType.Integer) != 0 * ); * int x = xnode.DataI; * int i = ynode.DataI; * //*/ // (4)低速バージョン.x_keyとy_keyを使わない /* * CvFileNode xnode = fs.GetFileNodeByName(pt, "x"); * CvFileNode ynode = fs.GetFileNodeByName(pt, "i"); * Debug.Assert( * xnode != null && * (xnode.Tag & NodeType.Integer) != 0 && * ynode != null && * (ynode.Tag & NodeType.Integer) != 0 * ); * int x = xnode.DataI; * int i = ynode.DataI; * //*/ // (5)さらに低速だが,使いやすいバージョン ///* int x = fs.ReadIntByName(pt, "x", 0); int y = fs.ReadIntByName(pt, "y", 0); //*/ // (6)データを表示し,次のシーケンスノードを取得 Cv.NEXT_SEQ_ELEM(seq.ElemSize, reader); Console.WriteLine("{0}: ({1}, {2})", i, x, y); } } } Console.ReadKey(); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public override void Write(CvFileStorage storage, string name) { if (storage == null) throw new ArgumentNullException("storage"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); NativeMethods.ml_CvANN_MLP_write(ptr, storage.CvPtr, name); }
public CalibrateStereoCamera() { // target filenames string[] pair = new string[] { "Data/Image/Calibration/left{0:D2}.jpg", "Data/Image/Calibration/right{0:D2}.jpg" }; string[][] fileNames = new string[ImageNum][]; for (int i = 0; i < ImageNum; i++) { fileNames[i] = new string[2] { string.Format(pair[0], i + 1), string.Format(pair[1], i + 1) }; } // FindChessboardCorners CvPoint2D32f[] imagePointsLeft, imagePointsRight; int[] pointCountLeft, pointCountRight; int[] goodImagelist; FindChessboardCorners(fileNames, out imagePointsLeft, out imagePointsRight, out pointCountLeft, out pointCountRight, out goodImagelist); int nImages = goodImagelist.Length; // StereoCalibrate CvPoint3D32f[, ,] objects = new CvPoint3D32f[ImageNum, BoardSize.Height, BoardSize.Width]; for (int i = 0; i < ImageNum; i++) for (int j = 0; j < BoardSize.Height; j++) for (int k = 0; k < BoardSize.Width; k++) objects[i, j, k] = new CvPoint3D32f(j * SquareSize, k * SquareSize, 0.0f); CvMat objectPoints = new CvMat(AllPoints, 3, MatrixType.F32C1, objects); CvMat imagePoints1 = new CvMat(AllPoints, 1, MatrixType.F32C2, imagePointsLeft); CvMat imagePoints2 = new CvMat(AllPoints, 1, MatrixType.F32C2, imagePointsRight); CvMat pointCount1 = new CvMat(nImages, 1, MatrixType.S32C1, pointCountLeft); CvMat pointCount2 = new CvMat(nImages, 1, MatrixType.S32C1, pointCountRight); CvMat cameraMatrix1 = CvMat.Identity(3, 3, MatrixType.F64C1); CvMat cameraMatrix2 = CvMat.Identity(3, 3, MatrixType.F64C1); CvMat distCoeffs1 = new CvMat(1, 4, MatrixType.F64C1); CvMat distCoeffs2 = new CvMat(1, 4, MatrixType.F64C1); CvMat R = new CvMat(3, 3, MatrixType.F64C1); CvMat T = new CvMat(3, 1, MatrixType.F64C1); Cv.StereoCalibrate(objectPoints, imagePoints1, imagePoints2, pointCount1, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, new CvSize(640, 480), R, T, null, null, new CvTermCriteria(100, 1e-5), CalibrationFlag.FixAspectRatio | CalibrationFlag.ZeroTangentDist | CalibrationFlag.SameFocalLength | CalibrationFlag.RationalModel | CalibrationFlag.FixK3 | CalibrationFlag.FixK4 | CalibrationFlag.FixK5); // Rectify CvMat R1 = new CvMat(3, 3, MatrixType.F64C1); CvMat R2 = new CvMat(3, 3, MatrixType.F64C1); CvMat P1 = new CvMat(3, 4, MatrixType.F64C1); CvMat P2 = new CvMat(3, 4, MatrixType.F64C1); CvMat Q = new CvMat(4, 4, MatrixType.F64C1); Cv.StereoRectify(cameraMatrix1, cameraMatrix2, distCoeffs1, distCoeffs2, new CvSize(640, 480), R, T, R1, R2, P1, P2, Q, StereoRectificationFlag.ZeroDisparity, 1, new CvSize(640, 480)); using (CvMemStorage mem = new CvMemStorage()) using(CvFileStorage fs = new CvFileStorage("extrinsic.yml", mem, FileStorageMode.Write)) { fs.Write("R", R); fs.Write("T", T); fs.Write("R1", R1); fs.Write("R2", R2); fs.Write("P1", P1); fs.Write("P1", P1); fs.Write("Q", Q); } Process.Start("notepad", "extrinsic.yml"); Console.Read(); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public virtual void Write(CvFileStorage storage, string name) { if(storage == null) throw new ArgumentNullException("storage"); if(string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); MLInvoke.CvStatModel_write(ptr, storage.CvPtr, name); }
// Update is called once per frame void Update() { if (runCalibration) { if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2)) { if (Input.GetMouseButton(0)) { //Debug.Log(Input.mousePosition); GameObject bc = GameObject.FindGameObjectWithTag("BlueCross"); bc.transform.localPosition = new Vector3(Map(Input.mousePosition.x, Screen.width / 2.0f - 320.0f, Screen.width / 2.0f + 320.0f, 0.0f, 640.0f) - 320.0f, -Map(Input.mousePosition.y, Screen.height / 2.0f + 240.0f, Screen.height / 2.0f - 240.0f, 0.0f, 480.0f) + 240.0f, 0.0f); } else if (Input.GetMouseButton(1)) { GameObject yc = GameObject.FindGameObjectWithTag("YellowCross"); yc.transform.localPosition = new Vector3(Map(Input.mousePosition.x, Screen.width / 2.0f - 320.0f, Screen.width / 2.0f + 320.0f, 0.0f, 640.0f) - 320.0f, -Map(Input.mousePosition.y, Screen.height / 2.0f + 240.0f, Screen.height / 2.0f - 240.0f, 0.0f, 480.0f) + 240.0f, 0.0f); nextBt = true; } else if (Input.GetMouseButton(2) && nextBt == true) { if (addKinectPoint()) { addProjectorPoint(); Debug.Log("Point Added! -> (" + kinectCoordinates.Count + ") "); nextBt = false; } else { Debug.Log("Kinect Point out of bounds!"); } } } if (Input.GetKeyDown(KeyCode.A)) { //PointerEventData pointer = new PointerEventData(EventSystem.current); //pointer.position = Input.mousePosition; //List<RaycastResult> raycastResults = new List<RaycastResult>(); //EventSystem.current.RaycastAll(pointer, raycastResults); if (addKinectPoint()) { addProjectorPoint(); Debug.Log("Point Added! -> " + kinectCoordinates.Count); } else { Debug.Log("Kinect Point out of bounds!"); } } if (Input.GetKeyDown(KeyCode.S)) { if (kinectCoordinates.Count >= 8) { Debug.Log("Starting Calibration..."); findTransformation(kinectCoordinates, projectorCoordinates); foundResult = true; } else { Debug.Log("Not Enough Points!"); } } if (Input.GetKeyDown(KeyCode.D) && foundResult == true) { showResult = !showResult; if (!showResult) { screenTx.SetPixels32(resetPixels); screenTx.Apply(false); } Debug.Log("Show result toggle: " + showResult); } if (Input.GetKeyDown(KeyCode.F) && foundResult == true) { using (CvFileStorage fs = new CvFileStorage("KinectCalibration.xml", null, FileStorageMode.Write)) { string nodeName = "calibResult"; fs.Write(nodeName, result.ToCvMat()); nodeName = "kinectPoints"; Mat kinectPts = new Mat(1, kinectCoordinates.Count, MatType.CV_64FC3); for (int i = 0; i < kinectCoordinates.Count; i++) { kinectPts.Set<CvPoint3D64f>(0, i, (CvPoint3D64f)kinectCoordinates[i]); } fs.Write(nodeName, kinectPts.ToCvMat()); nodeName = "projectorPoints"; Mat projPts = new Mat(1, projectorCoordinates.Count, MatType.CV_64FC2); for (int i = 0; i < projectorCoordinates.Count; i++) { projPts.Set<CvPoint2D64f>(0, i, (CvPoint2D64f)projectorCoordinates[i]); } fs.Write(nodeName, projPts.ToCvMat()); fs.Dispose(); } Debug.Log("Calib Data saved!"); } if (Input.GetKeyDown(KeyCode.Q)) { delLastPoints(); } if (kinect.GetDepthRaw()) { try { Mat src = DoDepthBuffer(kinect.usersDepthMap, KinectWrapper.GetDepthWidth(), KinectWrapper.GetDepthHeight()); dBuffer = src.Clone(); src.ConvertTo(src, OpenCvSharp.CPlusPlus.MatType.CV_8UC1, 255.0f / NUI_IMAGE_DEPTH_MAXIMUM); Mat show = new Mat(KinectWrapper.GetDepthHeight(), KinectWrapper.GetDepthWidth(), OpenCvSharp.CPlusPlus.MatType.CV_8UC4); Mat alpha = new Mat(KinectWrapper.GetDepthHeight(), KinectWrapper.GetDepthWidth(), OpenCvSharp.CPlusPlus.MatType.CV_8UC1, new Scalar(255)); Mat[] planes = new Mat[4] { src, src, src, alpha }; Cv2.Merge(planes, show); //Mat falseColorsMap = new Mat(); //Cv2.ApplyColorMap(src, falseColorsMap, OpenCvSharp.CPlusPlus.ColorMapMode.Rainbow); //Cv2.ImShow("show", falseColorsMap); int matSize = (int)show.Total() * show.Channels(); byte[] rColors = new byte[matSize]; Marshal.Copy(show.DataStart, rColors, 0, matSize); scTex.LoadRawTextureData(rColors); scTex.Apply(false); ScreenObject.GetComponent<RawImage>().texture = scTex; if (showResult) { //ResultObject.SetActive(true); screenTx.SetPixels32(resetPixels); long discarded = 0; long drawn = 0; long bounds = 0; //Color32[] txcl = (Color32[])resetPixels.Clone(); Color32[] txcl = new Color32[screenTx.height * screenTx.width]; for (int i = 0; i < txcl.Length; i++) { Color32 cCol = new Color32(0, 0, 0, 255); txcl[i] = cCol; } screenTx.SetPixels32(txcl, 0); Color32 sccolor = Color.white; for (int i = 0; i < show.Rows; i += 5) { for (int j = 0; j < show.Cols; j += 5) { CvPoint3D64f realVal = NuiTransformDepthImageToSkeleton((long)j, (long)i, dBuffer.Get<ushort>((int)i, (int)j)); if (realVal.Z < projThresh && realVal.Z > 1.0) { CvPoint2D64f scCoord = convertKinectToProjector(realVal); if (scCoord.X > 0.0 && scCoord.X < Screen.width && scCoord.Y > 0.0 && scCoord.Y < Screen.height) { //Debug.Log(scCoord.X.ToString() + " " + scCoord.Y.ToString()); //Vec4b bgrPixel = falseColorsMap.At<Vec4b>(i, j); //Color32 sccolor = new Color32(bgrPixel[2], bgrPixel[1], bgrPixel[0], 255); int X = Mathf.CeilToInt((float)scCoord.X); int Y = Mathf.CeilToInt((float)scCoord.Y); int arrPos = ((screenTx.height - Y) * screenTx.width) + X; //Debug.Log(scCoord.X + " -> " + X + " --" + scCoord.Y + " -> " + Y + " = " + arrPos + " == " + screenTx.height + " == " + screenTx.width); txcl[arrPos] = sccolor; //screenTx.SetPixel((int)scCoord.X, Screen.height - (int)scCoord.Y, sccolor); drawn++; } else { bounds++; } } else { discarded++; } } } Debug.Log("Discarded: " + discarded + " Bounds: " + bounds + " Drawn: " + drawn); screenTx.SetPixels32(txcl, 0); screenTx.Apply(false); //GameObject.FindGameObjectWithTag("Restex").GetComponent<RawImage>().texture = screenTx; //CvContour contourfinder = new CvContour(); } else { //ResultObject.SetActive(false); } } catch (System.Exception e) { throw e; } } } }
public CalibrateCamera() { const int ImageNum = 3; const int PatRow = 7; const int PatCol = 10; const int PatSize = PatRow * PatCol; const int AllPoints = ImageNum * PatSize; const float ChessSize = 24.0f; IplImage[] srcImg = new IplImage[ImageNum]; for (int i = 0; i < ImageNum; i++) { srcImg[i] = new IplImage(string.Format(FilePath.Image.Calibration, i), LoadMode.Color); } CvPoint3D32f[,,] objects = new CvPoint3D32f[ImageNum, PatRow, PatCol]; for (int i = 0; i < ImageNum; i++) { for (int j = 0; j < PatRow; j++) { for (int k = 0; k < PatCol; k++) { objects[i, j, k] = new CvPoint3D32f { X = j * ChessSize, Y = k * ChessSize, Z = 0.0f }; } } } CvMat objectPoints = new CvMat(AllPoints, 3, MatrixType.F32C1, objects); CvSize patternSize = new CvSize(PatCol, PatRow); int foundNum = 0; List<CvPoint2D32f> allCorners = new List<CvPoint2D32f>(AllPoints); int[] pointCountsValue = new int[ImageNum]; using (CvWindow window = new CvWindow("Calibration", WindowMode.AutoSize)) { for (int i = 0; i < ImageNum; i++) { CvPoint2D32f[] corners; bool found = Cv.FindChessboardCorners(srcImg[i], patternSize, out corners); Debug.Print("{0:D2}...", i); if (found) { Debug.Print("ok"); foundNum++; } else { Debug.Print("fail"); } using (IplImage srcGray = new IplImage(srcImg[i].Size, BitDepth.U8, 1)) { Cv.CvtColor(srcImg[i], srcGray, ColorConversion.BgrToGray); Cv.FindCornerSubPix(srcGray, corners, corners.Length, new CvSize(3, 3), new CvSize(-1, -1), new CvTermCriteria(20, 0.03)); Cv.DrawChessboardCorners(srcImg[i], patternSize, corners, found); pointCountsValue[i] = corners.Length; window.ShowImage(srcImg[i]); Cv.WaitKey(0); } allCorners.AddRange(corners); } if (foundNum != ImageNum) { Debug.Assert(false); } } CvMat imagePoints = new CvMat(AllPoints, 1, MatrixType.F32C2, allCorners.ToArray()); CvMat pointCounts = new CvMat(ImageNum, 1, MatrixType.S32C1, pointCountsValue); CvMat intrinsic = new CvMat(3, 3, MatrixType.F64C1); CvMat distortion = new CvMat(1, 4, MatrixType.F64C1); CvMat rotation = new CvMat(ImageNum, 3, MatrixType.F64C1); CvMat translation = new CvMat(ImageNum, 3, MatrixType.F64C1); Cv.CalibrateCamera2(objectPoints, imagePoints, pointCounts, srcImg[0].Size, intrinsic, distortion, rotation, translation, CalibrationFlag.Default); CvMat subImagePoints, subObjectPoints; Cv.GetRows(imagePoints, out subImagePoints, 0, PatSize); Cv.GetRows(objectPoints, out subObjectPoints, 0, PatSize); CvMat rotation_ = new CvMat(1, 3, MatrixType.F32C1); CvMat translation_ = new CvMat(1, 3, MatrixType.F32C1); Cv.FindExtrinsicCameraParams2(subObjectPoints, subImagePoints, intrinsic, distortion, rotation_, translation_, false); //Cv.FindExtrinsicCameraParams2_(subObjectPoints, subImagePoints, intrinsic, distortion, rotation_, translation_, false); using (var fs = new CvFileStorage("camera.xml", null, OpenCvSharp.FileStorageMode.Write)) { fs.Write("intrinsic", intrinsic); fs.Write("rotation", rotation_); fs.Write("translation", translation_); fs.Write("distortion", distortion); } foreach (IplImage img in srcImg) { img.Dispose(); } // 書き込んだファイルを表示 Console.WriteLine(File.ReadAllText("camera.xml")); Console.Read(); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="fs"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage fs, CvFileNode node) { if (fs == null) throw new ArgumentNullException("fs"); if (node == null) throw new ArgumentNullException("node"); MLInvoke.CvRTrees_read(ptr, fs.CvPtr, node.CvPtr); }
/// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="forest"></param> /// <param name="data"></param> #else /// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="forest"></param> /// <param name="data"></param> #endif public virtual void Read(CvFileStorage fs, CvFileNode node, CvRTrees forest, CvDTreeTrainData data) { if (fs == null) throw new ArgumentNullException("fs"); if (node == null) throw new ArgumentNullException("node"); if (forest == null) throw new ArgumentNullException("forest"); if (data == null) throw new ArgumentNullException("data"); MLInvoke.CvForestTree_read(ptr, fs.CvPtr, node.CvPtr, forest.CvPtr, data.CvPtr); }
public CalibrateStereoCamera() { // target filenames string[] pair = { "Data/Image/Calibration/left{0:D2}.jpg", "Data/Image/Calibration/right{0:D2}.jpg" }; string[][] fileNames = new string[ImageNum][]; for (int i = 0; i < ImageNum; i++) { fileNames[i] = new string[2] { string.Format(pair[0], i + 1), string.Format(pair[1], i + 1) }; } // FindChessboardCorners CvPoint2D32f[] imagePointsLeft, imagePointsRight; int[] pointCountLeft, pointCountRight; int[] goodImagelist; FindChessboardCorners(fileNames, out imagePointsLeft, out imagePointsRight, out pointCountLeft, out pointCountRight, out goodImagelist); int nImages = goodImagelist.Length; // StereoCalibrate CvPoint3D32f[, ,] objects = new CvPoint3D32f[ImageNum, BoardSize.Height, BoardSize.Width]; for (int i = 0; i < ImageNum; i++) { for (int j = 0; j < BoardSize.Height; j++) { for (int k = 0; k < BoardSize.Width; k++) { objects[i, j, k] = new CvPoint3D32f(j * SquareSize, k * SquareSize, 0.0f); } } } CvMat objectPoints = new CvMat(AllPoints, 3, MatrixType.F32C1, objects); CvMat imagePoints1 = new CvMat(AllPoints, 1, MatrixType.F32C2, imagePointsLeft); CvMat imagePoints2 = new CvMat(AllPoints, 1, MatrixType.F32C2, imagePointsRight); CvMat pointCount1 = new CvMat(nImages, 1, MatrixType.S32C1, pointCountLeft); CvMat pointCount2 = new CvMat(nImages, 1, MatrixType.S32C1, pointCountRight); CvMat cameraMatrix1 = CvMat.Identity(3, 3, MatrixType.F64C1); CvMat cameraMatrix2 = CvMat.Identity(3, 3, MatrixType.F64C1); CvMat distCoeffs1 = new CvMat(1, 4, MatrixType.F64C1); CvMat distCoeffs2 = new CvMat(1, 4, MatrixType.F64C1); CvMat R = new CvMat(3, 3, MatrixType.F64C1); CvMat T = new CvMat(3, 1, MatrixType.F64C1); Cv.StereoCalibrate(objectPoints, imagePoints1, imagePoints2, pointCount1, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, new CvSize(640, 480), R, T, null, null, new CvTermCriteria(100, 1e-5), CalibrationFlag.FixAspectRatio | CalibrationFlag.ZeroTangentDist | CalibrationFlag.SameFocalLength | CalibrationFlag.RationalModel | CalibrationFlag.FixK3 | CalibrationFlag.FixK4 | CalibrationFlag.FixK5); // Rectify CvMat R1 = new CvMat(3, 3, MatrixType.F64C1); CvMat R2 = new CvMat(3, 3, MatrixType.F64C1); CvMat P1 = new CvMat(3, 4, MatrixType.F64C1); CvMat P2 = new CvMat(3, 4, MatrixType.F64C1); CvMat Q = new CvMat(4, 4, MatrixType.F64C1); Cv.StereoRectify(cameraMatrix1, cameraMatrix2, distCoeffs1, distCoeffs2, new CvSize(640, 480), R, T, R1, R2, P1, P2, Q, StereoRectificationFlag.ZeroDisparity, 1, new CvSize(640, 480)); using (CvMemStorage mem = new CvMemStorage()) using (CvFileStorage fs = new CvFileStorage("extrinsic.yml", mem, OpenCvSharp.FileStorageMode.Write)) { fs.Write("R", R); fs.Write("T", T); fs.Write("R1", R1); fs.Write("R2", R2); fs.Write("P1", P1); fs.Write("P1", P1); fs.Write("Q", Q); } Process.Start("notepad", "extrinsic.yml"); Console.Read(); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage storage, CvFileNode node) { if (storage == null) throw new ArgumentNullException("storage"); if (node == null) throw new ArgumentNullException("node"); NativeMethods.ml_CvBoost_read(ptr, storage.CvPtr, node.CvPtr); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public override void Read(CvFileStorage storage, CvFileNode node) { if (storage == null) throw new ArgumentNullException("storage"); if (node == null) throw new ArgumentNullException("node"); MLInvoke.CvSVM_read(ptr, storage.CvPtr, node.CvPtr); }
/// <summary> /// モデルをファイルに書き込む /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #else /// <summary> /// Writes the model to file storage /// </summary> /// <param name="storage"></param> /// <param name="name"></param> #endif public virtual void Write(CvFileStorage storage, string name) { if (disposed) throw new ObjectDisposedException("CvStatModel"); if (storage == null) throw new ArgumentNullException("storage"); if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name"); NativeMethods.ml_CvStatModel_write(ptr, storage.CvPtr, name); }
/// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="ensemble"></param> /// <param name="data"></param> #else /// <summary> /// /// </summary> /// <param name="fs"></param> /// <param name="node"></param> /// <param name="ensemble"></param> /// <param name="data"></param> #endif public virtual void Read(CvFileStorage fs, CvFileNode node, CvBoost ensemble, CvDTreeTrainData data) { if (fs == null) throw new ArgumentNullException("fs"); if (node == null) throw new ArgumentNullException("node"); if (ensemble == null) throw new ArgumentNullException("ensemble"); if (data == null) throw new ArgumentNullException("data"); NativeMethods.ml_CvBoostTree_read( ptr, fs.CvPtr, node.CvPtr, ensemble.CvPtr, data.CvPtr); }
/// <summary> /// ファイルストレージからモデルを読み込む /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #else /// <summary> /// Reads the model from file storage /// </summary> /// <param name="storage"></param> /// <param name="node"></param> #endif public virtual void Read(CvFileStorage storage, CvFileNode node) { if (disposed) throw new ObjectDisposedException("CvStatModel"); if (storage == null) throw new ArgumentNullException("storage"); if (node == null) throw new ArgumentNullException("node"); NativeMethods.ml_CvStatModel_read(ptr, storage.CvPtr, node.CvPtr); }