internal static extern ContourScanner cvStartFindContours( Arr image, MemStorage storage, int header_size, ContourRetrieval mode, ContourApproximation method, Point offset);
internal static extern int cvFindContours( Arr image, MemStorage storage, out Seq first_contour, int header_size, ContourRetrieval mode, ContourApproximation method, Point offset);
static public CvPoint[] DistillContours( CvMat inputMat_grey, int maxContourPoints, CvPoint offset, ContourRetrieval retr = ContourRetrieval.External, ContourChain chain = ContourChain.ApproxSimple) { // maxContourPoints (original name "maxContours"); 5 or 100??!!! // TODO : The CV docs specifically state that the image should be in binary format. Check if it is. //std::vector<std::vector<cv::Point>> updateContours; //std::vector<cv::Vec4i> m_hierarchy; // TODO : Not used currently, but determine if it's gonna be needed for points-tracking // see usage here: http://stackoverflow.com/questions/35418714/opencvsharps-findcontour-returns-wrong-data ContourData contoursData = Filters.FindContours(inputMat_grey, retr, chain, offset); CvPoint[][] contoursFound = contoursData.contours; // original name: "updateContours" if (contoursFound.Length == 0) { return(null); // TODO : cannot process frame, no contours found. Maybe it's time to rethink about that strategy and not simply } // return empty handed!!! double[] contourAreas = contoursData.areas; Point[] newPtV; // find index of max-area contour int index = 0; double maxArea = 0; for (int i = contourAreas.Length - 1; i >= 0; --i) { double area = contourAreas[i]; if (area > maxArea) { index = i; maxArea = area; } } // approximate contour down to 4 points // TODO : This idea sounds weird. Why not check all contours and find one that is best approximated by 4 points??? CvPoint[] biggestContour = contoursFound[index]; newPtV = new Point[biggestContour.Length]; PointOps.CopyCvPointsToPoints(biggestContour, newPtV); //PointOps.CopyCvPointsToGenericPointsArray( biggestContour, newPtV ); double epsilon = 1; while (newPtV.Length > maxContourPoints) { newPtV = Cv2.ApproxPolyDP(newPtV, epsilon++, true); // TODO : Is incrementing epsilon by 1 a bit stupid? Maybe increment exponentially? } // finally CvPoint[] cvPoints = new CvPoint[newPtV.Length]; PointOps.CopyPointsToCvPoints(newPtV, cvPoints); return(cvPoints); }
/// <summary> /// 輪郭走査処理の初期化を行う /// </summary> /// <param name="image">入力画像.8ビットシングルチャンネルの2値化画像. </param> /// <param name="storage">抽出された輪郭データの保存領域. </param> /// <param name="headerSize">シーケンスヘッダのサイズ. method=CV_CHAIN_CODEの時, >=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour). </param> /// <param name="mode">抽出モード.</param> /// <param name="method">近似手法.cvFindContoursと同様,但し CV_LINK_RUNS は使用不可. </param> /// <param name="offset">ROIのオフセット.cvFindContoursを参照. </param> /// <returns>輪郭スキャナのポインタ</returns> #else /// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> /// <param name="method">Approximation method. It has the same meaning as in cvFindContours, but CV_LINK_RUNS can not be used here. </param> /// <param name="offset">ROI offset; see cvFindContours. </param> /// <returns>CvContourScanner</returns> #endif public CvContourScanner(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode, ContourChain method, CvPoint offset) { if (image == null) { throw new ArgumentNullException(nameof(image)); } if (storage == null) { throw new ArgumentNullException(nameof(storage)); } Initialize(image, storage, headerSize, mode, method, offset); }
/// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> /// <param name="method">Approximation method. It has the same meaning as in cvFindContours, but CV_LINK_RUNS can not be used here. </param> /// <param name="offset">ROI offset; see cvFindContours. </param> /// <returns>CvContourScanner</returns> private void Initialize(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode, ContourChain method, CvPoint offset) { this.image = image; this.storage = storage; this.headerSize = headerSize; this.mode = mode; this.method = method; this.offset = offset; if (ptr != IntPtr.Zero) { NativeMethods.cvEndFindContours(ref ptr); } ptr = NativeMethods.cvStartFindContours(image.CvPtr, storage.CvPtr, headerSize, mode, method, offset); }
// The OpenCVSharp is for some reason complicated and needs to be abstracted. So here is the abstraction layer... // TODO : The CV docs specifically state that the image should be in binary format. Check if it is. // see usage here: http://stackoverflow.com/questions/35418714/opencvsharps-findcontour-returns-wrong-data static public ContourData FindContours(CvMat input, ContourRetrieval retrievalMode, ContourChain chainMode, CvPoint offset, double minArea = 0) { List <CvPoint[]> pointsArrays = new List <CvPoint[]>(); List <double> areas = new List <double>(); CvSeq <CvPoint> contoursRaw; using (CvMemStorage storage = new CvMemStorage()) { Cv.FindContours(input, storage, out contoursRaw, CvContour.SizeOf, retrievalMode, chainMode, offset); using (CvContourScanner scanner = new CvContourScanner(input, storage, CvContour.SizeOf, retrievalMode, chainMode, offset)) { foreach (CvSeq <CvPoint> c in scanner) { List <CvPoint> points = new List <CvPoint>(); //Some contours have negative area! double area = c.ContourArea(); if (Math.Abs(area) >= minArea) { areas.Add(area); foreach (CvPoint p in c.ToArray()) { points.Add(p); } pointsArrays.Add(points.ToArray()); } } } } ContourData data = new ContourData(); data.contours = pointsArrays.ToArray(); data.areas = areas.ToArray(); return(data); }
/// <summary> /// 2値画像中の輪郭を検出します. /// 入力画像は,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像の中身を書き換えます. /// </summary> /// <param name="contours">検出された輪郭.各輪郭は,点のベクトルとして格納されます.</param> /// <param name="hierarchy">画像のトポロジーに関する情報を含む出力ベクトル.これは,輪郭数と同じ数の要素を持ちます.各輪郭 contours[i] に対して, /// 要素 hierarchy[i]のメンバにはそれぞれ,同じ階層レベルに存在する前後の輪郭,最初の子輪郭,および親輪郭の /// contours インデックス(0 基準)がセットされます.また,輪郭 i において,前後,親,子の輪郭が存在しない場合, /// それに対応する hierarchy[i] の要素は,負の値になります.</param> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> #else /// <summary> /// Finds contours in a binary image. /// The source is an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. The function modifies this image while extracting the contours. /// </summary> /// <param name="contours">Detected contours. Each contour is stored as a vector of points.</param> /// <param name="hierarchy">Optional output vector, containing information about the image topology. /// It has as many elements as the number of contours. For each i-th contour contours[i], /// the members of the elements hierarchy[i] are set to 0-based indices in contours of the next /// and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. /// If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.</param> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> #endif public void FindContours(out Point[][] contours, out HiearchyIndex[] hierarchy, ContourRetrieval mode, ContourChain method, Point? offset = null) { Cv2.FindContours(this, out contours, out hierarchy, mode, method, offset); }
/// <summary> /// 2値画像中の輪郭を検出します. /// </summary> /// <param name="image">入力画像,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像 image の中身を書き換えます.</param> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> /// <return>検出された輪郭.各輪郭は,点のベクトルとして格納されます.</return> #else /// <summary> /// Finds contours in a binary image. /// </summary> /// <param name="image">Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. /// The function modifies the image while extracting the contours.</param> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> /// <returns>Detected contours. Each contour is stored as a vector of points.</returns> #endif public static MatOfPoint[] FindContoursAsMat(InputOutputArray image, ContourRetrieval mode, ContourChain method, Point? offset = null) { if (image == null) throw new ArgumentNullException("image"); image.ThrowIfNotReady(); CvPoint offset0 = offset.GetValueOrDefault(new Point()); IntPtr contoursPtr; NativeMethods.imgproc_findContours2_OutputArray(image.CvPtr, out contoursPtr, (int)mode, (int)method, offset0); image.Fix(); using (var contoursVec = new VectorOfMat(contoursPtr)) { return contoursVec.ToArray<MatOfPoint>(); } }
/// <summary> /// 2値画像中の輪郭を検出します. /// </summary> /// <param name="image">入力画像,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像 image の中身を書き換えます.</param> /// <param name="contours">検出された輪郭.各輪郭は,点のベクトルとして格納されます.</param> /// <param name="hierarchy">画像のトポロジーに関する情報を含む出力ベクトル.これは,輪郭数と同じ数の要素を持ちます.各輪郭 contours[i] に対して, /// 要素 hierarchy[i]のメンバにはそれぞれ,同じ階層レベルに存在する前後の輪郭,最初の子輪郭,および親輪郭の /// contours インデックス(0 基準)がセットされます.また,輪郭 i において,前後,親,子の輪郭が存在しない場合, /// それに対応する hierarchy[i] の要素は,負の値になります.</param> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> #else /// <summary> /// Finds contours in a binary image. /// </summary> /// <param name="image">Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. /// The function modifies the image while extracting the contours.</param> /// <param name="contours">Detected contours. Each contour is stored as a vector of points.</param> /// <param name="hierarchy">Optional output vector, containing information about the image topology. /// It has as many elements as the number of contours. For each i-th contour contours[i], /// the members of the elements hierarchy[i] are set to 0-based indices in contours of the next /// and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. /// If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.</param> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> #endif public static void FindContours(InputOutputArray image, out Mat[] contours, OutputArray hierarchy, ContourRetrieval mode, ContourChain method, Point? offset = null) { if (image == null) throw new ArgumentNullException("image"); if (hierarchy == null) throw new ArgumentNullException("hierarchy"); image.ThrowIfNotReady(); hierarchy.ThrowIfNotReady(); CvPoint offset0 = offset.GetValueOrDefault(new Point()); IntPtr contoursPtr; NativeMethods.imgproc_findContours1_OutputArray(image.CvPtr, out contoursPtr, hierarchy.CvPtr, (int)mode, (int)method, offset0); using (var contoursVec = new VectorOfMat(contoursPtr)) { contours = contoursVec.ToArray(); } image.Fix(); hierarchy.Fix(); }
/// <summary> /// 2値画像中の輪郭を検出します. /// </summary> /// <param name="image">入力画像,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像 image の中身を書き換えます.</param> /// <param name="contours">検出された輪郭.各輪郭は,点のベクトルとして格納されます.</param> /// <param name="hierarchy">画像のトポロジーに関する情報を含む出力ベクトル.これは,輪郭数と同じ数の要素を持ちます.各輪郭 contours[i] に対して, /// 要素 hierarchy[i]のメンバにはそれぞれ,同じ階層レベルに存在する前後の輪郭,最初の子輪郭,および親輪郭の /// contours インデックス(0 基準)がセットされます.また,輪郭 i において,前後,親,子の輪郭が存在しない場合, /// それに対応する hierarchy[i] の要素は,負の値になります.</param> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> #else /// <summary> /// Finds contours in a binary image. /// </summary> /// <param name="image">Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. /// The function modifies the image while extracting the contours.</param> /// <param name="contours">Detected contours. Each contour is stored as a vector of points.</param> /// <param name="hierarchy">Optional output vector, containing information about the image topology. /// It has as many elements as the number of contours. For each i-th contour contours[i], /// the members of the elements hierarchy[i] are set to 0-based indices in contours of the next /// and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. /// If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.</param> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> #endif public static void FindContours(InputOutputArray image, out Point[][] contours, out HierarchyIndex[] hierarchy, ContourRetrieval mode, ContourChain method, Point? offset = null) { if (image == null) throw new ArgumentNullException("image"); image.ThrowIfNotReady(); CvPoint offset0 = offset.GetValueOrDefault(new Point()); IntPtr contoursPtr, hierarchyPtr; NativeMethods.imgproc_findContours1_vector(image.CvPtr, out contoursPtr, out hierarchyPtr, (int)mode, (int)method, offset0); using (var contoursVec = new VectorOfVectorPoint(contoursPtr)) using (var hierarchyVec = new VectorOfVec4i(hierarchyPtr)) { contours = contoursVec.ToArray(); Vec4i[] hierarchyOrg = hierarchyVec.ToArray(); hierarchy = EnumerableEx.SelectToArray(hierarchyOrg, HierarchyIndex.FromVec4i); } image.Fix(); }
/// <summary> /// 輪郭走査処理の初期化を行う /// </summary> /// <param name="image">入力画像.8ビットシングルチャンネルの2値化画像. </param> /// <param name="storage">抽出された輪郭データの保存領域. </param> /// <param name="headerSize">シーケンスヘッダのサイズ. method=CV_CHAIN_CODEの時, >=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour). </param> /// <param name="mode">抽出モード.</param> #else /// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> #endif public CvContourScanner(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode) : this(image, storage, headerSize, mode, ContourChain.ApproxSimple, new CvPoint(0, 0)) { }
/// <summary> /// 2値画像中の輪郭を検出します. /// 入力画像は,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像の中身を書き換えます. /// </summary> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> /// <return>検出された輪郭.各輪郭は,点のベクトルとして格納されます.</return> #else /// <summary> /// Finds contours in a binary image. /// The source is an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. The function modifies this image while extracting the contours. /// </summary> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> /// <returns>Detected contours. Each contour is stored as a vector of points.</returns> #endif public MatOfPoint[] FindContoursAsMat(ContourRetrieval mode, ContourChain method, Point? offset = null) { return Cv2.FindContoursAsMat(this, mode, method, offset); }
/// <summary> /// 輪郭走査処理の初期化を行う /// </summary> /// <param name="image">入力画像.8ビットシングルチャンネルの2値化画像. </param> /// <param name="storage">抽出された輪郭データの保存領域. </param> /// <param name="headerSize">シーケンスヘッダのサイズ. method=CV_CHAIN_CODEの時, >=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour). </param> /// <param name="mode">抽出モード.</param> /// <param name="method">近似手法.cvFindContoursと同様,但し CV_LINK_RUNS は使用不可. </param> #else /// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> /// <param name="method">Approximation method. It has the same meaning as in cvFindContours, but CV_LINK_RUNS can not be used here. </param> #endif public CvContourScanner(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode, ContourChain method) : this(image, storage, headerSize, mode, method, new CvPoint(0, 0)) { }
/// <summary> /// 2値画像中の輪郭を見つける /// </summary> /// <param name="image">入力画像(8ビットシングルチャンネル).値が0以外のピクセルは「1」,0のピクセルは「0」とする.</param> /// <param name="storage">抽出された輪郭を保存する領域</param> /// <param name="firstContour">出力パラメータ.一番外側の輪郭へのポインタが入っている.</param> /// <param name="headerSize">シーケンスヘッダのサイズ.method=CV_CHAIN_CODEの場合,>=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour).</param> /// <param name="mode">抽出モード </param> /// <param name="method">近似手法</param> /// <param name="offset">オフセット.全ての輪郭点はこれによってシフトされる.</param> /// <returns>抽出した輪郭の個数</returns> #else /// <summary> /// Retrieves contours from the binary image and returns the number of retrieved contours. /// </summary> /// <param name="image">The source 8-bit single channel image. Non-zero pixels are treated as 1’s, zero pixels remain 0’s - that is image treated as binary. /// To get such a binary image from grayscale, one may use cvThreshold, cvAdaptiveThreshold or cvCanny. The function modifies the source image content. </param> /// <param name="storage">Container of the retrieved contours. </param> /// <param name="firstContour">Output parameter, will contain the pointer to the first outer contour. </param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode. </param> /// <param name="method">Approximation method. </param> /// <param name="offset">Offset, by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context. </param> /// <returns>The number of retrieved contours.</returns> #endif public static int FindContours(CvArr image, CvMemStorage storage, out CvSeq<CvPoint> firstContour, int headerSize, ContourRetrieval mode, ContourChain method, CvPoint offset) { if (image == null) throw new ArgumentNullException("image"); if (storage == null) throw new ArgumentNullException("storage"); IntPtr firstContourPtr = IntPtr.Zero; int result = NativeMethods.cvFindContours(image.CvPtr, storage.CvPtr, ref firstContourPtr, headerSize, mode, method, offset); if (firstContourPtr == IntPtr.Zero) firstContour = null; else if (method == ContourChain.Code) firstContour = new CvChain(firstContourPtr); else firstContour = new CvContour(firstContourPtr); return result; }
/// <summary> /// 2値画像中の輪郭を見つける /// </summary> /// <param name="image">入力画像(8ビットシングルチャンネル).値が0以外のピクセルは「1」,0のピクセルは「0」とする.</param> /// <param name="storage">抽出された輪郭を保存する領域</param> /// <param name="firstContour">出力パラメータ.一番外側の輪郭へのポインタが入っている.</param> /// <param name="headerSize">シーケンスヘッダのサイズ.method=CV_CHAIN_CODEの場合,>=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour).</param> /// <param name="mode">抽出モード </param> /// <param name="method">近似手法</param> /// <returns>抽出した輪郭の個数</returns> #else /// <summary> /// Retrieves contours from the binary image and returns the number of retrieved contours. /// </summary> /// <param name="image">The source 8-bit single channel image. Non-zero pixels are treated as 1’s, zero pixels remain 0’s - that is image treated as binary. /// To get such a binary image from grayscale, one may use cvThreshold, cvAdaptiveThreshold or cvCanny. The function modifies the source image content. </param> /// <param name="storage">Container of the retrieved contours. </param> /// <param name="firstContour">Output parameter, will contain the pointer to the first outer contour. </param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode. </param> /// <param name="method">Approximation method. </param> /// <returns>The number of retrieved contours.</returns> #endif public static int FindContours(CvArr image, CvMemStorage storage, out CvSeq<CvPoint> firstContour, int headerSize, ContourRetrieval mode, ContourChain method) { return FindContours(image, storage, out firstContour, headerSize, mode, method, new CvPoint(0, 0)); }
/// <summary> /// 2値画像中の輪郭を検出します. /// 入力画像は,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像の中身を書き換えます. /// </summary> /// <param name="contours">検出された輪郭.各輪郭は,点のベクトルとして格納されます.</param> /// <param name="hierarchy">画像のトポロジーに関する情報を含む出力ベクトル.これは,輪郭数と同じ数の要素を持ちます.各輪郭 contours[i] に対して, /// 要素 hierarchy[i]のメンバにはそれぞれ,同じ階層レベルに存在する前後の輪郭,最初の子輪郭,および親輪郭の /// contours インデックス(0 基準)がセットされます.また,輪郭 i において,前後,親,子の輪郭が存在しない場合, /// それに対応する hierarchy[i] の要素は,負の値になります.</param> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> #else /// <summary> /// Finds contours in a binary image. /// The source is an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. The function modifies this image while extracting the contours. /// </summary> /// <param name="contours">Detected contours. Each contour is stored as a vector of points.</param> /// <param name="hierarchy">Optional output vector, containing information about the image topology. /// It has as many elements as the number of contours. For each i-th contour contours[i], /// the members of the elements hierarchy[i] are set to 0-based indices in contours of the next /// and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively. /// If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.</param> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> #endif public void FindContours(out Mat[] contours, OutputArray hierarchy, ContourRetrieval mode, ContourChain method, Point? offset = null) { Cv2.FindContours(this, out contours, hierarchy, mode, method, offset); }
/// <summary> /// 2値画像中の輪郭を検出します. /// 入力画像は,8ビット,シングルチャンネル.0以外のピクセルは 1として,0のピクセルは0のまま扱われます. /// また,この関数は,輪郭抽出処理中に入力画像の中身を書き換えます. /// </summary> /// <param name="mode">輪郭抽出モード</param> /// <param name="method">輪郭の近似手法</param> /// <param name="offset">オプションのオフセット.各輪郭点はこの値の分だけシフトします.これは,ROIの中で抽出された輪郭を,画像全体に対して位置づけて解析する場合に役立ちます.</param> /// <return>検出された輪郭.各輪郭は,点のベクトルとして格納されます.</return> #else /// <summary> /// Finds contours in a binary image. /// The source is an 8-bit single-channel image. Non-zero pixels are treated as 1’s. /// Zero pixels remain 0’s, so the image is treated as binary. The function modifies this image while extracting the contours. /// </summary> /// <param name="mode">Contour retrieval mode</param> /// <param name="method">Contour approximation method</param> /// <param name="offset"> Optional offset by which every contour point is shifted. /// This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.</param> /// <returns>Detected contours. Each contour is stored as a vector of points.</returns> #endif public Point[][] FindContoursAsArray(ContourRetrieval mode, ContourChain method, Point? offset = null) { return Cv2.FindContoursAsArray(this, mode, method, offset); }
/// <summary> /// 輪郭走査処理の初期化を行う /// </summary> /// <param name="image">入力画像.8ビットシングルチャンネルの2値化画像. </param> /// <param name="storage">抽出された輪郭データの保存領域. </param> /// <param name="headerSize">シーケンスヘッダのサイズ. method=CV_CHAIN_CODEの時, >=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour). </param> /// <param name="mode">抽出モード.</param> /// <returns>輪郭スキャナのポインタ</returns> #else /// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> /// <returns>CvContourScanner</returns> #endif public static CvContourScanner StartFindContours(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode) { return(new CvContourScanner(image, storage, headerSize, mode, ContourChain.ApproxSimple, new CvPoint(0, 0))); }
/// <summary> /// 輪郭走査処理の初期化を行う /// </summary> /// <param name="image">入力画像.8ビットシングルチャンネルの2値化画像. </param> /// <param name="storage">抽出された輪郭データの保存領域. </param> /// <param name="headerSize">シーケンスヘッダのサイズ. method=CV_CHAIN_CODEの時, >=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour). </param> /// <param name="mode">抽出モード.</param> /// <param name="method">近似手法.cvFindContoursと同様,但し CV_LINK_RUNS は使用不可. </param> /// <param name="offset">ROIのオフセット.cvFindContoursを参照. </param> /// <returns>輪郭スキャナのポインタ</returns> #else /// <summary> /// Initializes contour scanning process /// </summary> /// <param name="image">The source 8-bit single channel binary image. </param> /// <param name="storage">Container of the retrieved contours.</param> /// <param name="headerSize">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode; see cvFindContours. </param> /// <param name="method">Approximation method. It has the same meaning as in cvFindContours, but CV_LINK_RUNS can not be used here. </param> /// <param name="offset">ROI offset; see cvFindContours. </param> /// <returns>CvContourScanner</returns> #endif public static CvContourScanner StartFindContours(CvArr image, CvMemStorage storage, int headerSize, ContourRetrieval mode, ContourChain method, CvPoint offset) { return(new CvContourScanner(image, storage, headerSize, mode, method, offset)); }
/// <summary> /// 2値画像中の輪郭を見つける /// </summary> /// <param name="image">入力画像(8ビットシングルチャンネル).値が0以外のピクセルは「1」,0のピクセルは「0」とする.</param> /// <param name="storage">抽出された輪郭を保存する領域</param> /// <param name="first_contour">出力パラメータ.一番外側の輪郭へのポインタが入っている.</param> /// <param name="header_size">シーケンスヘッダのサイズ.method=CV_CHAIN_CODEの場合,>=sizeof(CvChain) ,それ以外の場合 >=sizeof(CvContour).</param> /// <param name="mode">抽出モード </param> /// <returns>抽出した輪郭の個数</returns> #else /// <summary> /// Retrieves contours from the binary image and returns the number of retrieved contours. /// </summary> /// <param name="image">The source 8-bit single channel image. Non-zero pixels are treated as 1’s, zero pixels remain 0’s - that is image treated as binary. /// To get such a binary image from grayscale, one may use cvThreshold, cvAdaptiveThreshold or cvCanny. The function modifies the source image content. </param> /// <param name="storage">Container of the retrieved contours. </param> /// <param name="first_contour">Output parameter, will contain the pointer to the first outer contour. </param> /// <param name="header_size">Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise. </param> /// <param name="mode">Retrieval mode. </param> /// <returns>The number of retrieved contours.</returns> #endif public static int FindContours(CvArr image, CvMemStorage storage, out CvSeq<CvPoint> first_contour, int header_size, ContourRetrieval mode) { return FindContours(image, storage, out first_contour, header_size, mode, ContourChain.ApproxSimple, new CvPoint(0, 0)); }