她的一些代碼,您可以使用使用AForge.NET框架來識別圖像中的四角形:
// get angles between 2 pairs of opposite sides
float angleBetween1stPair = Tools.GetAngleBetweenLines(corners[0], corners[1], corners[2], corners[3]);
float angleBetween2ndPair = Tools.GetAngleBetweenLines(corners[1], corners[2], corners[3], corners[0]);
// check 1st pair for parallelism
if (angleBetween1stPair <= angleError)
{
subType = PolygonSubType.Trapezoid;
// check 2nd pair for parallelism
if (angleBetween2ndPair <= angleError)
{
subType = PolygonSubType.Parallelogram;
// check angle between adjacent sides
if (Math.Abs(Tools.GetAngleBetweenVectors(corners[1], corners[0], corners[2]) - 90) <= angleError)
subType = PolygonSubType.Rectangle;
//get length of 2 adjacent sides
float side1Length = (float)corners[0].DistanceTo(corners[1]);
float side2Length = (float)corners[0].DistanceTo(corners[3]);
if (Math.Abs(side1Length - side2Length) <= maxLengthDiff)
subType = (subType == PolygonSubType.Parallelogram) ? PolygonSubType.Rhombus : PolygonSubType.Square;
}
}
else
{
// check 2nd pair for parallelism - last chence to detect trapezoid
if (angleBetween2ndPair <= angleError)
{
subType = PolygonSubType.Trapezoid;
}
}
請參見本文的如何檢測各種形狀的例子:
http://www.aforgenet.com/articles/shape_checker/
這裏下載AForge.NET框架的鏈接:
http://www.aforgenet.com/framework/downloads.html
我想你不僅可以從圖片中確定的比例。您需要更多信息。例如,如果在同一張照片中有正方形,或者如果您知道透視參數,等等...... – Francisco