本文整理汇总了C#中Contour.GetMinAreaRect方法的典型用法代码示例。如果您正苦于以下问题:C# Contour.GetMinAreaRect方法的具体用法?C# Contour.GetMinAreaRect怎么用?C# Contour.GetMinAreaRect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Contour
的用法示例。
在下文中一共展示了Contour.GetMinAreaRect方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: isRectangleByMath
bool isRectangleByMath(Contour<Point> blob)
{
MCvBox2D box = blob.GetMinAreaRect();
double boxRatio = blob.Area / (box.size.Width * box.size.Height);
return boxRatio > 0.7f;
}
示例2: ProcesseFramesToFindSquares
public void ProcesseFramesToFindSquares(Image<Bgr, Byte> pImage)
{
pbBad.Visible = false;
pbGood.Visible = false;
scala_x = (float)(pImage.Width * 1.0 / pictureBox1.Width);
scala_y = (float)(pImage.Height * 1.0 / pictureBox1.Height);
#region square
if (pImage != null)
{
Image<Bgr, Byte> pImg = pImage.Copy();
Image<Gray, Byte> gray = pImg.Convert<Gray, Byte>().PyrDown().PyrUp();
ibHoughCircles.Image = null;
ibHoughCircles.Image = gray;
Gray cannyThreshold = new Gray(180);
Gray cannyThresholdLinking = new Gray(120);
Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);
ibHoughCircles2.Image = null;
ibHoughCircles2.Image = cannyEdges;
LineSegment2D[] lines = cannyEdges.HoughLinesBinary(1, Math.PI / 45.0, 20, 30, 10)[0];
List<MCvBox2D> boxList = new List<MCvBox2D>();
listSquares = new List<MCvBox2D>();
int squares = 0;
using (MemStorage storage = new MemStorage())
{
for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext)
{
//Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
if (contours.Area > 1000 )// Int32.Parse(txtTotalApplicants.Text))
{
if (currentContour.Total == 4 || currentContour.Total == 2 || currentContour.Total == 6)
{
bool isRectangle = true;
Point[] pts = currentContour.ToArray();
LineSegment2D[] edges = PointCollection.PolyLine(pts, true);
//using edges i found coordinates.
for (int i = 0; i < edges.Length; i++)
{
double angle = Math.Abs(edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
if (angle < 85 || angle > 95)
//if (angle < 80 || angle > 110)
//if (angle != 90)
{
isRectangle = false;
break;
}
if (isRectangle)
{
boxList.Add(currentContour.GetMinAreaRect());
// added
MCvBox2D box = contours.GetMinAreaRect();
listSquares.Add(box);
pImg.Draw(box, new Bgr(System.Drawing.Color.Red), 9);
squares++;
}
}
}
}
}
}
ibOriginal.Image = pImg;
txtTotalApplicants.Text = listSquares.Count.ToString(); //squares.ToString();
bool a, b, c, d;
a = b = c = d = false;
for (int i = 0; i < listSquares.Count; i++)
{
//set the x,y position of the 4 squares guides of the scanned papaer
float xp = listSquares[i].center.X / scala_x;
float yp = listSquares[i].center.Y / scala_y;
if (listSquares[i].center.X < pImg.Width / 2 &&
listSquares[i].center.Y < pImg.Height / 2)
{
mayas_corner[3].circles[0].set_X(xp);
mayas_corner[3].circles[0].set_Y(yp);
a = true;
}
if (listSquares[i].center.X > pImg.Width / 2 &&
listSquares[i].center.Y < pImg.Height / 2)
{
mayas_corner[3].circles[1].set_X(xp);
mayas_corner[3].circles[1].set_Y(yp);
b = true;
}
if (listSquares[i].center.X < pImg.Width / 2 &&
listSquares[i].center.Y > pImg.Height / 2)
{
mayas_corner[3].circles[2].set_X(xp);
mayas_corner[3].circles[2].set_Y(yp);
c = true;
}
if (listSquares[i].center.X > pImg.Width / 2 &&
//.........这里部分代码省略.........
示例3: ProcesseFrameToFindCircles
//.........这里部分代码省略.........
//ibHoughCircles.Image = imgSobel;
//imgProcessed = imgProcessed.SmoothGaussian(3);
//ibHoughCircles2.Image = imgProcessed;
//THRESHOLD_BINARY
//imgProcessed = imgProcessed.ThresholdBinary(new Gray(THRESHOLD_BINARY_MIN), new Gray(THRESHOLD_BINARY_MAX)); //ThresholdBinary
//imgProcessed = imgProcessed.ThresholdBinaryInv(new Gray(THRESHOLD_BINARY_MIN), new Gray(THRESHOLD_BINARY_MAX)); //ThresholdBinary
#region MemStorage
contoursPositions.Clear();
#region Circles
using (MemStorage stor = new MemStorage())
{
//Find contours with no holes try CV_RETR_EXTERNAL to find holes
contours = imgProcessed.FindContours(
Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
stor);
//contours = imgProcessed.FindContours(
// Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
// Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP,
// stor);
//markers.Clear();
for (int i = 0; contours != null; contours = contours.HNext)
{
i++;
//if ((contours.Area > Math.Pow(17, 2)) && (contours.Area < Math.Pow(45, 2)))
if ((contours.Area > Math.Pow(7, 2)) && (contours.Area < Math.Pow(45, 2)))
{
MCvBox2D box = contours.GetMinAreaRect();
CountorsPositions cp = new CountorsPositions(box.center.X, box.center.Y,(float)contours.Area);
contoursPositions.Add(cp);
//pImg.Draw(box, new Bgr(System.Drawing.Color.Red), 3);
//blobCount++;
CvInvoke.cvCircle(pImg,
new System.Drawing.Point((int)box.center.X, (int)box.center.Y),
2,
new MCvScalar(0, 255, 0),
-1,
LINE_TYPE.CV_AA,
0);
PointF p = new PointF((int)box.center.X, (int)box.center.Y);
CircleF c = new CircleF(p, (float)Math.Sqrt(contours.Area));
pImg.Draw(c,
new Bgr(System.Drawing.Color.Orange),
3);
//markers.Add(new Point((int)box.center.X, (int)box.center.Y));
}
}
}
#endregion
//cleanList();
FillingAnswersToAbstractObject();
FillingAnswerstToFinalQuestionsList();
示例4: IdentifyObject
private IdentifiedObject IdentifyObject(Contour<Point> contour, double ContourAccuracy, long timeinms)
{
//PointF center;
//int boundingWidth = contour.BoundingRectangle.Width;
//double dx = Double.Parse (boundingWidth.ToString ());
// // horizontal coordinates of center point of area
// dx = dx * 0.5;
// dx = dx + contour.BoundingRectangle.X;
//int boundingHeight = contour.BoundingRectangle.Height;
bool circ = false;
bool rect = false;
string strheight = contour.GetMinAreaRect ().size.Height.ToString ();
string strwidth = contour.GetMinAreaRect ().size.Width.ToString ();
// set accuracy for shape detection, since it depends on contouracc, we'll do it as follows
//int boundaries = 6;
// if (ContourAccuracy >= 0.005) {
// boundaries = 8;
// }
// Example: the bounding box can be 90 degrees rotated from the bounding area square
// to make sure the right edges are compared the ratio is used to widen the range when needed
// the following values resulted in a 99.9% true negative when fully detected, and some colours 100% true positive
double ratio = double.Parse (strheight) / double.Parse (strwidth);
if (ratio < 1)
ratio = 1 / ratio;
ratio = ratio * ratio * 1.05;
double rectHeight = (double)contour.BoundingRectangle.Height;
double rectWidth = (double)contour.BoundingRectangle.Width;
// find center
double dy = 0.5 * rectHeight + contour.BoundingRectangle.Y;
double dx = 0.5 * rectWidth + contour.BoundingRectangle.X;
// old: if (double.Parse(strheight) <= contour.BoundingRectangle.Height + 2 && int.Parse(strheight) >= contour.BoundingRectangle.Height - boundaries)
if (double.Parse (strheight) <= rectHeight * ratio && double.Parse (strheight) >= rectHeight / ratio) {
if (double.Parse (strwidth) <= rectWidth * ratio && double.Parse (strwidth) >= rectWidth / ratio) {
// // is it non-elliptical?
if (double.Parse (strwidth) * 0.9 < double.Parse (strheight) && (double.Parse (strwidth) * 1.1 > double.Parse (strheight))) {
circ = true;
//Console.WriteLine (contour.GetMinAreaRect ().size.Height.ToString ());
} else {
return null;
//Console.WriteLine ("elliptical");
}
} else {
// Console.WriteLine ("width not a circle? float:" + currentContour.GetMinAreaRect ().size.Width.ToString () + " int: " + strwidth + " exp: " + currentContour.BoundingRectangle.Width.ToString ());
}
} else {
// Console.WriteLine ("height not a circle? float:" + currentContour.GetMinAreaRect ().size.Height.ToString () + " int: " + strheight + " exp: " + currentContour.BoundingRectangle.Height.ToString ());
}
// sometimes pc would act up
if (dy == 240) {
// this usually happens when the variables aren't being refreshed properly, this happened with internal cam on laptop
Console.WriteLine ("oops, this shouldnt occur often with the ps eye cam, you using the right one?");
//break;
}
// float coordinates of center of area
float xf;
float yf;
//Console.WriteLine ("doubley: " + dy);
// convert values to floats
float.TryParse (dx.ToString (), out xf);
float.TryParse (dy.ToString (), out yf);
// if (contour == biggestContour) {
// center = new PointF (xf, yf);
// }
//PointF center = new PointF (xf, yf);
//CircleF circle = new CircleF (center2, float.Parse (contour.Area.ToString ()));
//CircleF circle = new CircleF (center, 20f);
// compare square footing of expected circle with contour area
// calc average radius
double avgr = (rectWidth + rectHeight) / 4;
double total = Math.PI * avgr * avgr;
//Console.WriteLine ("circle totalarea" + contour.Area.ToString ()+ " calculated area " + total.ToString());
double area = Convert.ToDouble (contour.Area);
if (circ) if (area > total * 1.02) {
circ = false;
rect = true;
// I'm actually a square after all! just nearly vertical
}
// if object doesn't already exist create new one
IDcounter++;
IdentifiedObject io = new IdentifiedObject ((int)dx, (int)dy, Convert.ToDouble (contour.Area), circ, rect, IDcounter, timeinms);
return io;
}
示例5: DetectPlate
private void DetectPlate(Contour<Point> contours, List<MCvBox2D> detectedLicensePlateRegionList)
{
for (; contours != null; contours = contours.HNext)
{
int numberOfChildren = GetNumberOfChildren(contours);
if (numberOfChildren == 0) continue;
if (contours.Area > 400)
{
if (numberOfChildren < 3)
{
DetectPlate(contours.VNext, detectedLicensePlateRegionList);
continue;
}
MCvBox2D box = contours.GetMinAreaRect();
if (box.angle < -45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle += 90.0f;
}
else if (box.angle > 45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle -= 90.0f;
}
double whRatio = (double)box.size.Width / box.size.Height;
if (!(3.0 < whRatio && whRatio < 10.0))
{
Contour<Point> child = contours.VNext;
if (child != null)
DetectPlate(child, detectedLicensePlateRegionList);
continue;
}
detectedLicensePlateRegionList.Add(box);
}
}
}
示例6: FindLicensePlate
private void FindLicensePlate(
Contour<Point> contours, Image<Gray, Byte> gray, Image<Gray, Byte> canny,
List<Image<Gray, Byte>> licensePlateImagesList, List<Image<Gray, Byte>> filteredLicensePlateImagesList, List<MCvBox2D> detectedLicensePlateRegionList,
List<String> licenses)
{
for (; contours != null; contours = contours.HNext)
{
int numberOfChildren = GetNumberOfChildren(contours);
//if it does not contains any children (charactor), it is not a license plate region
if (numberOfChildren == 0) continue;
if (contours.Area > 400)
{
if (numberOfChildren < 3)
{
//If the contour has less than 3 children, it is not a license plate (assuming license plate has at least 3 charactor)
//However we should search the children of this contour to see if any of them is a license plate
FindLicensePlate(contours.VNext, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
MCvBox2D box = contours.GetMinAreaRect();
if (box.angle < -45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle += 90.0f;
}
else if (box.angle > 45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle -= 90.0f;
}
double whRatio = (double)box.size.Width / box.size.Height;
if (!(3.0 < whRatio && whRatio < 10.0))
//if (!(1.0 < whRatio && whRatio < 2.0))
{ //if the width height ratio is not in the specific range,it is not a license plate
//However we should search the children of this contour to see if any of them is a license plate
Contour<Point> child = contours.VNext;
if (child != null)
FindLicensePlate(child, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
using (Image<Gray, Byte> tmp1 = gray.Copy(box))
//resize the license plate such that the front is ~ 10-12. This size of front results in better accuracy from tesseract
using (Image<Gray, Byte> tmp2 = tmp1.Resize(240, 180, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC, true))
{
//removes some pixels from the edge
int edgePixelSize = 2;
tmp2.ROI = new Rectangle(new Point(edgePixelSize, edgePixelSize), tmp2.Size - new Size(2 * edgePixelSize, 2 * edgePixelSize));
Image<Gray, Byte> plate = tmp2.Copy();
Image<Gray, Byte> filteredPlate = FilterPlate(plate);
Tesseract.Charactor[] words;
StringBuilder strBuilder = new StringBuilder();
using (Image<Gray, Byte> tmp = filteredPlate.Clone())
{
_ocr.Recognize(tmp);
words = _ocr.GetCharactors();
if (words.Length == 0) continue;
for (int i = 0; i < words.Length; i++)
{
strBuilder.Append(words[i].Text);
}
}
licenses.Add(strBuilder.ToString());
licensePlateImagesList.Add(plate);
filteredLicensePlateImagesList.Add(filteredPlate);
detectedLicensePlateRegionList.Add(box);
}
}
}
}
示例7: FindLicensePlate
private void FindLicensePlate(
Contour<Point> contours, Image<Gray, Byte> gray, Image<Gray, Byte> canny,
List<Image<Gray, Byte>> licensePlateImagesList, List<Image<Gray, Byte>> filteredLicensePlateImagesList, List<MCvBox2D> detectedLicensePlateRegionList,
List<List<Word>> licenses)
{
for (; contours != null; contours = contours.HNext)
{
int numberOfChildren = GetNumberOfChildren(contours);
//if it does not contains any children (charactor), it is not a license plate region
if (numberOfChildren == 0) continue;
//Check area of licensePlate
if (contours.Area > 400)
{
if (numberOfChildren < 3)
{
//If the contour has less than 3 children, it is not a license plate (assuming license plate has at least 3 charactor)
//However we should search the children of this contour to see if any of them is a license plate
FindLicensePlate(contours.VNext, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
MCvBox2D box = contours.GetMinAreaRect();
if (box.angle < -45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle += 90.0f;
}
else if (box.angle > 45.0)
{
float tmp = box.size.Width;
box.size.Width = box.size.Height;
box.size.Height = tmp;
box.angle -= 90.0f;
}
double whRatio = (double)box.size.Width / box.size.Height;
if (!(1.0 < whRatio && whRatio < 2.0))
{ //if the width height ratio is not in the specific range,it is not a license plate
//However we should search the children of this contour to see if any of them is a license plate
Contour<Point> child = contours.VNext;
if (child != null)
FindLicensePlate(child, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
//box.size.Width -= 2;
//box.size.Height -= 2;
Image<Gray, Byte> plate = gray.Copy(box);
Image<Gray, Byte> filteredPlate = FilterPlate(plate);
List<Word> words = new List<Word>();
StringBuilder strBuilder = new StringBuilder();
using (Bitmap bmp = filteredPlate.Bitmap)
{
words = _ocr.DoOCR(plate.Bitmap, plate.ROI);
if (words.Count == 0) continue;
for (int i = 0; i < words.Count; i++)
{
strBuilder.Append(words[i].Text);
}
}
licenses.Add(words);
licensePlateImagesList.Add(plate);
filteredLicensePlateImagesList.Add(filteredPlate);
detectedLicensePlateRegionList.Add(box);
return;
}
}
}
示例8: FindRect
/// <summary>
///
/// </summary>
/// <param name="img"></param>
/// <param name="stopSignList"></param>
/// <param name="boxList"></param>
/// <param name="contours"></param>
/// <param name="color">1=dark blue, 2 = pink, 3 = light green, 4 = purple, 5 = black, 6 = white</param>
private void FindRect(Image<Bgr, byte> img, List<Image<Gray, Byte>> stopSignList, List<Rectangle> boxList, Contour<Point> contours, int color)
{
int i=0;
MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
for (; contours != null; contours = contours.HNext)
{
//draw box from any contour
//imageGray.Draw(new CircleF(centerBox(contours.BoundingRectangle), 3), new Gray(150), 2);
contours.ApproxPoly(contours.Perimeter * 0.02, 0, contours.Storage);
if (contours.Area > 100 && contours.Total>0)
{
//handle normal rectangle
MCvBox2D minAreaRect = contours.GetMinAreaRect();
shapeRatio = CvInvoke.cvMatchShapes(rect, contours, Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3, 0);
double areaRatio = areaSize(minAreaRect.size) / contours.Area;
PointF c = centerBox(contours.BoundingRectangle);
if (shapeRatio < 0.1 && areaRatio<1.2)
{
Rectangle box = contours.BoundingRectangle;
//imageGray.Draw(box, new Gray(150), 1);
//imageGray.Draw(contours, new Gray(50), 2);
imageGray.Draw("" + i, ref f, new Point((int)c.X, (int)c.Y), new Gray(200));
pointBlack[i] = c;
minBoxesBlack[i] = minAreaRect;
foreach (Point p in contours) joinContour.Push(p);
i++;
}
//handle the rectangle with diferent orientation
else if (areaRatio < 1.3 && shapeRatio < 0.5)
{
Rectangle box = contours.BoundingRectangle;
//imageGray.Draw(box, new Gray(150), 1);
//imageGray.Draw(contours, new Gray(50), 2);
imageGray.Draw("" + i, ref f, new Point((int)c.X, (int)c.Y), new Gray(200));
pointBlack[i] = c;
minBoxesBlack[i] = minAreaRect;
i++;
foreach (Point p in contours) joinContour.Push(p);
} else
imageGray.Draw("" + i, ref f, new Point((int)c.X, (int)c.Y), new Gray(100));
}
}
}
示例9: FindLicensePlate
private void FindLicensePlate(
Contour<Point> contours, Image<Gray, Byte> gray, Image<Gray, Byte> canny,
List<Image<Gray, Byte>> licensePlateImagesList, List<Image<Gray, Byte>> filteredLicensePlateImagesList, List<MCvBox2D> detectedLicensePlateRegionList,
List<List<Word>> licenses)
{
for (; contours != null; contours = contours.HNext)
{
int numberOfChildren = GetNumberOfChildren(contours);
//if it does not contains any children (charactor), it is not a license plate region
if (numberOfChildren == 0) continue;
if (contours.Area > 100)
{
if (numberOfChildren < 3)
{
//If the contour has less than 3 children, it is not a license plate (assuming license plate has at least 3 charactor)
//However we should search the children of this contour to see if any of them is a license plate
FindLicensePlate(contours.VNext, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
MCvBox2D box = contours.GetMinAreaRect();
double whRatio = (double)box.size.Width / box.size.Height;
if (!(3.0 < whRatio && whRatio < 10.0))
{ //if the width height ratio is not in the specific range,it is not a license plate
//However we should search the children of this contour to see if any of them is a license plate
Contour<Point> child = contours.VNext;
if (child != null)
FindLicensePlate(child, gray, canny, licensePlateImagesList, filteredLicensePlateImagesList, detectedLicensePlateRegionList, licenses);
continue;
}
box.size.Width -= 2;
box.size.Height -= 2;
Image<Gray, Byte> plate = gray.Copy(box);
Image<Gray, Byte> filteredPlate = FilterPlate(plate);
List<Word> words;
using (Bitmap bmp = filteredPlate.Bitmap)
words = _ocr.DoOCR(bmp, filteredPlate.ROI);
licenses.Add(words);
licensePlateImagesList.Add(plate);
filteredLicensePlateImagesList.Add(filteredPlate);
detectedLicensePlateRegionList.Add(box);
}
}
}
示例10: FindRect
/// <summary>
///
/// </summary>
/// <param name="img"></param>
/// <param name="contours"></param>
/// <param name="color">1=dark blue, 2 = pink, 3 = light green, 4 = purple, 5 = black, 6 = white</param>
private void FindRect(Image<Bgr, byte> img, Contour<Point> contours, int color)
{
int i = 0;
MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 0.8, 0.8);
for (; contours != null; contours = contours.HNext)
{
contours.ApproxPoly(contours.Perimeter * 0.02, 0, contours.Storage);
if (contours.Area > 100 && contours.Total > 0)
{
MCvBox2D minAreaRect = contours.GetMinAreaRect();
shapeRatio = CvInvoke.cvMatchShapes(rect, contours, Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3, 0);
double areaRatio = areaSize(minAreaRect.size) / contours.Area;
PointF c = centerBox(contours.BoundingRectangle);
if (shapeRatio < 0.1 && areaRatio < 1.2)
{
Rectangle box = contours.BoundingRectangle;
pointBlack[i] = c;
minBoxesBlack[i] = minAreaRect;
i++;
}
//handle the rectangle with diferent orientation
else if (areaRatio < 1.3 && shapeRatio < 0.5)
{
Rectangle box = contours.BoundingRectangle;
pointBlack[i] = c;
minBoxesBlack[i] = minAreaRect;
i++;
}
else
imageGray.Draw("" + i, ref f, new Point((int)c.X, (int)c.Y), new Gray(100));
}
}
}