def extract_plate(img_original, list_of_matching_chars) -> PossiblePlate: possible_plate = PossiblePlate.PossiblePlate() # Ordenamos los chars de izquierda a derecha en base a la posicion X list_of_matching_chars.sort(key=lambda matching_char: matching_char.intCenterX) # Calculamos el punto central de la patente flt_plate_center_x = (list_of_matching_chars[0].intCenterX + list_of_matching_chars[ len(list_of_matching_chars) - 1].intCenterX) / 2.0 flt_plate_center_y = (list_of_matching_chars[0].intCenterY + list_of_matching_chars[ len(list_of_matching_chars) - 1].intCenterY) / 2.0 pt_plate_center = flt_plate_center_x, flt_plate_center_y # calculate plate width and height int_plate_width = 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) int_total_of_char_heights = 0 for matching_char in list_of_matching_chars: int_total_of_char_heights = int_total_of_char_heights + matching_char.intBoundingRectHeight flt_average_char_height = int_total_of_char_heights / len(list_of_matching_chars) int_plate_height = int(flt_average_char_height * PLATE_HEIGHT_PADDING_FACTOR) # calculate correction angle of plate region flt_opposite = list_of_matching_chars[len(list_of_matching_chars) - 1].intCenterY - list_of_matching_chars[ 0].intCenterY flt_hypotenuse = DetectChars.distance_between_chars(list_of_matching_chars[0], list_of_matching_chars[len(list_of_matching_chars) - 1]) flt_correction_angle_in_rad = math.asin(flt_opposite / flt_hypotenuse) flt_correction_angle_in_deg = flt_correction_angle_in_rad * (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(pt_plate_center), (int_plate_width, int_plate_height), flt_correction_angle_in_deg) # final steps are to perform the actual rotation # get the rotation matrix for our calculated correction angle rotation_matrix = cv2.getRotationMatrix2D(tuple(pt_plate_center), flt_correction_angle_in_deg, 1.0) height, width, num_channels = img_original.shape # unpack original image width and height img_rotated = cv2.warpAffine(img_original, rotation_matrix, (width, height)) # rotate the entire image img_cropped = cv2.getRectSubPix(img_rotated, (int_plate_width, int_plate_height), tuple(pt_plate_center)) possible_plate.imgPlate = img_cropped return possible_plate
def extract_plate(img_original, chars): possible_plate = PossiblePlate.PossiblePlate( ) # this will be the return value chars.sort(key=lambda x: x.center_x ) # sort chars from left to right based on x position # calculate the center point of the plate plate_center_x = (chars[0].center_x + chars[-1].center_x) / 2.0 plate_center_y = (chars[0].center_y + chars[-1].center_y) / 2.0 # This is the probable centeral point of this plate. plate_center = plate_center_x, plate_center_y # calculate plate width and height plate_width = int( (chars[-1].pos_x + chars[-1].width - chars[0].pos_x) * 1.3) # Here we calculate the probable width of this plate. height_sum = 0 for matchingChar in chars: height_sum += matchingChar.height average_char_height = height_sum / len(chars) plate_height = int(average_char_height * 1.5) # We include the padding factor. # calculate correction angle of plate region opposite = chars[-1].center_y - chars[0].center_y hypotenuse = DetectChars.distance_between_chars(chars[0], chars[-1]) correction_angle_in_rad = math.asin(opposite / hypotenuse) correction_angle_deg = correction_angle_in_rad * (180.0 / math.pi) # pack plate region center point, width and height, and correction angle into rotated rect member variable of plate possible_plate.location_plate_on_img = (tuple(plate_center), (plate_width, plate_height), correction_angle_deg) # final steps are to perform the actual rotation # get the rotation matrix for our calculated correction angle rotation_matrix = cv2.getRotationMatrix2D(tuple(plate_center), correction_angle_deg, 1.0) height, width, _ = img_original.shape # unpack original image width and height img_rotated = cv2.warpAffine(img_original, rotation_matrix, (width, height)) # rotate the entire image # imgTmp = cv2.getRectSubPix(img_rotated, (width, plate_height), tuple(plate_center)) img_cropped = cv2.getRectSubPix(img_rotated, (plate_width, plate_height), tuple(plate_center)) possible_plate.img_plate = img_cropped return possible_plate
def extract_plate(img_original, list_of_matching_chars): possible_plate = PossiblePlate.PossiblePlate() list_of_matching_chars.sort( key=lambda matching_char: matching_char.intCenterX) plate_center_x = (list_of_matching_chars[0].intCenterX + list_of_matching_chars[len(list_of_matching_chars) - 1].intCenterX) / 2.0 plate_center_y = (list_of_matching_chars[0].intCenterY + list_of_matching_chars[len(list_of_matching_chars) - 1].intCenterY) / 2.0 plate_center = plate_center_x, plate_center_y plate_width = 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) total_of_char_heights = 0 for matching_char in list_of_matching_chars: total_of_char_heights = total_of_char_heights + matching_char.intBoundingRectHeight average_char_height = total_of_char_heights / len(list_of_matching_chars) plate_height = int(average_char_height * PLATE_HEIGHT_PADDING_FACTOR) opposite = list_of_matching_chars[ len(list_of_matching_chars) - 1].intCenterY - list_of_matching_chars[0].intCenterY hypotenuse = DetectChars.distance_between_chars( list_of_matching_chars[0], list_of_matching_chars[len(list_of_matching_chars) - 1]) correction_angle_in_rad = math.asin(opposite / hypotenuse) correction_angle_in_deg = correction_angle_in_rad * (180.0 / math.pi) possible_plate.rrLocationOfPlateInScene = (tuple(plate_center), (plate_width, plate_height), correction_angle_in_deg) rotation_matrix = cv2.getRotationMatrix2D(tuple(plate_center), correction_angle_in_deg, 1.0) height, width, num_of_channels = img_original.shape img_rotated = cv2.warpAffine(img_original, rotation_matrix, (width, height)) img_cropped = cv2.getRectSubPix(img_rotated, (plate_width, plate_height), tuple(plate_center)) possible_plate.imgPlate = img_cropped return possible_plate
def rotate(img_orig, list_of_matching_chars): """ rotate image to recive straight line of chrs """ list_of_matching_chars.sort(key=lambda char: char.center_x) # calculate the center point of the plate plate_center_x = ( list_of_matching_chars[0].center_x + list_of_matching_chars[len(list_of_matching_chars) - 1].center_x) / 2.0 plate_center_y = ( list_of_matching_chars[0].center_y + list_of_matching_chars[len(list_of_matching_chars) - 1].center_y) / 2.0 # This is the probable centeral point of this plate. pt_plate_center = plate_center_x, plate_center_y # calculate correction angle of plate region flt_opposite = list_of_matching_chars[ -1].center_y - list_of_matching_chars[0].center_y flt_hypotenuse = DetectChars.distance_between_chars( list_of_matching_chars[0], list_of_matching_chars[-1]) try: flt_correction_angle_in_rad = math.asin(flt_opposite / flt_hypotenuse) except ZeroDivisionError: flt_correction_angle_in_rad = 0 flt_correction_angle_in_deg = flt_correction_angle_in_rad * (180.0 / math.pi) # final steps are to perform the actual rotation # get the rotation matrix for our calculated correction angle # The first poin tis the point of rotaion or center,theta and scaling factor rotation_matrix = cv2.getRotationMatrix2D(tuple(pt_plate_center), flt_correction_angle_in_deg, 1.0) height, width = img_orig.shape # unpack original image width and height img_rotated = cv2.warpAffine(img_orig, rotation_matrix, (width, height)) # rotate the entire image return img_rotated