/// <summary> /// Вычисление минимальной площади прямоугольника, обрамляющего два области /// </summary> /// <param name="firstRegion">Первая область</param> /// <param name="secondRegion">Вторая область</param> /// <returns></returns> private int CalculateAllTextBoxSquare(TextRegion firstRegion, TextRegion secondRegion) { try { int minAllBBI = Math.Min(firstRegion.MinBorderIndexI, secondRegion.MinBorderIndexI); int minAllBBJ = Math.Min(firstRegion.MinBorderIndexJ, secondRegion.MinBorderIndexJ); int maxAllBBI = Math.Max(firstRegion.MaxBorderIndexI, secondRegion.MaxBorderIndexI); int maxAllBBJ = Math.Max(firstRegion.MaxBorderIndexJ, secondRegion.MaxBorderIndexJ); return (maxAllBBI - minAllBBI + 1) * (maxAllBBJ - minAllBBJ + 1); } catch (Exception excption) { throw excption; } }
/// <summary> /// Принадлежит ли точка с заданными координатами области /// </summary> /// <param name="textRegion">Область</param> /// <param name="pointIndexI">Координата по строке</param> /// <param name="pointIndexJ">Координата по столбцу</param> /// <returns>1 - принадлежит, 0 - иначе</returns> private bool IsBelongToRegion(TextRegion textRegion, int pointIndexI, int pointIndexJ) { try { bool result = false; if (pointIndexI >= textRegion.MinBorderIndexI && pointIndexJ >= textRegion.MinBorderIndexJ && pointIndexJ <= textRegion.MaxBorderIndexJ && pointIndexI <= textRegion.MaxBorderIndexI) result = true; return result; } catch (Exception exception) { throw exception; } }
/// <summary> /// Вычисление площади пересечения двух текстовых областей /// </summary> /// <param name="firstRegion">Первая область</param> /// <param name="secondRegion">Вторая область</param> /// <returns></returns> private int CalculateIntersectionSquare(TextRegion firstRegion, TextRegion secondRegion) { try { int minAllBBI = Math.Min(firstRegion.MinBorderIndexI, secondRegion.MinBorderIndexI); int minAllBBJ = Math.Min(firstRegion.MinBorderIndexJ, secondRegion.MinBorderIndexJ); int maxAllBBI = Math.Max(firstRegion.MaxBorderIndexI, secondRegion.MaxBorderIndexI); int maxAllBBJ = Math.Max(firstRegion.MaxBorderIndexJ, secondRegion.MaxBorderIndexJ); int square = 0; for (int i = minAllBBI; i <= maxAllBBI; i++) for (int j = minAllBBJ; j <= maxAllBBJ; j++) if (IsBelongToRegion(firstRegion, i, j) && IsBelongToRegion(secondRegion, i, j)) ++square; return square; } catch (Exception exception) { throw exception; } }
/// <summary> /// Вычисление вероятности совпадения двух текстовых областей /// </summary> /// <param name="firstRegion">Первая текстовая область</param> /// <param name="secondRegion">Вторая текстовая область</param> /// <returns></returns> private double CalculateMatch(TextRegion firstRegion, TextRegion secondRegion) { try { int allBBSquare = CalculateAllTextBoxSquare(firstRegion, secondRegion); int intersectionSquare = CalculateIntersectionSquare(firstRegion, secondRegion); return (double) intersectionSquare / (double) allBBSquare; } catch (Exception exception) { throw exception; } }
/// <summary> /// Считывание текстовых областей для одного кадра /// </summary> /// <param name="xmlReader">xml - reader</param> /// <param name="textRegions">Текстовые области</param> /// <param name="frameNumber">Номер кадра</param> private static void ReadFrameTextBlocksInformation(System.Xml.XmlReader xmlReader, out List<TextRegion> textRegions, out int frameNumber) { try { textRegions = new List<TextRegion>(); xmlReader.MoveToFirstAttribute(); frameNumber = Convert.ToInt32(xmlReader.Value); xmlReader.MoveToNextAttribute(); int textRegionsNumber = Convert.ToInt32(xmlReader.Value); if (textRegionsNumber != 0) { bool readingFrame = true; while (xmlReader.Read() && readingFrame) { if (xmlReader.Name == "Frame") readingFrame = false; else if (xmlReader.Name == "TextRegion") { TextRegion textRegion = new TextRegion(); while (xmlReader.MoveToNextAttribute()) { switch (xmlReader.Name) { case "LeftUpPointIndexI": textRegion.MinBorderIndexI = Convert.ToInt32(xmlReader.Value); break; case "LeftUpPointIndexJ": textRegion.MinBorderIndexJ = Convert.ToInt32(xmlReader.Value); break; case "RightDownPointIndexI": textRegion.MaxBorderIndexI = Convert.ToInt32(xmlReader.Value); break; case "RightDownPointIndexJ": textRegion.MaxBorderIndexJ = Convert.ToInt32(xmlReader.Value); break; default: break; } } textRegions.Add(textRegion); } } } } catch (Exception exception) { throw exception; } }