Skip to content

Instantly share code, notes, and snippets.

@TheB1ackSheep
Created December 23, 2014 17:45
Show Gist options
  • Save TheB1ackSheep/dde35eb870f34883ecfb to your computer and use it in GitHub Desktop.
Save TheB1ackSheep/dde35eb870f34883ecfb to your computer and use it in GitHub Desktop.
private void RecDetection(Image<Gray, Byte> img, Image<Bgr, Byte> showRecOnImg, int areaV)
{
Image<Gray, Byte> imgForContour = new Image<Gray, byte>(img.Width, img.Height);
CvInvoke.cvCopy(img, imgForContour, System.IntPtr.Zero);
IntPtr storage = CvInvoke.cvCreateMemStorage(0);
IntPtr contour = new IntPtr();
CvInvoke.cvFindContours(
imgForContour,
storage,
ref contour,
System.Runtime.InteropServices.Marshal.SizeOf(typeof(MCvContour)),
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,
new Point(0, 0));
Seq<Point> seq = new Seq<Point>(contour, null);
for (; seq != null && seq.Ptr.ToInt64() != 0; seq = seq.HNext)
{
Rectangle bndRec = CvInvoke.cvBoundingRect(seq, 2);
double areaC = CvInvoke.cvContourArea(seq, MCvSlice.WholeSeq, 1) * -1;
if (areaC > areaV)
{
CvInvoke.cvRectangle(showRecOnImg, new Point(bndRec.X, bndRec.Y),
new Point(bndRec.X + bndRec.Width, bndRec.Y + bndRec.Height),
new MCvScalar(0, 0, 255), 1, LINE_TYPE.CV_AA, 0);
}
}
pbSrc.Image = showRecOnImg.ToBitmap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment