本文整理汇总了C#中OpenCvSharp.IplImage.FindContours方法的典型用法代码示例。如果您正苦于以下问题:C# IplImage.FindContours方法的具体用法?C# IplImage.FindContours怎么用?C# IplImage.FindContours使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenCvSharp.IplImage
的用法示例。
在下文中一共展示了IplImage.FindContours方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DetectSquares
/// <summary>
/// Detect the square in the image using contours
/// </summary>
/// <param name="img">Image</param>
/// <param name="modifiedImg">Modified image to be return</param>
/// <param name="storage">Memory storage</param>
/// <returns></returns>
public static CvPoint[] DetectSquares(IplImage img)
{
// Debug
//System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
//stopWatch.Start();
using (CvMemStorage storage = new CvMemStorage())
{
// create empty sequence that will contain points -
// 4 points per square (the square's vertices)
CvSeq<CvPoint> squares = new CvSeq<CvPoint>(SeqType.Zero, CvSeq.SizeOf, storage);
using (IplImage timg = img.Clone())
using (IplImage gray = new IplImage(timg.Size, BitDepth.U8, 1))
using (IplImage dstCanny = new IplImage(timg.Size, BitDepth.U8, 1))
{
// Get gray scale
timg.CvtColor(gray, ColorConversion.BgrToGray);
// Canny
Cv.Canny(gray, dstCanny, 70, 300);
// dilate canny output to remove potential
// holes between edge segments
Cv.Dilate(dstCanny, dstCanny, null, 2);
// find contours and store them all as a list
CvSeq<CvPoint> contours;
dstCanny.FindContours(storage, out contours);
// Debug
//Cv.ShowImage("Edge", dstCanny);
//if (contours != null) Console.WriteLine(contours.Count());
// Test each contour
while (contours != null)
{
// Debug
//if (stopWatch.ElapsedMilliseconds > 100)
//{
// Console.WriteLine("ROI detection is taking too long and is skipped.");
//}
// approximate contour with accuracy proportional
// to the contour perimeter
CvSeq<CvPoint> result = Cv.ApproxPoly(contours, CvContour.SizeOf, storage, ApproxPolyMethod.DP, contours.ContourPerimeter() * 0.02, false);
// square contours should have 4 vertices after approximation
// relatively large area (to filter out noisy contours)
// and be convex.
// Note: absolute value of an area is used because
// area may be positive or negative - in accordance with the
// contour orientation
if (result.Total == 4 &&
Math.Abs(result.ContourArea(CvSlice.WholeSeq)) > 250 &&
result.CheckContourConvexity())
{
double s = 0;
for (int i = 0; i < 5; i++)
{
// find minimum Angle between joint
// edges (maximum of cosine)
if (i >= 2)
{
double t = Math.Abs(Angle(result[i].Value, result[i - 2].Value, result[i - 1].Value));
s = s > t ? s : t;
}
}
// if cosines of all angles are small
// (all angles are ~90 degree) then write quandrange
// vertices to resultant sequence
if (s < 0.3)
{
//Console.WriteLine("ROI found!"); // Debug
for (int i = 0; i < 4; i++)
{
//Console.WriteLine(result[i]);
squares.Push(result[i].Value);
}
}
}
// Take the next contour
contours = contours.HNext;
}
}
//stopWatch.Stop();
//Console.WriteLine("ROI Detection : {0} ms", stopWatch.ElapsedMilliseconds); // Debug
return squares.ToArray();
}
}