Example #1
0
    def CheckIfIntersectingLineSegment(p1, p2, p3, p4):
        tolerance = Constants.PRECISION
        Pixeltolerance = 0.5
        x1 = p1.x
        x2 = p2.x
        x3 = p3.x
        x4 = p4.x
        y1 = p1.y
        y2 = p2.y
        y3 = p3.y
        y4 = p4.y
        numeratorA = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3))
        numeratorB = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3))
        denominator = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1))

        if (MathUtils.EQTF(denominator, 0.0, tolerance)):
            if (MathUtils.EQTF(numeratorB, 0.0, tolerance)
                    and MathUtils.EQTF(numeratorA, 0.0, tolerance)):
                return Cognition.CheckForCoincidentLineSegments(p1, p2, p3, p4)
            return False
        ua = numeratorA / denominator
        ub = numeratorB / denominator

        if ((ua > (0.0 - Pixeltolerance)) and (ua < (1.0 + Pixeltolerance))
                and (ub > (0.0 - Pixeltolerance))
                and (ub < (1.0 + Pixeltolerance))):
            return True
        return False
Example #2
0
 def ProjectLineSegmentEnds(self):
     projLineSegments = []
     for ls in self._lineSegments:
         lss = ls.startPoint
         lse = ls.endPoint
         lssp = MathUtils.ProjectToLine2(self._line.startPoint,
                                         self._line.endPoint, lss)
         lsep = MathUtils.ProjectToLine2(self._line.startPoint,
                                         self._line.endPoint, lse)
         lsp = Line2(lssp, lsep)
         projLineSegments.append(lsp)
     self._lineSegments = projLineSegments
Example #3
0
    def ArrowHeadDirection(Feature_Manager):
        OriginalImg = Feature_Manager._ImageOriginal.copy()
        OutputImg = OriginalImg.copy()
        DimensionalLines = Feature_Manager._DetectedDimensionalLine
        ArrowHeadsList = Feature_Manager._DetectedArrowHead
        for dl in DimensionalLines:
            for ah in dl._ArrowHeads:
                p1 = ah._BoundingBoxP1
                p2 = ah._BoundingBoxP2
                TempImg = OriginalImg[p1.y:p2.y + 4, p1.x:p2.x + 4]
                TempImg = ImgTransform.ImgAspectResize(TempImg, 100, 100)

                cornerImg = TempImg.copy()
                gray = cv2.cvtColor(TempImg, cv2.COLOR_BGR2GRAY)
                corners = cv2.goodFeaturesToTrack(gray, 20, 0.09, 10, True)
                corners = np.int0(corners)
                cornerPts = []
                xpts = []
                ypts = []
                for i in corners:
                    x, y = i.ravel()
                    p = Point2(x, y)
                    xpts.append(x)
                    ypts.append(y)
                    cornerPts.append(p)
                    cv2.circle(cornerImg, (x, y), 1, 255, -1)
                xc = np.mean(xpts)
                yc = np.mean(ypts)
                c = Point2(xc, yc)
                dictPt = {}
                for i in cornerPts:
                    d = sqrt((c.x - i.x)**2 + (c.y - i.y)**2)
                    dictPt[i] = d
                sortedDict = sorted(dictPt.items(), key=operator.itemgetter(1))
                extremePt = sortedDict[len(sortedDict) - 1][0]
                cv2.circle(cornerImg, (extremePt.x, extremePt.y), 3, 255, -1)
                projectedPt = MathUtils.ProjectToLine2(
                    dl._Leaders[0].startPoint, dl._Leaders[0].endPoint,
                    extremePt)
                projectedCorner = MathUtils.ProjectToLine2(
                    dl._Leaders[0].startPoint, dl._Leaders[0].endPoint, c)
                Direction = Cognition.GetDirection(projectedPt,
                                                   projectedCorner)
                ah._Direction = Direction
                Cognition.AssignArrowHeadsDirection(ArrowHeadsList,
                                                    ah._ArrowCenter, Direction)
                print(Direction)
Example #4
0
 def ProjectCorners(self):
     corners = self._cornerPoints
     projCorners = []
     for cp in corners:
         pcp = MathUtils.ProjectToLine2(self._line.startPoint,
                                        self._line.endPoint, cp)
         projCorners.append(pcp)
     self._cornerPoints = projCorners
Example #5
0
 def OnLine(self, point):
     s = self.startPoint
     e = self.endPoint
     dir = e - s
     dir.Normalize()
     d = MathUtils.Distance_PointToLine2(s, dir, point)
     if (d <= 5):
         return True
     return False
Example #6
0
 def CheckIfOverlapLineSegments(Rect1_Segments, Rect2_Segments):
     for i in Rect1_Segments:
         p1 = i.startPoint
         p2 = i.endPoint
         for j in Rect2_Segments:
             p3 = j.startPoint
             p4 = j.endPoint
             Intersects = MathUtils.Check_Intersects_LineSegmentLineSegment(
                 p1, p2, p3, p4)
             if Intersects == True:
                 return True
     return False
Example #7
0
 def SetEnds(self):
     points = []
     numCorners = len(self._cornerPoints)
     if numCorners >= 1:
         lsp1, lsp2 = SpecialLineSegments.SeggregatePoints(
             self._lineSegments, self._line)
         lsp1 = MathUtils.ProjectToLine2(self._line.startPoint,
                                         self._line.endPoint, lsp1)
         lsp2 = MathUtils.ProjectToLine2(self._line.startPoint,
                                         self._line.endPoint, lsp2)
         cs = self._cornerPoints[0]
         ce = self._cornerPoints[numCorners - 1]
         points.append(lsp1)
         points.append(lsp2)
         points.append(cs)
         points.append(ce)
         sortedEndPts = SpecialLineSegments.sortPoints(points, self._line)
         numPts = len(sortedEndPts)
         s = sortedEndPts[0]
         e = sortedEndPts[numPts - 1]
         line = Line2(s, e)
         self._line = line
Example #8
0
 def getAngleBetweenLineAndAxis(p1, p2):
     line = Line2(p1, p2)
     up1 = Cognition.GetUParam(p1, line)
     up2 = Cognition.GetUParam(p2, line)
     if up2 > up1:
         deltaY = p2.y - p1.y
         deltaX = p2.x - p1.x
     else:
         deltaY = -p1.y - p2.y
         deltaX = -p1.x - p2.x
     radians = Cognition.angle_trunc(atan2(deltaY, deltaX))
     degrees = MathUtils.RadToDeg(radians)
     degrees = (360 - degrees)
     return degrees
Example #9
0
 def GetNearestLineSegment(point, ls, lineSegments):
     if lineSegments.Length == 0:
         return None
     nearestSegment = None
     minDist = sys.float_info.max
     nearestPoint = None
     for l in lineSegments:
         if Cognition.areSameLineSegments(l, ls):
             continue
         nearestPointDist, nearestPoint = MathUtils.MinDistance_PointToLineSegment(
             l.startPoint, l.endPoint, point)
         if minDist >= nearestPointDist:
             nearestSegment = l
             minDist = nearestPointDist
             nearestPoint = nearestPoint
     return nearestSegment, minDist, nearestPoint
Example #10
0
    def JoinLineSegmentsWithinProximityTolerance(Feature_Manager):
        entityLineSegments = Feature_Manager._DetectedLine
        PIXEL_TOLERANCE = 8
        updatedLineSegments = []
        lineSegments = Cognition.flattenLineSegmentLists(entityLineSegments)

        for lsToExtend in lineSegments:
            dir = lsToExtend.endPoint - lsToExtend.startPoint
            dir = dir.Normalize()
            start = lsToExtend.startPoint + (dir.Negate() * PIXEL_TOLERANCE)
            end = lsToExtend.endPoint + (dir * PIXEL_TOLERANCE)

            minDistanceStart = sys.float_info.max
            minDistanceEnd = sys.float_info.max
            startExtensionPoint = None
            endExtensionPoint = None

            for lsTemp in lineSegments:
                if lsTemp == lsToExtend:
                    continue

                result, intersection = MathUtils.Intersects_LineSegmentLineSegment(
                    lsTemp.startPoint, lsTemp.endPoint, start, end)
                if result == False or intersection is None:
                    continue

                dist = lsToExtend.startPoint.DistanceTo(intersection)
                if dist < minDistanceStart and dist < PIXEL_TOLERANCE:
                    minDistanceStart = dist
                    startExtensionPoint = intersection

                dist = lsToExtend.endPoint.DistanceTo(intersection)
                if dist < minDistanceEnd and dist < PIXEL_TOLERANCE:
                    minDistanceEnd = dist
                    endExtensionPoint = intersection

            if startExtensionPoint is not None and minDistanceStart <= PIXEL_TOLERANCE:
                lsToExtend.startPoint = startExtensionPoint

            if endExtensionPoint is not None and minDistanceEnd <= PIXEL_TOLERANCE:
                lsToExtend.endPoint = endExtensionPoint

            updatedLineSegments.append(lsToExtend)

        Feature_Manager._DetectedLine = updatedLineSegments
Example #11
0
    def EntityCorrelation(Feature_Manager):
        entityLines = Feature_Manager._DetectedLine
        dimensions = Feature_Manager._DetectedDimension
        EntitiesCorrelated = []
        for d in dimensions:
            CorrelatedLines = []
            cE = CorrelatedEntity()
            for a in d._DimensionalLines._ArrowHeads:
                BB = Cognition.SortCoordinates(
                    [a._BoundingBoxP1, a._BoundingBoxP2])
                bbMin = BB[0]
                bbMax = BB[1]
                direction = a._Direction
                if direction == "West":
                    shortListedLines = []
                    for l in entityLines:
                        if bbMin.x + 3 > l.startPoint.x and fabs(
                                l.endPoint.x - l.startPoint.x) < 3 and fabs(
                                    l.endPoint.y - l.startPoint.y) > 5:
                            shortListedLines.append(l)

                    for l in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            l.startPoint, l.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            CorrelatedLines.append(l)

                elif direction == "East":
                    shortListedLines = []
                    for l in entityLines:

                        if bbMax.x - 3 < l.startPoint.x and fabs(
                                l.endPoint.x - l.startPoint.x) < 3 and fabs(
                                    l.endPoint.y - l.startPoint.y) > 5:
                            shortListedLines.append(l)

                    for l in shortListedLines:

                        projectedPt = MathUtils.ProjectToLine2(
                            l.startPoint, l.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            CorrelatedLines.append(l)

                elif direction == "North":
                    shortListedLines = []
                    for l in entityLines:
                        if bbMin.y + 3 > l.startPoint.y and fabs(
                                l.endPoint.y - l.startPoint.y) < 3 and fabs(
                                    l.endPoint.x - l.startPoint.x) > 5:
                            shortListedLines.append(l)

                    for l in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            l.startPoint, l.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            CorrelatedLines.append(l)

                elif direction == "South":
                    shortListedLines = []
                    for l in entityLines:
                        if bbMax.y - 3 < l.startPoint.y and fabs(
                                l.endPoint.y - l.startPoint.y) < 3 and fabs(
                                    l.endPoint.x - l.startPoint.x) > 5:
                            shortListedLines.append(l)

                    for l in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            l.startPoint, l.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            CorrelatedLines.append(l)

            cE.ExtractCorrelatedEntity(cE, d, CorrelatedLines)
            EntitiesCorrelated.append(cE)
        Feature_Manager._CorrelatedEntities = EntitiesCorrelated
Example #12
0
    def Detect(Feature_Manager):

        lines = Feature_Manager._DetectedLine
        dimension = Feature_Manager._DetectedDimension
        supportLinesegments = []
        entityLinesegments = []
        dist_Img = Cognition.DistanceTransform(Feature_Manager._ImageCleaned)
        minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dist_Img)
        for l in lines:
            for seg in l:
                PointsBetweenLine = SpecialLineSegments.PixelScanner(
                    seg.startPoint, seg.endPoint, dist_Img)
                sLinesPoints = []
                eLinePoints = []
                for i in PointsBetweenLine:
                    t = Cognition.CheckThicknessInVicinity(
                        i[0], i[1], dist_Img)
                    pt = Point2(i[0], i[1])
                    if t is None:
                        continue
                    if t <= 0.6 * maxVal:
                        sLinesPoints.append(pt)
                    else:
                        eLinePoints.append(pt)
                sortSuppPts = sLinesPoints
                sortEntPts = eLinePoints
                if (len(sortSuppPts) > 1):
                    startsupportPt = sortSuppPts[0]
                    for i in range(0, len(sortSuppPts) - 1):
                        p1 = sortSuppPts[i]
                        p2 = sortSuppPts[i + 1]
                        dist = p1.DistanceTo(p2)
                        if dist > 30:
                            Supportlinesegment = Line2(startsupportPt, p1)
                            supportLinesegments.append(Supportlinesegment)
                            startsupportPt = p2
                    Supportlinesegment = Line2(startsupportPt, p2)
                    supportLinesegments.append(Supportlinesegment)
                if (len(sortEntPts) > 1):
                    startentityPt = sortEntPts[0]
                    elines = []
                    for i in range(0, len(sortEntPts) - 1):
                        eline = []
                        p1 = sortEntPts[i]
                        p2 = sortEntPts[i + 1]
                        dist = p1.DistanceTo(p2)
                        if dist > 2:
                            entitylinesegment = Line2(startentityPt, p1)
                            eline.append(entitylinesegment)
                            entityLinesegments.append(eline)
                            startentityPt = p2
                    entitylinesegment = Line2(startentityPt, p2)
                    elines.append(entitylinesegment)
                    entityLinesegments.append(elines)
        for d in dimension:
            SupportL = []
            for a in d._DimensionalLines._ArrowHeads:
                BB = Cognition.SortCoordinates(
                    [a._BoundingBoxP1, a._BoundingBoxP2])
                bbMin = BB[0]
                bbMax = BB[1]
                direction = a._Direction
                if direction == "West":
                    shortListedLines = []
                    SupportLSW = []
                    for ls in supportLinesegments:
                        if bbMin.x + 3 > ls.startPoint.x and fabs(
                                ls.endPoint.x - ls.startPoint.x) < 3 and fabs(
                                    ls.endPoint.y - ls.startPoint.y) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:  #<7
                            SupportLSW.append(ls)
                    SupportL.append(SupportLSW)
                elif direction == "East":
                    shortListedLines = []
                    SupportLSE = []
                    for ls in supportLinesegments:
                        if bbMax.x - 3 < ls.startPoint.x and fabs(
                                ls.endPoint.x - ls.startPoint.x) < 3 and fabs(
                                    ls.endPoint.y - ls.startPoint.y) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSE.append(ls)
                    SupportL.append(SupportLSE)
                elif direction == "North":
                    shortListedLines = []
                    SupportLSN = []
                    for ls in supportLinesegments:
                        if bbMin.y + 3 > ls.startPoint.y and fabs(
                                ls.endPoint.y - ls.startPoint.y) < 3 and fabs(
                                    ls.endPoint.x - ls.startPoint.x) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMin)
                        projectedDistance = bbMin.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSN.append(ls)
                    SupportL.append(SupportLSN)
                elif direction == "South":
                    SupportLSS = []
                    shortListedLines = []
                    for ls in supportLinesegments:
                        if bbMax.y - 3 < ls.startPoint.y and fabs(
                                ls.endPoint.y - ls.startPoint.y) < 3 and fabs(
                                    ls.endPoint.x - ls.startPoint.x) > 5:
                            shortListedLines.append(ls)
                    for ls in shortListedLines:
                        projectedPt = MathUtils.ProjectToLine2(
                            ls.startPoint, ls.endPoint, bbMax)
                        projectedDistance = bbMax.DistanceTo(projectedPt)
                        if projectedDistance < 8:
                            SupportLSS.append(ls)
                    SupportL.append(SupportLSS)
            d._SupportLines = SupportL
        Feature_Manager._DetectedLine = entityLinesegments