Пример #1
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()

    listOfMatchingChars.sort(key=lambda matchingChar: matchingChar.intCenterX)

    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped

    return possiblePlate
Пример #2
0
def extractPlate(img_original, list_of_matching_chars):
    possible_plate = PossiblePlate.PossiblePlate()  # this will be the return value

    list_of_matching_chars.sort(key=lambda c: c.intCenterX)

    # calculate the center point of the plate
    fltPlateCenterX = (list_of_matching_chars[0].intCenterX + list_of_matching_chars[
        len(list_of_matching_chars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (list_of_matching_chars[0].intCenterY + list_of_matching_chars[
        len(list_of_matching_chars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # calculate plate width and height
    intPlateWidth = int((list_of_matching_chars[len(list_of_matching_chars) - 1].intBoundingRectX + list_of_matching_chars[
        len(list_of_matching_chars) - 1].intBoundingRectWidth - list_of_matching_chars[
                             0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in list_of_matching_chars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(list_of_matching_chars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calculate correction angle of plate region
    fltOpposite = list_of_matching_chars[len(list_of_matching_chars) - 1].intCenterY - list_of_matching_chars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(list_of_matching_chars[0],
                                                     list_of_matching_chars[len(list_of_matching_chars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possible_plate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                               (intPlateWidth, intPlateHeight),
                                               fltCorrectionAngleInDeg)

    # final steps are to perform the actual rotation

    # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = img_original.shape  # unpack original image width and height

    imgRotated = cv2.warpAffine(img_original, rotationMatrix, (width, height))  # rotate the entire image

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))

    possible_plate.imgPlate = imgCropped

    return possible_plate
Пример #3
0
def extractPlate(listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate(
    )  # this will be the return value

    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # sort chars from left to right based on x position

    # calculate the center point of the plate
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # calculate plate width and height
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    return possiblePlate
Пример #4
0
def extractPlate(input_image, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()

    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # sort chars from left to right based on x position

    plate_center_X = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    plate_center_Y = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0
    plate_center = plate_center_X, plate_center_Y

    plate_width = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) * 1.3)

    total_char_heights = 0
    for matchingChar in listOfMatchingChars:
        total_char_heights = total_char_heights + matchingChar.intBoundingRectHeight

    average_char_height = total_char_heights / len(listOfMatchingChars)
    plate_height = int(average_char_height * 1.5)

    opposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    hypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    correction_angle_radian = math.asin(opposite / hypotenuse)
    correction_angle_degree = correction_angle_radian * (180.0 / math.pi)
    possiblePlate.rrLocationOfPlateInScene = (tuple(plate_center),
                                              (plate_width, plate_height),
                                              correction_angle_degree)

    rotationMatrix = cv2.getRotationMatrix2D(tuple(plate_center),
                                             correction_angle_degree, 1.0)
    height, width, Channels = input_image.shape

    rotated_image = cv2.warpAffine(input_image, rotationMatrix,
                                   (width, height))
    cropped_image = cv2.getRectSubPix(rotated_image,
                                      (plate_width, plate_height),
                                      tuple(plate_center))
    possiblePlate.imgPlate = cropped_image

    return possiblePlate
Пример #5
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()
    # sort chars from left to right based on x position
    listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX)

    # calculate the center point of the plate
    fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (listOfMatchingChars[0].intCenterY + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0
    # This is the probable central point of this plate.
    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # calculate plate width and height
    intPlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX + listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth - listOfMatchingChars[0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)
    # Here we calculate the probable width of this plate.
    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # Here we calculate the probale height of this particular plate.
    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)
    # We include the padding factor.
    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0], listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg )

    # final steps are to perform the actual rotation

    # get the rotation matrix for our calculated correction angle
    # The first point is the point of rotation or center,theta and scaling factor
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)

    # unpack original image width and height
    height, width, numChannels = imgOriginal.shape
    # rotate the entire image
    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))
    # We extract the probable plate from the Original image
    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))
    # copy the cropped plate image into the applicable member variable of the possible plate
    possiblePlate.imgPlate = imgCropped

    return possiblePlate
Пример #6
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()           # this will be the return value

    listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX)        # sort chars from left to right based on x position

            # calculate the center point of the plate
    fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (listOfMatchingChars[0].intCenterY + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

            # calculate plate width and height
    intPlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX + listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth - listOfMatchingChars[0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

            # calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0], listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

            # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg )

            # final steps are to perform the actual rotation

            # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape      # unpack original image width and height

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))       # rotate the entire image

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped         # copy the cropped plate image into the applicable member variable of the possible plate

    return possiblePlate
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()           
    # sort chars from left to right based on x position
    listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.CenterX)        

    # calculate the center point of the plate
    PlateCenterX = (listOfMatchingChars[0].CenterX + listOfMatchingChars[len(listOfMatchingChars) - 1].CenterX) / 2.0
    PlateCenterY = (listOfMatchingChars[0].CenterY + listOfMatchingChars[len(listOfMatchingChars) - 1].CenterY) / 2.0

    ptPlateCenter = PlateCenterX, PlateCenterY

    # calculate plate width and height
    PlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].BoundingRectX + listOfMatchingChars[len(listOfMatchingChars) - 1].BoundingRectWidth - listOfMatchingChars[0].BoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)

    TotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        TotalOfCharHeights = TotalOfCharHeights + matchingChar.BoundingRectHeight
    # end for

    AverageCharHeight = TotalOfCharHeights / len(listOfMatchingChars)

    PlateHeight = int(AverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calculate correction angle of plate region
    Opposite = listOfMatchingChars[len(listOfMatchingChars) - 1].CenterY - listOfMatchingChars[0].CenterY
    Hypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0], listOfMatchingChars[len(listOfMatchingChars) - 1])
    CorrectionAngleInRad = math.asin(Opposite / Hypotenuse)
    CorrectionAngleInDeg = CorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (PlateWidth, PlateHeight), CorrectionAngleInDeg )

    # final steps are to perform the actual rotation

    # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), CorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape     

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))      

    imgCropped = cv2.getRectSubPix(imgRotated, (PlateWidth, PlateHeight), tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped         
    return possiblePlate
Пример #8
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()          

    listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX)      

    fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (listOfMatchingChars[0].intCenterY + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    intPlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX + listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth - listOfMatchingChars[0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
 

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    fltOpposite = listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0], listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg )

        
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape       

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))      

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped          

    return possiblePlate
Пример #9
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()    # Dönüş değeri olacak

    listOfMatchingChars.sort(key = lambda matchingChar: matchingChar.intCenterX)  # Karakterleri x konumuna göre sola veya sağa sıralayın

    # Plakanın merkez noktasını hesaplar
    fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (listOfMatchingChars[0].intCenterY + listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY
    # Plaka genişliği ve yüksekliğini hesaplar
    intPlateWidth = int((listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX + listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth - listOfMatchingChars[0].intBoundingRectX) * PLATE_WIDTH_PADDING_FACTOR)
    intTotalOfCharHeights = 0
    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)
    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)
    # Plaka bölgesinin düzeltme açısını hesaplar
    fltOpposite = listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(listOfMatchingChars[0], listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # Paket bölgesinin merkez noktası, genişliği, yüksekliği ve plakanın döndürülmüş dikme elemanı değişkenine düzeltme açısı
    possiblePlate.rrLocationOfPlateInScene = ( tuple(ptPlateCenter), (intPlateWidth, intPlateHeight), fltCorrectionAngleInDeg )

    # Son adımlar asıl dönüşü gerçekleştirmektir
    # Hesaplanan düzeltme açımız için rotasyon matrisini alın
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter), fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape      # Orjinal görüntü genişliğini ve yüksekliğini açma
    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))  # Tüm resmi döndür
    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight), tuple(ptPlateCenter))
    possiblePlate.imgPlate = imgCropped         # Kırpılmış plaka görüntüsünü olası plakanın uygulanabilir üye değişkenine kopyala
    return possiblePlate
Пример #10
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate(
    )  # this will be the return value

    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # 람다는 한줄로 정의하는 함수다. sort chars from left to right based on x position

    ##### [plate 중심좌표계산] calculate the center point of the plate
    fltPlateCenterX = (listOfMatchingChars[0].intCenterX + listOfMatchingChars[
        len(listOfMatchingChars) - 1].intCenterX) / 2.0  #처음거랑 마지막거 센터평균
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    ###### [plate높이와 폭계산] calculate plate width and height
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)
    ##### end [plate높이와 폭계산]

    ######[각도 계산. y차와 거리 이용] calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    #####[위치기록.Box2D형식인듯] pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # final steps are to perform the actual rotation

    #####[회전 및 crop]        # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape  # unpack original image width and height

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix,
                                (width, height))  # rotate the entire image

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped  # copy the cropped plate image into the applicable member variable of the possible plate

    return possiblePlate
Пример #11
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate(
    )  # this will be the return value ##回傳PossiblePlate.py内的物件PossiblePlate()

    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # sort chars from left to right based on x position #以listOfMatchingChars(裏面的物件)的屬性intCenterX去做sort

    # calculate the center point of the plate
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY  #

    # calculate plate width and height #最後一個點-第一個點=等於寬度*1.3
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    #listOfMatchingChar裏面的height 全部相加
    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(
        listOfMatchingChars)  #平均height

    intPlateHeight = int(fltAverageCharHeight *
                         PLATE_HEIGHT_PADDING_FACTOR)  #平均height*1.5

    # calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[len(
        listOfMatchingChars
    ) - 1].intCenterY - listOfMatchingChars[
        0].intCenterY  #listOfMatchingChars的CenterY最後一個-listOfMatchingChars的CenterY第一個  ##找到寬度==高度
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) -
                            1])  #第一個值和最後一個去做彼氏定理 找到char之間的距離
    fltCorrectionAngleInRad = math.asin(
        fltOpposite / fltHypotenuse)  #math.asin=只接受-1到1之間的數字 超出範圍就會回傳NAN##
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi
                                                         )  ##找角度

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # final steps are to perform the actual rotation

    # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(
        tuple(ptPlateCenter), fltCorrectionAngleInDeg,
        1.0)  ##(旋轉中心,旋轉角度,縮放比例 ) ##更正字體的角度

    height, width, numChannels = imgOriginal.shape  # unpack original image width and height

    imgRotated = cv2.warpAffine(
        imgOriginal, rotationMatrix,
        (width, height))  # rotate the entire image  ##圖像平移(圖像,變換矩陣,變換后的大小)

    imgCropped = cv2.getRectSubPix(
        imgRotated, (intPlateWidth, intPlateHeight),
        tuple(ptPlateCenter))  ##(imgRotated圖像,(截取圖像高和寬),中心)###回傳圖片

    possiblePlate.imgPlate = imgCropped  # copy the cropped plate image into the applicable member variable of the possible plate ###給值

    return possiblePlate
Пример #12
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate()

    listOfMatchingChars.sort(key=lambda matchingChar: matchingChar.intCenterX
                             )  # sắp xếp kí tự từ trái sang phải từ X

    # Tính điểm trung tâm của tấm
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # tính chiều rộng cua tấm
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # tính góc của tấm
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape  # giải nén chiều rộng và chiều cao ảnh gốc

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix,
                                (width, height))  # xoay hình ảnh

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped

    return possiblePlate
Пример #13
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate(
    )  #possiblePlate is object of class PossiblePlate
    """ object provides us huge flexibility like we can use multile attributes by just one object"""

    # this will be the return value
    """ NOTE->matchingChar.intCenterX """
    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # sort chars from left to right based on x position

    # calculate the center point of the plate
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # calculate plate width and height
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calculate correction angle of plate region
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # final steps are to perform the actual rotation

    # get the rotation matrix for our calculated correction angle
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape  # unpack original image width and height

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix,
                                (width, height))  # rotate the entire image

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))  #cropping
    #possiblePlate is object
    possiblePlate.imgPlate = imgCropped  # copy the cropped plate image into the applicable member variable of the possible plate

    return possiblePlate
def extractPlate(imgOriginal, listOfMatchingChars):
    # Valor retornado
    possiblePlate = PossiblePlate.PossiblePlate()

    # Ordena os caracteres da esquerda pra direita baseado na posição X
    listOfMatchingChars.sort(key=lambda matchingChar: matchingChar.intCenterX)

    # Calcula o centro da placa
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    # Calcula a altura e largura da placa
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # Calcula o angulo correto da reguao da placa
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # Reune o ponto central da regiao da placa, largura, altura e o angulo de rotacao em um retangulo rotacionado membro da variavel da placa
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # Executa a rotacao atual

    # Recupera a matriz de rotacao para o angulo encontrado
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    # Recupera os valores originais da imagem
    height, width, numChannels = imgOriginal.shape

    # Rotaciona a imagem inteira
    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix, (width, height))

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))

    # Guarda a imagem cortada da placa
    possiblePlate.imgPlate = imgCropped

    return possiblePlate


# end function
Пример #15
0
def extractPlate(imgOriginal, listOfMatchingChars):
    possiblePlate = PossiblePlate.PossiblePlate(
    )  # este será el valor de retorno
    listOfMatchingChars.sort(
        key=lambda matchingChar: matchingChar.intCenterX
    )  # ordenar caracteres de izquierda a derecha según la posición x

    # calcular el punto central de la placa
    fltPlateCenterX = (
        listOfMatchingChars[0].intCenterX +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterX) / 2.0
    fltPlateCenterY = (
        listOfMatchingChars[0].intCenterY +
        listOfMatchingChars[len(listOfMatchingChars) - 1].intCenterY) / 2.0

    ptPlateCenter = fltPlateCenterX, fltPlateCenterY

    #calcular el ancho y la altura de la placa
    intPlateWidth = int(
        (listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectX +
         listOfMatchingChars[len(listOfMatchingChars) - 1].intBoundingRectWidth
         - listOfMatchingChars[0].intBoundingRectX) *
        PLATE_WIDTH_PADDING_FACTOR)

    intTotalOfCharHeights = 0

    for matchingChar in listOfMatchingChars:
        intTotalOfCharHeights = intTotalOfCharHeights + matchingChar.intBoundingRectHeight
    # end for

    fltAverageCharHeight = intTotalOfCharHeights / len(listOfMatchingChars)

    intPlateHeight = int(fltAverageCharHeight * PLATE_HEIGHT_PADDING_FACTOR)

    # calcular el ángulo de corrección de la región de la placa
    fltOpposite = listOfMatchingChars[
        len(listOfMatchingChars) -
        1].intCenterY - listOfMatchingChars[0].intCenterY
    fltHypotenuse = DetectChars.distanceBetweenChars(
        listOfMatchingChars[0],
        listOfMatchingChars[len(listOfMatchingChars) - 1])
    fltCorrectionAngleInRad = math.asin(fltOpposite / fltHypotenuse)
    fltCorrectionAngleInDeg = fltCorrectionAngleInRad * (180.0 / math.pi)

    # empaquete el punto central, el ancho y la altura de la región de la placa, y el ángulo de corrección en la variable del miembro correcto rotado de la placa
    possiblePlate.rrLocationOfPlateInScene = (tuple(ptPlateCenter),
                                              (intPlateWidth, intPlateHeight),
                                              fltCorrectionAngleInDeg)

    # los pasos finales son realizar la rotación real

    # obtener la matriz de rotación para nuestro ángulo de corrección calculado
    rotationMatrix = cv2.getRotationMatrix2D(tuple(ptPlateCenter),
                                             fltCorrectionAngleInDeg, 1.0)

    height, width, numChannels = imgOriginal.shape  # descomprimir ancho y alto de la imagen original

    imgRotated = cv2.warpAffine(imgOriginal, rotationMatrix,
                                (width, height))  # rotar toda la imagen

    imgCropped = cv2.getRectSubPix(imgRotated, (intPlateWidth, intPlateHeight),
                                   tuple(ptPlateCenter))

    possiblePlate.imgPlate = imgCropped  # copie la imagen de la placa recortada en la variable miembro aplicable de la placa posible

    return possiblePlate