def normalize_characters_character_height(license_plate,
                                          image_original,
                                          character_height,
                                          m,
                                          c,
                                          recrop=True):
    '''
    Normalize height of characters.
    '''
    origin = Point(0, 0)
    end = Point(license_plate.image.width - 1, license_plate.image.height - 1)
    changed_image = False

    for index in range(len(license_plate.subrects)):
        new_y = int(license_plate.subrects[index].x * m + c)

        if abs(new_y - license_plate.subrects[index].y) > 5:
            license_plate.subrects[index].y = new_y

            if license_plate.subrects[index].y < origin.y:
                origin.y = license_plate.subrects[index].y
                changed_image = True

        license_plate.subrects[index].h = character_height

        if license_plate.subrects[index].h + license_plate.subrects[
                index].y - 1 > end.y:
            end.y = license_plate.subrects[index].y + license_plate.subrects[
                index].h - 1 + 50
            changed_image = True

    if changed_image and recrop:
        # Recrop image and compute rectangles again.
        license_plate = recrop_license_plate_image(license_plate, origin, end,
                                                   image_original)
        character_validator = models.character_validator.CharacterValidator()
        backup_image = Image(image=license_plate.image.data)
        image = pre_process_license_plate_image(license_plate.image,
                                                adaptative=False)
        license_plate.subrects = image.compute_rectangles_for_characters()
        character_validator.remove_wrong_characters(license_plate)

        # If not enough characters detected, binarize image using adaptative method.
        if len(license_plate.subrects) < 2:
            image = pre_process_license_plate_image(backup_image, True)
            license_plate.subrects = image.compute_rectangles_for_characters()
            character_validator.remove_wrong_characters(license_plate)

        validator = models.character_validator.CharacterValidator()
        m, c = validator.get_least_squares(license_plate)
        license_plate, m, c = normalize_characters_character_height(
            license_plate, image_original, character_height, m, c, False)

    return license_plate, m, c
    def _recrop_image(self, license_plate, image_original, width, height, m, c,
                      x_interval):
        '''
        Recrop image if necessary.
        '''

        changed_image = False
        origin = Point(0, 0)
        end = Point(license_plate.image.width - 1,
                    license_plate.image.height - 1)

        for character in license_plate.subrects:
            if character.y < origin.y:
                origin.y = character.y
                changed_image = True

            if character.x < origin.x:
                origin.x = character.x
                changed_image = True

            if character.h + character.y - 1 > end.y:
                end.y = character.y + character.h
                changed_image = True

            if character.w + character.x - 1 > end.x:
                end.x = character.x + character.w
                changed_image = True

        if changed_image:
            # Recrop image and compute rectangles again.
            scale = license_plate.w * 1.0 / license_plate.image.width
            license_plate = recrop_license_plate_image(license_plate, origin,
                                                       end, image_original)

            old_image = Image(image=license_plate.image.data)
            image_1 = pre_process_license_plate_image(license_plate.image,
                                                      True)
            image_2 = pre_process_license_plate_image(old_image, False)

            license_plate.image = image_1
            color_mean_1 = self._calculate_mean_caracter_values(license_plate)
            license_plate.image = image_2
            color_mean_2 = self._calculate_mean_caracter_values(license_plate)

            if color_mean_1 < 10 and color_mean_2 > 10:
                color_mean_2 = 119
            elif color_mean_1 > 10 and color_mean_2 < 10:
                color_mean_1 = 119

            if color_mean_1 < 255 and color_mean_2 > 255:
                color_mean_1 = 119
            elif color_mean_1 > 255 and color_mean_2 < 255:
                color_mean_2 = 119

            t1 = 70
            t2 = 180

            color_delta_1 = min(abs(color_mean_1 - t1), abs(color_mean_1 - t2))
            color_delta_2 = min(abs(color_mean_2 - t1), abs(color_mean_2 - t2))

            if color_delta_1 < color_delta_2:
                license_plate.image = image_1
            else:
                license_plate.image = image_2

            old_list = list(license_plate.subrects)

            if len(old_list) > 0:
                height = old_list[0].h
                width = old_list[0].w

                if len(old_list) > 2:
                    license_plate.subrects = license_plate.subrects[0:3]
                    m, c = self.get_least_squares(license_plate)
                else:
                    m, c = self.get_least_squares(license_plate)
            else:
                height, width = self._get_h_w(license_plate)
                m, c = self.get_least_squares(license_plate)

            x_interval = int(x_interval * scale + 0.5)
            license_plate.subrects = old_list

        #license_plate.image.plot_rectangles(license_plate.subrects)

        return license_plate, width, height, m, c, x_interval
Beispiel #3
0
def crop_image(image, quadrilateral, width_dest, height_dest):
    '''
    Crop image using parameters of quadrilateral.
    '''
    # Find crop parameters.
    logger = Logger()
    logger.log(Logger.INFO, "Finding crop parameters.")
    origin = Point(image.width, image.height)
    end = Point(0, 0)

    for point in quadrilateral.points:
        if point.x > end.x:
            end.x = point.x

        if point.y > end.y:
            end.y = point.y

        if point.x < origin.x:
            origin.x = point.x

        if point.y < origin.y:
            origin.y = point.y

    # Adjust dimensions to height be 20% of width.
    scale = height_dest * 1.0 / width_dest
    height = end.y - origin.y
    width = end.x - origin.x

    # Check if we will change height or width.
    if width * scale < height:
        # We need to increase width.
        increment = height * 2.5 - width
        end.x += increment / 2

        if end.x >= image.width:
            increment -= image.width - 1 - end.x
            end.x = image.width - 1
        else:
            increment /= 2

        origin.x -= increment

        if origin.x < 0:
            increment = -origin.x
            origin.x = 0
            end.x += increment

            if end.x >= image.width:
                end.x = image.width - 1

        logger.log(Logger.INFO,
                   "Increasing width in " + str(end.x - origin.x - width))
    else:
        # We need to increase height.
        increment = width * scale - height
        end.y += increment / 2

        if end.y >= image.height:
            increment -= image.height - 1 - end.y
            end.y = image.height - 1
        else:
            increment /= 2

        origin.y -= increment

        if origin.y < 0:
            increment = -origin.y
            origin.y = 0
            end.y += increment

            if end.y >= image.height:
                end.y = image.height - 1

        logger.log(Logger.INFO,
                   "Increasing height in " + str(end.y - origin.y - height))

    # Crop image using points from configuration data.
    cropped = image.crop(Point(origin.x, origin.y), Point(end.x, end.y))

    # Resize image.
    resized = cropped.resize(width_dest, height_dest)

    return resized