Core photo automatic correction and cropping method based on YOLOv8-seg and perspective transformation

By combining the YOLOv8-seg model and perspective transformation, automatic correction and cropping of core images were achieved, solving the problems of excessive manual intervention and low efficiency. This approach adapts to core image processing in complex scenarios and enhances the automation capabilities of geological exploration.

CN121707887BActive Publication Date: 2026-05-05ZIJIN MINING GRP SOUTHWEST GEOLOGICAL EXPLORATION CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
ZIJIN MINING GRP SOUTHWEST GEOLOGICAL EXPLORATION CO LTD
Filing Date
2026-02-14
Publication Date
2026-05-05

AI Technical Summary

Technical Problem

Existing technologies for processing core images suffer from problems such as excessive manual intervention, low efficiency, and poor adaptability, making it difficult to achieve fully automated batch processing and failing to meet the needs of large-scale geological exploration.

Method used

An automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation is adopted. Core box images are captured by digital devices, and corner point prediction and perspective transformation are performed using the YOLOv8-seg model to achieve automatic correction and cropping of the core box. A graphical user interface is built with the Tkinter library to support batch image processing.

Benefits of technology

It achieves end-to-end fully automated correction and cropping of core images, eliminating the need for field marking preprocessing and manual corner positioning. It adapts to core boxes with different scaling ratios, rotation angles, and distortion levels, significantly improving the work efficiency of geologists.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121707887B_ABST
    Figure CN121707887B_ABST
Patent Text Reader

Abstract

This invention provides an automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation, belonging to the field of image processing technology. The method includes: S1: collecting core box images; S2: polygon point sequence annotation; S3: obtaining the optimal segmentation model; S4: cropping. This invention addresses the problems of excessive manual intervention, low efficiency, and poor adaptability in existing core image processing methods by providing a fully automated end-to-end solution that requires no preprocessing. It enables rapid and stable correction and cropping of core images in complex scenarios, reducing the mechanical workload of geologists and allowing them to focus on core exploration work, thus meeting the practical application needs of large-scale geological exploration projects.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of image processing technology, and in particular to an automatic correction and cropping method for core photographs based on YOLOv8-seg and perspective transformation. Background Technology

[0002] In geological exploration, according to relevant regulations, core samples must be digitally photographed and archived box by box, and each image must contain only one set of horizontally undistorted core samples. However, in actual shooting, core samples are often densely packed in the core storage area, making it easy for adjacent boxes to be included. In the field, uneven terrain can easily cause problems such as core sample rotation, bending distortion, etc. Traditional processing methods rely on manual image processing software to correct and crop each image, which is a cumbersome process. Existing related patents either require drawing matching marks on the core samples for preprocessing or manually locating the corner points of the core samples, neither of which can achieve fully automated batch processing and cannot meet the needs of large-scale core image processing. Summary of the Invention

[0003] This invention provides an automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation. Addressing the problems of excessive manual intervention, low efficiency, and poor adaptability in existing core image processing, it offers a fully automated end-to-end solution that requires no preprocessing. This enables rapid and stable correction and cropping of core images in complex scenarios, reducing the mechanical labor burden on geologists and allowing them to focus on core exploration work, thus meeting the practical application needs of large-scale geological exploration projects.

[0004] To achieve the above objectives, the present invention adopts the following technical solution:

[0005] An automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation includes:

[0006] S1: Use digital equipment to photograph the core box and collect high-resolution images of the core box, including adjacent core boxes, rotation or distortion, without the need for marking and preprocessing of the core box;

[0007] S2: Perform polygon point sequence annotation on the high-definition core box images collected in S1. During the annotation process, automatically correct the EXIF ​​rotation information of the images. Divide the annotated images into training and validation sets, and export the dataset and configuration file adapted to the YOLOv8-seg model.

[0008] S3: Based on the YOLOv8-seg model, load the dataset and configuration file exported by S2, automatically select CPU or GPU devices according to the hardware environment, set training parameters and enable data augmentation strategies, and verify the model performance through the validation set after training to obtain the optimal segmentation model.

[0009] S4: Input the core box image to be processed into the optimal segmentation model obtained in S3 to obtain the core box segmentation mask. Extract the minimum circumscribed quadrilateral and the coordinates of the four corner points from the mask. Use a vector geometry-based sorting algorithm to determine the clockwise order of the corner points. Based on the sorted corner point coordinates, use perspective transformation to map the irregular quadrilateral region into a regular rectangle. Crop the corrected image and retain the region containing only one set of horizontal, distortion-free core boxes and save it.

[0010] In this manual, the automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation also includes S5: a graphical user interface is built using the Tkinter library, integrating the functional modules of S2 to S4, and the entire method is packaged into an executable program using Anaconda, supporting batch image import, parameter configuration, and processing progress monitoring.

[0011] In this specification, the high-definition core box images described in S1 support JPG, JPEG, PNG, BMP, TIFF formats and corresponding uppercase formats. There is no need to avoid adjacent core boxes when shooting, and the core box is allowed to be in a non-horizontal or curved distortion state.

[0012] In this manual, when annotating in S2, clicking the four corner points of the core box closes to form a polygon. The annotation coordinates are converted into the original image coordinates by calculating the scaling ratio between the displayed image and the original image, subtracting the canvas offset, and the converted coordinates are restricted to the effective range of the image. It supports persistent saving and repeated editing of annotated data.

[0013] In this specification, the ratio of training set to validation set in S2 is 80%:20%. The exported dataset includes image files and corresponding label files. The label files normalize the coordinates of polygon annotation points to relative coordinates within the range of [0,1]. The configuration file contains the dataset path, number of categories, and category name information, which is adapted to the training requirements of the YOLOv8-seg model.

[0014] In this specification, the data augmentation strategies described in S3 include perspective transformation, rotation within a 10° range, 0.5x scaling, and color space transformation. The training parameters include the default number of training rounds of 50, image size of 640, batch size of 16, and an early stop mechanism. When the validation metric fails to improve for 30 consecutive rounds, training is automatically stopped, and intermediate checkpoints are saved every 5 rounds.

[0015] In this specification, the sorting algorithm based on vector geometry described in S4 is as follows: First, calculate the average coordinates of all corner points of the quadrilateral extracted by S4 as the centroid. Then, calculate the vector from each corner point to the centroid. After sorting by vector angle, find the top left corner point with the smallest x+y value. Finally, obtain the clockwise corner point order of top left, top right, bottom right, and bottom left by cyclic shifting.

[0016] In this manual, the perspective transformation described in S4 uses the INTER_LINEAR interpolation method to automatically calculate the output size and add a 10% margin. The blank area after perspective transformation is filled with black. The output size does not exceed twice the size of the original image. The cropped image is labeled according to the original image naming rules and then saved to the specified folder.

[0017] In this manual, S4 processes core box images in batches, using a fixed batch size of 50 images per group. After each batch is processed, memory is automatically cleared to avoid memory overflow. Memory usage is monitored in real time during processing, and the processing results are automatically associated with the original image paths for traceability and comparison.

[0018] In this specification, the graphical user interface described in S5 supports the selection of the optimal segmentation model file obtained in S3, import of the image directory to be processed, and setting of confidence parameters. It also features real-time display of processing logs, with logs color-coded to distinguish between errors, successes, and progress. The logs also display processing progress, total time consumed, and memory usage.

[0019] In summary, this invention offers at least the following advantages: it enables fully automated end-to-end correction and cropping of core images, eliminating the need for pre-processing with field markings on the core box and manual corner positioning. It adapts to core boxes with varying scaling ratios, rotation angles, and distortion levels, effectively solving the problem of repetitive mechanical labor in traditional processing and significantly improving the work efficiency of geologists. Furthermore, it optimizes program performance and stability, features a user-friendly graphical interface, lowers the barrier to entry, and facilitates widespread application in geological exploration scenarios. Attached Figure Description

[0020] Figure 1 This is a schematic diagram of the method of the present invention.

[0021] Figure 2 This is a schematic diagram of a core box four-corner sorting algorithm based on vector geometry. Detailed Implementation

[0022] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.

[0023] like Figure 1 As shown, this invention uses Python, combined with the YOLOv8-seg instance segmentation model, OpenCV image processing library, and Tkinter graphical interface library, to build a complete industrial-grade solution. The core process includes: collecting high-resolution images of core boxes; generating a training dataset adapted to YOLOv8-seg using a self-developed annotation tool; training the model to automatically predict core box corner points; performing image correction and cropping based on perspective transformation principles; and finally packaging it into an executable (.exe) program, providing a visual interface and supporting batch processing.

[0024] This embodiment addresses the inefficiency of manual processing of large volumes of core images by geological workers. It proposes an automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation, enabling stable and automated processing of large batches of core images. Using Python, and considering user interactivity, program performance efficiency, and stability, this embodiment employs the YOLOv8-seg model, the OpenCV computer vision library, and the Tkinter graphical interface library. It provides a complete end-to-end industrial-grade solution for core boxes with different scaling ratios, rotation angles, and distortion levels, encompassing training set generation, model training and prediction, and perspective transformation processing. Specifically, it includes the following steps:

[0025] Step S1: Use a digital device to take pictures of the core box and collect high-resolution images of the core box that may include adjacent core boxes on the top, bottom, left, and right.

[0026] In the field of geological exploration, according to the relevant requirements of the "Regulations for Original Geological Logging of Solid Mineral Exploration DZT0078-2015", all core samples that have been inspected and sorted must be digitally photographed and archived box by box. Ultimately, each image should only show one set of core boxes that are horizontal and free from bending or deformation. However, core boxes in the core repository are usually placed densely in rows to avoid wasting space. Therefore, during the photography process, it is inevitable that photos of adjacent core boxes, or even two rows of core boxes, will be taken. Furthermore, the core boxes in each photo may not be horizontally positioned, and when placed near drilling rigs in the field, they may be bent or deformed due to uneven ground. Therefore, after collecting the images of the core boxes, geologists usually need to use computer image processing software to rotate, correct, and crop each image.

[0027] By employing the YOLOv8-seg model to predict the four corner points of the core box and utilizing perspective transformation to automate the processing of core images, no outdoor marking preprocessing of the core box is required. Only high-resolution images of the core box need to be collected using digital equipment, thus completely eliminating the time geologists spend processing core images. Furthermore, using the YOLOv8-seg model to predict the four corner points replaces the inefficient step of users interacting with a graphical user interface to determine the cropping range, a process previously inefficient. In summary, compared to traditional image processing algorithms that combine Hough transform, Canny edge detection, and template matching, or rely on manual localization, this solution, using the YOLOv8-seg model for automated prediction of the four corner points of the core box, offers a clear and user-friendly approach, making it a highly readily applicable and easily promoted solution.

[0028] Furthermore, since this technical solution is based on the YOLOv8-seg model and perspective transformation principle, compared to template matching technology, it eliminates the need to determine a matching template and draw matching marks at specific locations on the core box, significantly reducing the overall processing time for core images. Compared to users manually locating the coordinates of the four corner points of the core box in a graphical interface, this technical solution uses deep learning to automatically predict the corner coordinates. This solution iterates from a semi-automatic approach requiring preprocessing of core images and manual annotation of core box corner coordinates to fully automated processing, completely eliminating the time geologists spend on core image processing. This allows geologists to shift their focus to higher value-added processes, enabling them to better concentrate on mineral resource exploration and contributing to breakthroughs in mineral discovery.

[0029] Furthermore, when core boxes photographed in the field are located on slopes or uneven surfaces, the core images will appear distorted. This technical solution, based on the YOLOv8-seg model trained on a training set, can not only add distorted core images but also use training set data augmentation to teach the model how to process distorted core images, thereby improving the model's predictive ability under various complex conditions. For users, this technical solution eliminates the need to draw pre-defined matching markers at specific locations on the core box or manually label the coordinates of the four corner points of the core box in a graphical user interface.

[0030] Step S2: Intelligent annotation of core images and generation of YOLOv8-seg model dataset.

[0031] While a general-purpose annotation tool, LabelImg, exists, it is designed for object detection tasks and only supports rectangular bounding box annotation, which is insufficient to meet the polygonal point sequence annotation requirements for core image contour segmentation. To improve the autonomy, controllability, and functional adaptability of the technical solution, this solution adopts a self-developed annotation tool, whose advantages are mainly reflected in the following three aspects: First, the tool supports polygonal point sequence annotation, which can accurately fit the contour of the core box; second, after annotation, the inter-point interpolation can be flexibly configured to optimize the contour smoothness, and the training set format can be easily modified and exported to adapt to various deep learning models (such as YOLOv8-seg); third, the tool architecture reserves a model interface, supporting the import of pre-trained models to achieve semi-automatic annotation. The operator only needs to judge the automatic annotation effect and make appropriate auxiliary corrections to the annotation points in the graphical interface, thereby significantly improving the annotation efficiency of large-scale training sets.

[0032] Step S2.1: Import the necessary libraries and initialize the interface framework, components, and key variables of the DataLabelingTool class.

[0033] To build interactive annotation software for generating core image training sets for the YOLOv8-seg model, the Tkinter library was imported for the GUI interface, the PIL library for image processing and display, pathlib for cross-platform path operations, and the OpenCV library for image processing.

[0034] Define the DataLabelingTool class. In its __init__ method, use tk.Tk() to create the main window and initialize key variables such as image_dir (to store the image directory path), current_image_idx (to store the current image index), images (to store the image path list), polygons (to store all polygon labels), current_polygon (to store the current polygon point set), and points (to store the currently labeled points).

[0035] Preferably, to improve the modularity and maintainability of the code, an object-oriented class encapsulation approach is adopted to integrate GUI components, annotation logic and data management into the same class, which facilitates state sharing and function expansion.

[0036] Step S2.2: Construct the GUI interface for the control panel and image display area for user interaction using the create_widgets method.

[0037] Since users need to interact with the graphical user interface to complete operations such as image loading, annotation point drawing, annotation area display, annotation control, and data export, the `create_widgets` method uses `tk.Frame` to create the left control panel and the right image canvas. The control panel includes "Open Folder," "Previous / Next" image navigation buttons, annotation control buttons (reset current), and data operation buttons (save annotations, export YOLO-seg data). The image display area uses the `tk.Canvas` component and is bound to... <button-1>The left mouse click event is passed to the on_canvas_click method.

[0038] Step S2.3: Implement functions such as mouse click annotation, automatic closing of annotation points, and polygon drawing in the on_canvas_click and draw_annotation methods.

[0039] Since users need to obtain accurate original coordinate labels when interacting with scaled images in the graphical interface, the conversion from display coordinates to original image coordinates is implemented in the on_canvas_click method. First, the scaling ratio between the displayed image and the original image is calculated (scale_x=original_width / display_width, scale_y=original_height / display_height). Then, the original image coordinates are obtained by subtracting the canvas offset (10,10) from the mouse click coordinates (event.x, event.y) and multiplying it by the scaling ratio.

[0040] To present the core image to be processed in a more aesthetically pleasing way in the GUI interface, the image is not displayed close to the edge of the canvas. In this technical solution, the image is displayed at a position 10 pixels away from the edge of the canvas. Therefore, the mouse click coordinates (event.x, event.y) need to be subtracted from the canvas offset (10, 10).

[0041] Preferably, to avoid floating-point calculation errors, scaling rounding errors, and slight deviations from the boundary when the core box corner is near the image edge, the converted coordinates are always within the valid range of the image by means of, for example, original_x=max(0,min(original_x,original_width-1)), thus ensuring the validity of the labeled points.

[0042] Preferably, to accommodate cases where image scaling is not proportional and to ensure the accuracy of obtaining the original image coordinates, the scaling ratios scale_x and scale_y in the horizontal and vertical directions of the image are calculated respectively.

[0043] Preferably, to export labeled coordinates conforming to the YOLOv8-seg model training set format, a four-point automatic closure mechanism is enabled. When the user clicks the fourth point, the coordinates of the first point are automatically appended to the end of the points list to form a closed polygon, and `save_current_annotation` is immediately called to save the current annotation. Automatic quadrilateral closure not only avoids errors from manual closure but also greatly improves annotation efficiency. The user only needs to click the four core box corner points on the interface to automatically determine the range of the labeled quadrilateral, and then process the next image.

[0044] Preferably, the draw_annotation method employs layered drawing and visual optimization techniques. First, a semi-transparent filled polygon is drawn (using a stipple='gray25' dot matrix pattern to achieve a 50% transparency effect), then a red border is drawn (outline_color="#FF0000"), followed by the drawing of vertex dots and number labels with white outer rings, and finally, connecting lines are drawn as needed.

[0045] The core process of the `on_canvas_click` method is as follows: When the user clicks the mouse on the image canvas, firstly, the click coordinates are subtracted by a canvas offset of 10 pixels (to avoid the image being displayed too close to the canvas edge); then, the width and height of the original image and the displayed image are obtained, and the horizontal and vertical scaling ratios are calculated respectively; the adjusted click coordinates are converted into original image coordinates using the scaling ratios, and the coordinates are restricted to the valid range of the original image (not exceeding the image's width and height boundaries); the converted coordinates are added to the annotation point set, and when the number of annotation points reaches 4, the coordinates of the first point are automatically appended to the end of the set, achieving automatic polygon closure; then, the current annotation data is saved and the annotation area is drawn on the canvas.

[0046] Step S2.4: Implement EXIF ​​orientation correction and adaptive scaling during the core image loading process using the load_current_image method.

[0047] Since actual core images may contain EXIF ​​rotation information and have different resolutions, the `load_current_image` method uses `PIL.Image.open` to load the image and automatically corrects the image orientation by checking EXIF ​​label 274 (orientation marker). An orientation of 3 results in a 180° rotation, 6 in a 270° rotation, and 8 in a 90° rotation. The display size is then dynamically calculated based on available canvas space and the original image aspect ratio. A high-quality LANCZOS resampling algorithm is used for scaling to ensure image display quality. A `display_image` is then generated and displayed on the canvas using `ImageTk.PhotoImage`. Simultaneously, the saved annotations corresponding to the current image (`self.polygons[self.current_image_idx]`) are loaded into `self.points`, and the `draw_annotation` method is called to redraw the image.

[0048] Preferably, an adaptive display size calculation algorithm is implemented. First, the actual usable canvas size (canvas.winfo_width()-20, canvas.winfo_height()-20) is obtained, and the image aspect ratio (img_ratio) and canvas aspect ratio (canvas_ratio) are calculated. If img_ratio > canvas_ratio, the height is calculated with the width as a constraint; otherwise, the width is calculated with the height as a constraint, ultimately ensuring that the displayed image is fully visible and maximizing the use of canvas space.

[0049] Preferably, to ensure compatibility with paths containing Chinese characters or other non-ASCII characters, PIL.Image.open is used to directly load the image instead of cv2.imread, avoiding OpenCV's failure to read Chinese paths on Windows systems. Simultaneously, image navigation is implemented using the prev_image and next_image methods, automatically calling save_current_annotation to save the current annotation before switching, ensuring no annotation data is lost. A try-except exception handling method is used during image loading; if it fails, tkinter.messagebox.showerror is used to notify the user, enhancing the tool's robustness.

[0050] Preferably, a robust image file verification and deduplication mechanism is constructed. The `load_images` method implements folder-level batch loading, using `pathlib.Path` to traverse the image folder, collecting all supported file formats (.jpg, .jpeg, .png, .bmp, .tiff, .tif and their uppercase versions). Deduplication is performed using a set of absolute paths, and then `PIL.Image.verify()` is used to verify the integrity of each image file. This mechanism effectively avoids program crashes due to file corruption and counting errors due to case sensitivity, ensuring that only valid and unique image files are loaded.

[0051] The EXIF ​​selection process in the `load_current_image` method is as follows: When loading an image, it attempts to read the image's EXIF ​​information, focusing on checking the orientation indicator (label 274); if the indicator is 3, the image is rotated 180°; if the indicator is 6, it is rotated 270°; if the indicator is 8, it is rotated 90°, thus correcting the image orientation deviation caused by the device's placement angle during shooting; if reading the EXIF ​​information fails or there is no orientation indicator, no rotation operation is performed to avoid program errors.

[0052] The process of adjusting the display size in the `load_current_image` method is as follows: First, calculate the available size of the canvas (actual canvas width and height minus 20 pixels). Then, calculate the aspect ratio of the original image and the canvas. If the image aspect ratio is greater than the canvas aspect ratio, calculate the display height with the canvas width as a constraint; otherwise, calculate the display width with the canvas height as a constraint to ensure that the image is displayed completely and maximizes the use of canvas space. The LANCZOS resampling algorithm is used to scale the image to ensure display quality. If the available canvas size is too small (less than 1 pixel), the original image is directly copied as the display image.

[0053] Step S2.5: Implement persistent saving of annotation data and export of YOLOv8-seg format in the save_annotation and export_YOLO_seg_format methods.

[0054] Since the annotation results need to be persistently saved and converted into the format required for deep learning training, the save_annotation method iterates through the self.polygons list and saves the polygon annotations containing at least 3 points in JSON format to the seg_annotations.json file in the image directory. The key is the image path and the value is the list of polygon point coordinates.

[0055] The `export_YOLO_seg_format` method first determines the export directory through a folder selection dialog box, then automatically creates the YOLOv8-seg standard directory structure (images / train / , images / val / , labels / train / , labels / val / ), randomly dividing the labeled images into a training set (80%) and a validation set (20%). Next, for each sample, the image is copied to the corresponding `images` subdirectory, and the polygon point coordinates are normalized and written to a `txt` file in the `labels` subdirectory, in the format `class_id x1 y1 x2 y2...`. Finally, a `data.yaml` configuration file containing the path, number of classes, and class names is generated.

[0056] Preferably, considering actual annotation requirements, multiple points are sometimes densely annotated along the core box outline to more accurately describe irregular shapes. At the same time, it is also compatible with training set label formats containing interpolation points. Therefore, polygon optimization processing is implemented during data conversion. The _simplify_polygon_for_seg method simplifies complex polygons to a maximum of 20 points. The equal-interval sampling algorithm (step=len(polygon) / / max_points) is used to reduce the number of points while maintaining the polygon shape features. This is particularly suitable for the point limit requirements of YOLOv8-seg format training sets.

[0057] Preferably, considering EXIF ​​orientation consistency processing, the EXIF ​​rotation information is processed independently for each image in the `_export_seg_sample` method to ensure that core images with different original shooting orientations maintain a unified orientation in the training dataset. Simultaneously, polygon coordinates are normalized to relative coordinates within the range [0,1] (norm_x=x / img_width, norm_y=y / img_height) to ensure that model training is not affected by specific image resolutions. After normalization, boundary protection is performed using `max(0.0,min(1.0,...))` to prevent training errors caused by annotation points slightly exceeding image boundaries.

[0058] Preferably, to ensure continuity of the annotation work, the load_existing_annotations method is automatically called in the load_images_fixed method. If the seg_annotations.json file is detected, the existing annotations are loaded into self.polygons to avoid duplicate annotations.

[0059] The core process of the `save_annotation` method is as follows: Iterate through the polygon annotation data of all images, collecting only valid annotations containing at least 3 points; organize the valid annotation data into a dictionary format where the key is the image path and the value is a list of annotation point coordinates; create a `seg_annotations.json` file in the image's directory, write the annotation dictionary into the file in JSON format, and save it; if there is no valid annotation data, display a message to the user saying "No annotation data can be saved"; if an error occurs during the saving process, display an error message containing specific error information.

[0060] The core process of the `export_YOLO_seg_format` method is as follows: First, the user selects the export directory through a folder selection dialog box. Then, a standard directory structure (containing four subdirectories: images / train, images / val, labels / train, and labels / val) is automatically created in that directory. All image data containing valid annotations (at least 3 points) are collected, randomly shuffled, and divided into training and validation sets in an 80%:20% ratio. The image files of the training and validation sets are copied to the corresponding images subdirectories, and the coordinates of the polygon annotation points are normalized to the range [0,1] and written to the corresponding labels subdirectory's txt file in the format of "category ID + coordinate sequence". Finally, a data.yaml configuration file is generated, specifying the dataset path, the number of categories (1 category, named core_box), and the number of training and validation set samples.

[0061] In some embodiments, the self-developed annotation tool described in S2 supports importing intermediate checkpoint models generated during the training process in S3 as pre-trained auxiliary models. The semi-automatic annotation process includes: ① After the tool loads the core image to be annotated, it automatically calls the pre-trained auxiliary model to perform preliminary segmentation of the core box outline and outputs candidate polygon annotations; ② The candidate annotations are displayed on the image canvas in semi-transparent gray. Users can operate through the "Accept Annotation", "Correct Annotation", and "Reject Annotation" buttons on the graphical interface. When correcting, users can directly drag the vertices of the candidate annotations to adjust their positions, or click on the canvas to add new vertices or press the Delete key to delete redundant vertices; ③ The tool has a built-in annotation quality scoring module, which scores the annotation effect of the pre-trained auxiliary model in real time based on the overlap (IoU) between the candidate annotations and the manually corrected annotations. When the score is higher than 85 points, the annotation result is automatically saved. When the score is lower than 60 points, the model feedback mechanism is triggered, and the image is added to the supplementary training set for subsequent fine-tuning of the model to continuously improve the accuracy of semi-automatic annotation.

[0062] In some embodiments, the self-developed annotation tool described in S2 adds an ellipse fitting-centroid calibration linkage mechanism to optimize polygon annotation accuracy: ① When the user clicks on 4 corner points during annotation, the tool automatically extracts 100-200 sampling points from the edge of the core box and fits the edge contour through the least squares ellipse fitting algorithm to eliminate corner point deviations caused by blurry images; ② Calculate the ellipse center based on the major and minor axis parameters of the fitted ellipse, and perform weighted fusion with the centroid of the original vector geometry algorithm (ellipse center weight 0.7, original centroid weight 0.3) to obtain a more robust corrected centroid; ③ Recalculate the vector angles of each corner point based on the corrected centroid to avoid corner point sorting errors caused by slight curvature of the core box edge. Meanwhile, the tool integrates near-infrared modal auxiliary annotation function: for core images with severe reflection (brightness variance > 80) or single color (saturation < 0.3), it automatically calls the near-infrared image channel (if the original image contains near-infrared information) or generates a pseudo near-infrared image through GAN to highlight the contour difference between the core box and the background, helping users to quickly locate corner points and improving annotation efficiency by more than 40%.

[0063] Step S3: Core box segmentation training and validation based on the YOLOv8-seg model.

[0064] Preferably, the instance segmentation mask generated by YOLOv8-seg provides more accurate object contour information than a simple bounding box. For near-quadrilateral targets like core boxes, the four corner points can be accurately extracted from the mask, providing a basis for subsequent perspective transformation correction. Compared to methods based on YOLOv8-pose keypoint detection, the segmentation mask is more robust to interference such as occlusion and shadows. Furthermore, observation of the training samples clearly shows that the four corner points of the core box do not have prominent features in the samples, and the actual prediction performance of the trained model is not as good as the segmentation model. Therefore, the final technical solution selected YOLOv8-seg as the corner point prediction model for the core box.

[0065] Step S3.1: Set up the environment and define the basic framework and parameter checks of the training entry function train_seg_model.

[0066] By adopting a modular design oriented towards functions, the core training logic is encapsulated in the `train_seg_model` function instead of being written directly in the script's global scope. This design facilitates code reuse, unit testing, and parameterized calls, and users can easily integrate this function into their own automated workflows.

[0067] The ultralytics.YOLO library provides interfaces for loading, training, and inference of the YOLOv8-seg instance segmentation model; the torch library, as a deep learning framework, provides GPU-accelerated computation, automatic differentiation, and neural network construction support for the model; and the yaml library is used to parse and validate data.yaml files containing key configurations such as dataset paths and category information. These three core libraries together constitute the technical foundation for training the core box segmentation model, and are responsible for model algorithm implementation, underlying computation acceleration, and training configuration management, respectively.

[0068] To resolve dynamic link library conflicts caused by multiple scientific computing libraries (such as PyTorch, NumPy, OpenCV, etc.) simultaneously loading different versions of the Intel OpenMP runtime library libiomp5md.dll on Windows systems, the environment variable os.environ['KMP_DUPLICATE_LIB_OK']='TRUE' is set to avoid runtime crashes such as "Intel MKL FATAL ERROR: Cannot load libiomp5md.dll" or "OMP: Error#15" due to library version incompatibility.

[0069] In the `train_seg_model` function, the existence of the configuration file is checked using `Path(data_yaml).exists()`, and the contents of the `data.yaml` configuration file are read and verified using `yaml.safe_load` to ensure that the data path, number of categories, and category names are set correctly.

[0070] Preferably, a strict configuration file verification mechanism is implemented. Before training begins, not only is the existence of the file checked, but the contents of the YAML file are also read and parsed to verify key fields such as nc (number of categories) and names (category names), so as to detect configuration errors in advance and avoid unexpected failures caused by data configuration problems during training.

[0071] The configuration file verification mechanism in the train_seg_model method follows these steps: Before training the model, check if the data.yaml configuration file exists in the specified path. If it does not exist, prompt the user "configuration file does not exist" and terminate the training. If the file exists, read the file content and verify the key configuration items to ensure that the file contains required fields such as dataset path, number of categories (nc), and category name (names), thus preventing training failures caused by configuration errors in advance.

[0072] Step S3.2: Configure the training parameters of the YOLOv8-seg model and start the training process.

[0073] Since training the YOLOv8-seg model requires setting a large number of hyperparameters to balance training efficiency and segmentation accuracy, the train_seg_model function first automatically selects the device based on CUDA availability (device='cuda'iftorch.cuda.is_available()else'cpu'), and then constructs the train_args parameter dictionary, which includes basic parameters such as the number of training epochs, image size imgsz, batch size, number of worker processes, and optimizer type optimizer.

[0074] Preferably, the device detection logic is implemented to automatically detect the availability of CUDA GPUs through torch.cuda.is_available(), prioritize the use of GPUs for accelerated training, and automatically fall back to CPUs when GPUs are unavailable. This design ensures both training efficiency on high-performance workstations and runnability on ordinary computers without GPUs.

[0075] To address the issue of diverse specifications, colors, and materials of core boxes in actual geological exploration projects, and to enhance the generalization ability of neural network models to complex visual scenes, a large-scale sample dataset covering various specifications and colors was first constructed. Secondly, a multi-level data augmentation scheme was implemented, which simulated different lighting and color variations through color space transformation and adapted to changes in shooting angle through geometric transformation. This generated sufficiently rich training data based on limited labeled samples, ensuring that the model can robustly adapt to changes in the visual characteristics of various core boxes.

[0076] Data augmentation uses a series of image transformation techniques (such as color adjustment, geometric transformation, and noise addition) to artificially expand the diversity and coverage of training data while keeping the core semantic features of the core box unchanged. This improves the generalization ability and robustness of the neural network model for core boxes of different sizes, colors, and materials, effectively alleviating the overfitting problem caused by differences in actual shooting conditions (such as changes in lighting, complex backgrounds, and varied core colors). This enables the trained segmentation model to adapt to the complex and varied visual scenes of core boxes in actual geological exploration projects.

[0077] Preferably, the data augmentation parameters are finely adjusted according to the characteristics of the core image segmentation task: the perspective (perspective transformation intensity) is set to 0.0006, because a distortion-free core box usually maintains a relatively regular rectangular shape in the photo, and data augmentation through perspective transformation can train the model to cope with core box distortion; a moderate rotation enhancement (degrees=10) is retained to cope with changes in shooting angle and to train the model to cope with non-horizontal core boxes; a moderate scaling enhancement (scale=0.5) is set to adapt to shooting at different distances and to train the model to cope with core boxes photographed at different heights.

[0078] Preferably, to solve the compatibility problem of multi-process data loading under Windows system, the worker parameter is explicitly set to 0. Although this will slightly reduce the data loading efficiency, it avoids the data loader crashing due to the difference between Windows and Linux process models, and ensures the stability of the training process.

[0079] Preferably, a reasonable early stopping mechanism (patience=30) and periodic saving (save_period=5) are set. When the validation metric does not improve for 30 consecutive rounds, training is automatically stopped to prevent overfitting. At the same time, intermediate checkpoints are saved every 5 rounds, which ensures that training can be resumed after interruption and does not generate too many intermediate files that occupy disk space.

[0080] The `train_args` parameter dictionary is set as follows: It automatically detects whether the current hardware environment supports CUDA GPU; if so, it prioritizes GPU training; otherwise, it uses the CPU. It configures the core training parameters, with a default training epoch count of 50, input image size of 640 pixels, batch size of 16, and worker processes set to 0 on Windows systems (to avoid compatibility issues with multi-process data loading). It enables an early stopping mechanism, automatically stopping training if the validation metric does not improve for 30 consecutive epochs, and saving intermediate model checkpoints every 5 epochs. It sets data augmentation strategies, including perspective transformation (intensity 0.0006), rotation within a 10° range, 0.5x scaling, and color space transformation, to improve the model's generalization ability. Additionally, it configures parameters such as weight decay, warm-up epoch count, and loss weights to balance training efficiency and model accuracy.

[0081] Step S3.3: Perform model training and validation, and generate detailed model performance metrics.

[0082] Since the model's segmentation performance on the validation set needs to be evaluated after training, model.val() is called for validation after model.train() is executed. During the validation process, all available evaluation metrics are dynamically obtained and printed using Python reflection mechanisms dir(metrics) and getattr, including the general bounding box metric metrics.box, the segmentation-specific metric metrics.seg, and the average precision metric metrics.map.

[0083] Preferably, a dynamic metric probing mechanism is adopted, which uses reflection to traverse all attributes of the metrics object and display all available performance metrics. This method does not require prior knowledge of the specific structure of the metrics returned by the ultralytics library, and has good version compatibility. Even if the YOLOv8 library updates the metric return format in the future, the code flow can still work normally.

[0084] Preferably, a hierarchical metric display strategy is implemented, first displaying general metrics such as bounding box related metrics (metrics.box), then displaying segmentation-specific metrics (metrics.seg), and finally displaying comprehensive evaluation metrics (metrics.map). This organization method conforms to the cognitive logic of moving from general to specific and from local to global, making it easier for developers to understand the model's performance in different aspects.

[0085] Preferably, the training output is output, and after training is completed, model files (best.pt, last.pt), visualization files (loss curve, PR curve, etc.) are generated to provide users with complete model performance data, which facilitates subsequent model selection, deployment and result tracking.

[0086] Preferably, the entire training process is monitored, the total training time is recorded, and the model file size is monitored. For scenarios requiring embedded deployment, users can make appropriate choices based on the balance between model size and accuracy.

[0087] The training and validation process in the `train_seg_model` method is as follows: First, the corresponding YOLOv8-seg pre-trained model is loaded according to the model size (n / s / m / l / x) selected by the user; the model training is started according to the configured training parameters; after training, the validation function is called to evaluate the model's performance on the validation set; all attributes of the validation metric object are traversed through the reflection mechanism, and bounding box related metrics, segmentation-specific metrics, and mean accuracy (mAP) metrics are displayed hierarchically without prior knowledge of the specific structure of the metrics, adapting to different versions of the model library; finally, the best model (best.pt), the latest model (last.pt), and visualization files such as loss curves and PR curves generated by training are output, and the names and sizes of these files are also counted for easy viewing and selection by the user.

[0088] The process related to the output of training products and the generation of model performance metrics by the train_seg_model method is as follows:

[0089] 1. Dynamic Metric Exploration (Reflection Traversal of Performance Metrics): After training, the model is traversed through all attributes of the metric object using a reflection mechanism. Non-private (not starting with an underscore) and non-callable attributes are selected. These attributes are the model's usability performance metrics. The name and corresponding value of each metric are printed and displayed one by one. It is not necessary to know the specific structure of the metric in advance, ensuring compatibility with different versions of the model library and avoiding display failures caused by updates to the metric return format.

[0090] 2. Layered Metric Display (Performance Presented by Dimension): A layered display strategy is adopted to present model performance: First, general metrics related to bounding boxes are displayed. If the metric object contains the "box" attribute, the non-private, non-callable sub-attributes under this attribute are traversed to print specific metrics such as precision, recall, and mean precision related to the bounding box. Second, segmentation-specific metrics are displayed. If the metric object contains the "seg" attribute, the specific performance metrics corresponding to the segmentation task are similarly traversed and printed. Finally, comprehensive evaluation metrics are displayed. If the metric object contains the "map" attribute, the mean precision (mAP) related metrics under different thresholds are traversed and printed. The logic of "general → specific → comprehensive" helps users clearly understand the model's performance in each dimension.

[0091] 3. Training Output Statistics and Path Output: Automatically locates the directory where model training is saved, obtains the paths of the best model file (best.pt) and the latest model file (last.pt) in the "weights" subdirectory of that directory, making it convenient for users to select and use them later; at the same time, it traverses all files in the training save directory, filters out the visualization files generated during the training process (such as loss curves, PR curves, etc.), and counts and displays the names and sizes (in KB) of the top 5 files, providing users with complete training output information, which is convenient for subsequent model deployment, performance tracking and result analysis.

[0092] Step S3.4: Build the main program, interactively determine the yaml path, epochs, imgsz and model size and train it.

[0093] Since the model training process may be intended for geological researchers without programming experience, an interactive command-line main program, `main`, was designed. Upon startup, the program first prompts the user to enter or select the path to the `data.yaml` configuration file.

[0094] Preferably, a progressive parameter configuration process is designed. For training tasks, key parameters such as the number of training epochs, image size (imgsz), and model size are queried in sequence, and reasonable default values ​​are provided for each parameter.

[0095] Preferably, comprehensive error handling and user guidance are implemented, input validation is performed at each interactive step, clear prompts are given for invalid input, specific solution suggestions are provided for situations such as missing files, and detailed print information and formatted output interface make the entire tool intuitive and less prone to errors.

[0096] In some embodiments, the data augmentation strategy described in S3 adopts a scene-adaptive adjustment mechanism: ① For images of densely packed core boxes, the scaling ratio in the augmentation strategy is adjusted to 0.3-0.7 times, and random cropping enhancement is enabled. The cropping area is centered on the candidate area of ​​the core box to avoid cropping the key outline of the core box; ② For distorted core images taken on outdoor slopes, the perspective transformation intensity is increased to 0.001-0.0015, and random affine transformation enhancement is added. The affine parameter range is dynamically adjusted according to the degree of distortion (slight distortion: translation ±10 pixels, rotation ±5°; severe distortion: translation ±20 pixels, rotation ±15°); ③ For images with large differences in illumination (judged by the average image brightness, with the average value below 100 or above 200), color balance enhancement and Gamma correction (Gamma value range 0.8-1.2) are added to simulate the visual characteristics of the core box under different illumination conditions and improve the robustness of the model to illumination interference.

[0097] In some embodiments, the model training described in S3 adopts a Bayesian optimization-dynamic loss weight adaptive strategy instead of a fixed loss weight configuration: ① Using the validation set mAP@0.5 as the objective function, the Bayesian optimization algorithm dynamically searches for box loss weights (search range 3-7), cls loss weights (search range 0.5-2), dfl loss weights (search range 0.5-2), and mask loss weights (newly added, search range 2-6), updating the weight combination every 10 rounds; ② During training, the core box size distribution is monitored in real time (small size <300px, medium size 300-600px, large size >600px). When the validation recall rate of a certain size sample is <85%, the feature enhancement branch of the corresponding scale is automatically activated (small size samples strengthen the number of shallow feature convolution kernels, and large size samples add deep feature attention modules). In addition, the data augmentation strategy incorporates mineral texture transfer technology: the core textures of different mineral types (sandstone, limestone, granite) are transferred to the training samples through a style transfer network to simulate the diversity of core materials in real geological exploration. The model improves the segmentation accuracy of heterogeneous core boxes by 15%-20%.

[0098] Step S4: High-performance adaptive segmentation and perspective transformation correction clipping of core boxes based on YOLOv8-seg

[0099] Step S4.1: Import the necessary libraries and initialize the hardware adaptation and performance optimization system of the CoreBoxSegPredictor class.

[0100] Since this technical solution requires a graphical user interface for core box segmentation prediction and ensures processing efficiency and cross-platform compatibility, it first imports core deep learning and image processing libraries such as torch, cv2, and numpy, sets matplotlib.use('Agg') to avoid GUI conflicts, and imports ultralytics.YOLO for segmentation prediction. In the __init__ method of the CoreBoxSegPredictor class, the availability of CUDA devices is automatically detected by torch.cuda.is_available(), intelligently selecting devices (GPU priority), and dynamically setting the max_workers parameter based on the device type (multi-threaded for CPU, single-threaded for GPU).

[0101] Preferably, to improve the initial inference speed, model warm-up is implemented in GPU mode. A random tensor, dummy_input, is created and propagated through the model forward. This operation triggers CUDA kernel compilation and memory allocation, reducing the latency of the initial inference and significantly improving the user experience. Immediately after warm-up, torch.cuda.empty_cache() is called to clear temporary memory, avoiding excessive GPU memory consumption during the warm-up process.

[0102] 100 highly discriminative colors in the HSV color space are dynamically generated for instance segmentation mask visualization. This is mainly based on three core advantages of the HSV color model: First, the hue dimension of HSV provides a natural cyclic color space. By uniformly distributing hue values ​​(hue=i / n_colors), it ensures the maximum interval between adjacent index colors on the color wheel, completely avoiding color confusion. Second, fixed saturation (S=0.8) and brightness (V=1.0) ensure that all generated colors maintain both high vibrancy and consistent visual weight, preventing interference with target recognition due to differences in brightness. Finally, the HSV-to-RGB conversion algorithm (_hsv_to_rgb) can accurately map perceptually uniform color differences to the display space. Combined with a semi-transparent overlay parameter of mask_alpha=0.5, it ensures that the masked area is clearly distinguishable while preserving the contextual information of the original image content, achieving the optimal visual expression of semantic segmentation results.

[0103] Preferably, 100 highly distinguishable colors are dynamically generated using the HSV color space for visualization of segmentation masks for different instances. An algorithm is used to maximize the color difference between adjacent indices (using the _hsv_to_rgb method) to avoid visual confusion caused by colors being too similar when displaying multiple segmentation targets. Simultaneously, the semi-transparent mask overlay parameter mask_alpha is set to 0.5 to maintain mask visibility without completely obscuring the original image content.

[0104] This method plays a crucial role in scenarios with densely packed core boxes. By dynamically generating 100 highly distinguishable colors using the HSV color space, it assigns unique color identifiers with clear visual differences to the four corner points of each adjacent core box (e.g., red for the upper left, blue for the upper right, green for the lower left, and yellow for the lower right). Even when there are adjacent core boxes on all sides, it can clearly distinguish the corner point affiliation of the current target box from other background boxes. Simultaneously, a 50% semi-transparent overlay ensures that the texture features of the core box itself and the boundary information of adjacent boxes are not obscured when displaying the identified corner point markers. This helps developers quickly verify the accuracy of the automatically identified corner points, avoids misjudgments of corner point affiliation due to similar colors, and improves the interpretability of corner point recognition for densely packed core boxes.

[0105] The model warm-up process implemented in the `__init__` method of the `CoreBoxSegPredictor` class in GPU mode is as follows: If the model is running in a GPU environment, a random tensor with dimensions of 1×3×640×640 is created as virtual input data and passed into the model for one forward propagation; this operation can trigger CUDA kernel compilation and GPU memory allocation, reducing the initial latency during subsequent actual image inference; after the warm-up is completed, the temporarily occupied GPU memory is immediately cleared to avoid affecting the memory usage of subsequent batch processing.

[0106] The process of the multi-target visual discrimination color generator in the initialization method _init_ of the CoreBoxSegPredictor class is as follows: 100 visually distinct colors are generated for instance segmentation mask visualization. Specifically, this is achieved through the HSV color space: hue values ​​are assigned at uniform intervals (the hue value of each color is "serial number / total number of colors"), with a fixed saturation of 0.8 and a brightness of 1.0 to ensure vivid colors and consistent visual weights; the above parameters are converted to RGB color values ​​using an HSV-to-RGB algorithm, ultimately generating a set of 100 colors to avoid visual confusion caused by similar colors among multiple segmentation targets.

[0107] Step S4.2: Implement an efficient algorithm for extracting the minimum bounding quadrilateral and optimizing geometry in the _get_min_area_rect method.

[0108] Since a regular quadrilateral boundary needs to be extracted after core box segmentation for subsequent perspective transformation, the `_get_min_area_rect` method uses the `cv2.findContours` RETR_EXTERNAL mode to extract only the outermost contour of the segmentation mask. This reduces computational cost compared to the `RETR_LIST` or `RETR_TREE` modes. After obtaining the maximum contour, `cv2.minAreaRect` is used to calculate the minimum bounding rectangle, and `cv2.boxPoints` is used to obtain the four corner points of the quadrilateral.

[0109] Preferably, for the crucial step of sorting the corner points of quadrilaterals, an optimized sorting algorithm based on vector geometry is implemented. Figure 2 The algorithm first calculates the centroid of the quadrilateral, then calculates the vector from each point to the centroid, and calculates the vector angle using `np.arctan2`. After sorting by angle, it finds the top-left corner point (the point with the smallest x+y ratio). Finally, it uses `np.roll` to cyclically shift the points to obtain the clockwise order. Compared to the traditional method of "sorting the x-coordinates and then dividing them into two groups for sorting", this algorithm reduces the computational complexity from O(nlogn) to O(n) and has higher numerical stability, especially when the quadrilateral is close to a rectangle or has rotations, it can still maintain the correct order.

[0110] Preferably, a boundary protection mechanism is implemented in the sorting algorithm: np.clip is used to ensure that the corner coordinates do not exceed the image boundary, avoiding coordinate out-of-bounds errors during subsequent perspective transformations. Simultaneously, the geometric properties of the rectangle, such as width, height, and aspect ratio, are calculated and returned to provide data support for subsequent perspective transformation parameter selection (such as output size calculation).

[0111] The process of optimizing the quadrilateral corner point sorting based on vector geometry in the `_get_min_area_rect` method is as follows: After extracting the minimum bounding quadrilateral of the segmentation mask, the average coordinates of the four corner points of the quadrilateral are first calculated as the centroid; then, the vector from each corner point to the centroid is calculated, and the angle of each vector is calculated using the arctangent function of the vector; all corner points are sorted in ascending order of angle, and then the sum of the x and y coordinates of each sorted corner point is calculated, and the corner point with the smallest sum is selected as the top-left corner point; finally, the corner point order is adjusted to a clockwise order of top-left, top-right, bottom-right, bottom-left through a cyclic shift operation to ensure the consistency of the corner point order.

[0112] Step S4.3: Construct efficient single-image and batch prediction pipelines in the predict_single_image_fast and predict_batch_fast methods.

[0113] To handle a large number of core images and ensure processing speed, multi-dimensional optimizations were implemented in the `predict_single_image_fast` method: a fast image reading mode using `cv2.IMREAD_COLOR` was employed; a fixed inference size `imgssz=640` was used to avoid the overhead of dynamic size adjustments; and `max_det=10` was set to limit the maximum number of detections to prevent excessive redundant detections. During inference, the `torch.no_grad()` context manager was enabled to disable gradient calculation, reducing memory usage and computational overhead. (Settings for `imgssz`)

[0114] Preferably, an intelligent memory management strategy is implemented in the post-processing stage of the prediction results. For each detected mask, a binary mask is immediately generated using cv2.resize and thresholding. After calculating the minimum bounding quadrilateral, intermediate variables are immediately deleted (del mask, mask_resized, mask_binary). After the entire single-image processing is completed, boxes.cpu() and masks.cpu() are explicitly called to move the tensor off the GPU, and torch.cuda.empty_cache() and gc.collect() are used for thorough memory cleanup to ensure that OOM (Out-Of-Memory Error) errors will not occur due to memory leaks during long-term batch processing.

[0115] Preferably, an adaptive parallel processing mechanism is implemented in the `predict_batch_fast` method: In CPU mode, a `ThreadPoolExecutor` thread pool (maximum `max_workers` threads) is dynamically created based on the number of cores. Each thread processes one image, and a 60-second timeout is set using `future.result(timeout=60)` to prevent individual abnormal images from blocking the entire process. In GPU mode, single-threaded sequential processing is used because the YOLO model itself is highly optimized and GPU parallelism is saturated; multi-threading would actually lead to performance degradation due to GPU context switching. After processing 50 images (one batch), memory usage monitoring `_log_memory_usage` is executed once, using the `psutil` library to obtain process memory usage and GPU memory usage, and displaying it in real time in the log.

[0116] To address the challenges of large numbers of high-resolution core images and the time-consuming instance segmentation, a batch processing approach with a batch size of 50 was implemented. This grouping of images effectively prevents memory overflow, supports processing extremely large datasets, provides real-time progress feedback, and lays the foundation for system fault tolerance and recoverability. This is a key optimization design for achieving efficient and stable processing of massive amounts of high-resolution geological images. To prevent program crashes when processing large numbers of core images, the image files to be processed are divided into groups of 50 for batch processing. Memory is cleared after each batch is processed to avoid OutOfMemory (OOM) errors.

[0117] The processes related to fixed batch size in the `predict_batch_fast` method and the intelligent memory management strategy in the `CoreBoxSegPredictor` class are as follows: Batch prediction and memory optimization: During batch processing, each batch processes a fixed 50 images to avoid memory overflow caused by loading too many images at once; In CPU mode, a thread pool is dynamically created based on the number of physical cores (maximum of 8 threads), with each thread processing one image independently, and a 60-second timeout is set for each thread to prevent abnormal images from blocking the entire process; In GPU mode, single-threaded sequential processing is used to avoid GPU context switching reducing performance; During processing, memory usage is monitored and recorded in real time after each batch is completed; Gradient calculation is disabled during the inference phase to reduce memory consumption, and intermediate variables such as masks, scaled masks, and binary masks are deleted immediately after each image is processed, the tensors output by the model are moved off the GPU, and GPU memory and system garbage collection are performed simultaneously; After batch processing is completed, the result data of that batch is deleted to further release memory.

[0118] Step S4.4: Implement fast perspective transformation and result saving optimization in the _apply_perspective_transform and _save_perspective_transform_fast methods.

[0119] Preferably, since core box calibration requires mapping the detected quadrilateral region to a regular rectangle through perspective transformation, several acceleration strategies are adopted in the _apply_perspective_transform method: using integer coordinates and np.float32 data type to reduce memory usage, using np.clip to ensure that the source point coordinates are within the image range, using the cv2.INTER_LINEAR interpolation method (about 2 times faster than INTER_CUBIC but with similar quality), and setting borderMode=cv2.BORDER_CONSTANT and borderValue=(0,0,0) to fill the blank area with black.

[0120] Preferably, to prevent edge information from being cropped, an intelligent output size calculation algorithm is implemented. When output_size is not specified, the output size is automatically calculated based on the minimum bounding rectangle of the quadrilateral corner points, and a 10% margin (*1.1) is added to ensure that the core box content is fully contained, while limiting the maximum size to no more than twice the original image to prevent the generation of excessively large images due to extreme quadrilaterals. This adaptive size calculation ensures content integrity while avoiding unnecessary resolution waste.

[0121] Adding a 10% margin primarily addresses three key issues: First, corner detection inherently has errors; the vertices of quadrilaterals detected by the YOLO model often fail to accurately fall on the actual outermost edge of the core box, resulting in a deviation of a few pixels. Second, the curved edges of the core box in the original image may undergo non-linear deformation after perspective transformation, causing the actual mapping range to slightly exceed the rectangular area calculated based on corner points. Finally, image interpolation algorithms produce semi-transparent transition pixels in edge regions; without buffer space, these edge pixels will lose crucial information during cropping. This 10% safety margin ensures the integrity of the core sample while preserving the necessary image context for subsequent quantitative analysis and visual evaluation.

[0122] Preferably, the file saving process is optimized in the `_save_perspective_transform_fast` method: a subfolder named `perspective_transforms_fast` is created directly in the directory containing the original image to save the results, maintaining the same filename structure as the original image (only adding the `_trans_{id}` suffix), making it easy for the results to correspond with the original image. During saving, optimized encoding parameters are selected based on the file extension: for JPEG format, `[cv2.IMWRITE_JPEG_QUALITY,95,cv2.IMWRITE_JPEG_OPTIMIZE,1]` is used to enable Huffman optimization while maintaining 95% quality; lossless formats such as PNG use the default parameters. This design reduces disk space usage while maintaining sufficient visual quality.

[0123] The process of accelerating perspective transformation mapping to a regular rectangle in the `_apply_perspective_transform` method is as follows: Corner coordinates are stored using the `float32` data type to reduce memory usage and improve calculation speed; the source corner coordinates are first limited to the original image range to avoid coordinate overflow; if no output size is specified, the extreme difference between the x and y coordinates of the corners is automatically calculated as the base size, and then a 10% margin is added (to avoid core information loss due to corner detection errors), while limiting the output size to no more than twice the size of the original image; the four corners of the target rectangle are defined (top left (0,0), top right (output width -1,0), bottom right (output width -1, output height -1), bottom left (0, output height -1)), and the perspective transformation matrix from the source corner to the target corner is calculated; the `INTER_LINEAR` interpolation method (balancing speed and accuracy) is used to perform perspective transformation on the image, and the blank areas after transformation are filled with black, finally obtaining the corrected image of the regular rectangle.

[0124] Step S4.5: Build the graphical user interface and end-to-end interactive management system in the ModernSegmentationGUI class.

[0125] To provide geological researchers with an intuitive and user-friendly interface, a modern UI design was adopted in the ModernSegmentationGUI class. A Material Design-style color scheme was used (the main color, accent color, etc., were defined using the self.colors dictionary), and a flat interface style was implemented using ttk.Style and theme_use('clam'). The interface layout adopted a card-based design (config_card, log_card), with main function buttons evenly distributed in a grid layout and color-coded to indicate status (blue - select model, green - select directory, purple - set confidence level, orange - start processing).

[0126] Preferably, a dynamic button status feedback mechanism is implemented: when the user successfully selects a model file, the select_model button turns dark blue and displays "Model selected"; when selecting an image directory, the button turns dark green and displays "Directory selected"; after setting the confidence level, the button turns dark purple and displays the specific value. Simultaneously, the status label displays different colored prompts based on the configuration completion status. This multi-layered visual feedback significantly reduces user confusion.

[0127] Preferably, semantic tagging and color coding are implemented in the log system: log messages are automatically categorized into "error" (red), "success" (green), "warning" (orange), and "progress" (blue) based on their content, and a different foreground color is set for each message category using `self.log_text.tag_config`. Each log entry is stamped with a timestamp in [HH:MM:SS] format and tagged accordingly by message type, making key information (such as errors and completion status) readily apparent when browsing the logs.

[0128] Preferably, a robust multi-threaded processing and exception handling framework is constructed: When the "Start Processing" button is clicked, all buttons are disabled in the main thread to prevent repeated operations, and a daemon thread `threading.Thread(target=self.run_processing, daemon=True)` is created to execute the actual prediction task. All exceptions in the processing thread are caught using try-except, ensuring that the button state is restored at the end regardless of success or failure. Progress updates are safely passed from the child thread to the main thread via the `progress_callback` mechanism (using `self.root.after(0,...)` or directly calling thread-safe methods), avoiding GUI freezing while ensuring thread safety.

[0129] In some embodiments, the batch processing described in S4 adopts a dynamic adaptation mechanism of "number of threads - batch size": ① In CPU mode, the tool obtains the number of physical CPU cores N through the psutil library and automatically sets the maximum number of threads to min(N×0.8,8) to avoid resource contention caused by too many threads; ② For high-resolution core photos with a single image resolution exceeding 4000×3000, the batch size is automatically adjusted from 50 to 20, and an image block loading mechanism is enabled. The single image is divided into 2000×2000 pixel blocks and input into the model for inference. After inference, the block results are stitched together to avoid high-resolution images occupying too much memory; ③ A progress prediction module is built into the batch processing process. Based on the average processing time of the processed images, the number of remaining images, and the current memory usage rate, the remaining processing time is predicted in real time and displayed on the graphical interface. When the memory usage rate exceeds 80%, the processing is automatically paused and the cache is cleared. Processing is resumed after the memory usage rate drops below 60%, ensuring the stability of batch processing.

[0130] In some embodiments, the corner sorting algorithm based on vector geometry described in S4 adds an anti-interference preprocessing step: ① After extracting corner points from the segmentation mask, outlier corner points with coordinate deviations exceeding 3 standard deviations are first removed using a statistical outlier detection algorithm (such as the Z-score algorithm). If the number of outlier corner points exceeds one, the contour of the segmentation mask is re-extracted and the corner points are calculated; ② When calculating the centroid of the quadrilateral, a weighted average method is used, and the corner point weight is positively correlated with the distance from the point to the contour edge to avoid centroid shift caused by edge noise; ③ After sorting, the rationality of the corner point order is verified by the vector cross product. If the cross product signs of adjacent corner points are inconsistent, a secondary sorting is automatically triggered to ensure that the corner points always maintain a clockwise order of top left → top right → bottom right → bottom left, thus solving the problem of corner point sorting disorder in complex backgrounds.

[0131] In some embodiments, S4's memory management adopts a three-stage cleanup mechanism of "preprocessing-inference-postprocessing": ① In the image preprocessing stage, the EXIF ​​data and temporary cache of the original image are released immediately after loading the image, and only the RGB channel data used for inference are retained; ② In the model inference stage, the gradient checkpoint mechanism of torch is enabled, and only the key intermediate feature maps in the inference process are saved, while non-key intermediate data are released in real time; ③ In the postprocessing stage, the original corner coordinates, transformation matrix and other temporary data are deleted immediately after the perspective transformation is completed. The cropped image adopts an incremental saving mode, and 10 images are written to the disk in batches after processing, instead of saving one image in real time, reducing the memory occupied by disk I / O operations; ④ For GPU mode, the tool monitors the video memory usage periodically. When the video memory usage exceeds 90%, the unprocessed images in the current batch are automatically cached to the disk, and the video memory is released before being loaded and processed in batches to avoid video memory overflow causing program crashes.

[0132] In some embodiments, the batch processing described in S4 introduces a dynamic scheduling mechanism for memory-SSD heterogeneous storage: ① During the preprocessing stage, the physical memory usage is monitored in real time using the psutil library. When the usage rate is >70%, the original data of the images to be processed are automatically cached to a temporary partition of the SSD (using the LZ4 compression algorithm to reduce storage usage), and only image thumbnails are loaded for preprocessing; ② During the inference stage, image data is read in the order of "memory first, then SSD". After the inference is completed, the original image data is released immediately, and only the segmentation mask and corner coordinates are retained; ③ During the postprocessing stage, when the video memory usage rate is >85%, the intermediate matrix of perspective transformation is cached to the SSD and released in batches after the batch processing is completed. Meanwhile, a lightweight GAN edge repair step is added after perspective transformation: ① Using the MobileGAN architecture, the image after perspective transformation is used as input. The generator repairs the problems of blurred edges and texture breaks in black-filled areas, while the discriminator only focuses on the authenticity of details within 5px of the core box edge; ② Prior knowledge of core texture (such as bedding direction and grain size statistical features) is introduced during the repair process to ensure that the repaired edges are consistent with the actual texture of the core. The edge clarity of the corrected image is improved by more than 30%.

[0133] In some embodiments, the corner sorting algorithm based on vector geometry described in S4 adds a double geometric constraint anti-interference preprocessing: ① After corner extraction, the four edge lines of the core box are fitted using the RANSAC algorithm, and the slope and intercept of each line are calculated. Abnormal corners with a deviation from the line fitting error > 2px are removed; ② The optimal quadrilateral is selected using the "rectangularity-AspectRatio" double constraint: the rectangularity (the ratio of the quadrilateral area to the area of ​​the minimum bounding rectangle) and aspect ratio of all candidate quadrilaterals are calculated, and only quadrilaterals with a rectangularity > 0.9 and an aspect ratio between 1.5 and 5.0 (the standard aspect ratio range of the core box) are retained to exclude erroneous segmentation areas caused by background interference; ③ After sorting, the cross product of vectors and the distance from the point to the line are used for double verification: the cross product sign of adjacent corners must be consistent (ensuring clockwise order), and the distance from each corner to the corresponding edge line must be < 3px, otherwise a second sorting is triggered.

[0134] Step S5: After creating a graphical user interface using Tkinter, use Anaconda to package and create an executable program (.exe).

[0135] Preferably, to facilitate use by geologists, a graphical user interface (GUI) is created using the Tkinter library. Tkinter is Python's standard GUI library, providing functionality for creating windows, buttons, text boxes, and other GUI elements. The Tkinter library uses the `command` attribute to bind corresponding event functions when defining components, making it concise. The front-end primarily uses three components: selecting the path to the core image to be processed, and running auxiliary cropping. The automatic cropping component encapsulates steps S2-S4 mentioned above.

[0136] Preferably, when the code imports a large number of third-party libraries, using the pyinstaller command in the command prompt (CMD) to package Python into an exe file often results in a mismatch between the Pyinstaller version and the Python version or the third-party library version. It is usually difficult to package Python code into an exe executable file. However, using Anaconda for packaging can solve the compatibility and matching problems of various function libraries, and the exe executable file generated in the Anaconda virtual environment is relatively small.

[0137] The core advantage of separating the .pt model file from the .exe package lies in the efficiency of updates and maintenance: First, separating the model from the code allows users to easily replace or upgrade the model file without repackaging the .exe, which is particularly suitable for core identification scenarios where different mining areas and lithological types may require targeted fine-tuning of the model; second, it significantly reduces the size of the .exe installation package, avoiding the difficulties in distribution and download caused by bundling model files of hundreds of MB or even GB; finally, this architecture supports a hot update mechanism, so when the model is optimized and iterated, only the .pt file needs to be updated remotely to upgrade the algorithm, without requiring users to reinstall the entire application.

[0138] This technical solution is an industrial-grade solution with a complete product form. It not only realizes end-to-end automation of the process from data annotation and intelligent analysis to result output, but also integrates engineered memory optimization, batch processing, and user interaction modules. It can be directly deployed in actual exploration scenarios for instance segmentation and geometric correction of large-scale core box images. The solution includes standardized installation configuration, graphical parameter settings, real-time processing monitoring, and result visualization output, forming a complete and stable software product that can reliably serve the production practice of geological exploration, rather than just a technical prototype remaining in the laboratory verification stage.

[0139] In some embodiments, the graphical user interface described in S5 adds intelligent auxiliary functions: ① The model selection module has a built-in model performance preview function. After the user selects a YOLOv8-seg model (n / s / m / l / x) of different sizes, the interface automatically displays the inference speed, memory usage, and accuracy reference value (based on the validation set test results) of the model, helping the user select an appropriate model according to their own hardware conditions and processing needs; ② The processing parameter configuration module supports a "Quick Scene Selection" drop-down menu, which includes four preset configurations: "Standard Core Repository Scene", "Slightly Distorted Field Scene", "Severely Distorted Field Scene", and "High-Resolution Image Scene". After the user selects, the corresponding confidence level, enhancement parameters, and other configurations are automatically filled in, without the need for manual adjustment; ③ The processing result display module supports dual-screen comparison display of the original image and the processed image. The user can zoom the two images synchronously using the slider, or click the "Difference Annotation" button on the comparison interface to automatically mark the boundary of the cropping range with a red box, and intuitively view the correction and cropping effects.

Claims

1. An automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation, characterized in that, include: S1: Use digital equipment to photograph the core box and collect high-resolution images of the core box, including adjacent core boxes, rotation or distortion, without the need for marking and preprocessing of the core box; S2: Perform polygon point sequence annotation on the high-definition core box images collected in S1. During the annotation process, automatically correct the EXIF ​​rotation information of the images. Divide the annotated images into training and validation sets, and export the dataset and configuration file adapted to the YOLOv8-seg model. S3: Based on the YOLOv8-seg model, load the dataset and configuration file exported by S2, automatically select CPU or GPU devices according to the hardware environment, set training parameters and enable data augmentation strategies, and verify the model performance through the validation set after training to obtain the optimal segmentation model. The model training employs a Bayesian optimization-dynamic loss weight adaptive strategy instead of a fixed loss weight configuration. The validation set mAP@0.5 is used as the objective function. The Bayesian optimization algorithm dynamically searches for box loss weights (range 3-7), CLS loss weights (range 0.5-2), DFL loss weights (range 0.5-2), and mask loss weights (range 2-6), updating the weight combination every 10 rounds. During training, the core box size distribution is monitored in real-time: small sizes are less than 300px, medium sizes are 300px-600px, and large sizes are greater than 600px. When the validation recall of a certain size sample is less than 85%, the corresponding feature enhancement branch is automatically activated. Small-sized samples have increased shallow feature convolution kernels, while large-sized samples have added deep feature attention modules. The data augmentation strategy incorporates mineral texture transfer technology, using a style transfer network to transfer core textures of different mineral types to the training samples, simulating the diversity of core materials in real geological exploration. S4: Input the core box image to be processed into the optimal segmentation model obtained in S3 to obtain the core box segmentation mask. Extract the minimum circumscribed quadrilateral and the coordinates of the four corner points from the mask. Use a sorting algorithm based on vector geometry to determine the clockwise order of the corner points. Based on the sorted corner point coordinates, use perspective transformation to map the irregular quadrilateral region into a regular rectangle. Crop the corrected image and retain the region containing only one set of horizontal, distortion-free core boxes and save it. The process involves extracting the minimum bounding quadrilateral of the segmentation mask, first calculating the average coordinates of the four corner points of the quadrilateral as the centroid; then calculating the vector from each corner point to the centroid, and calculating the angle of each vector using the arctangent function of the vector; sorting all corner points by angle from smallest to largest, then calculating the sum of the x and y coordinates of each sorted corner point, and finding the corner point with the smallest sum as the top-left corner point; finally, through a cyclic shift operation, adjusting the corner point order to a clockwise order of top-left, top-right, bottom-right, bottom-left to ensure the consistency of the corner point order.

2. The automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, It also includes S5: a graphical user interface built using the Tkinter library, integrating the functional modules of S2 to S4, and using Anaconda to encapsulate the entire method into an executable program, supporting batch image import, parameter configuration, and processing progress monitoring.

3. The method for automatic correction and cropping of core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, The high-definition core box images described in S1 support JPG, JPEG, PNG, BMP, TIFF formats and corresponding uppercase formats. There is no need to avoid adjacent core boxes when shooting, and the core box is allowed to be in a non-horizontal or curved distortion state.

4. The method for automatic correction and cropping of core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, When annotating in S2, clicking on the four corner points of the core box closes to form a polygon. The annotation coordinates are converted into the original image coordinates by calculating the scaling ratio between the displayed image and the original image, subtracting the canvas offset, and the converted coordinates are restricted to the valid range of the image. It supports persistent saving and repeated editing of annotated data.

5. The automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, The training set and validation set ratio described in S2 is 80%:20%. The exported dataset includes image files and corresponding label files. The label files normalize the coordinates of polygon annotation points to relative coordinates within the range of [0,1]. The configuration file contains the dataset path, number of categories, and category name information, which is adapted to the training requirements of the YOLOv8-seg model.

6. The automatic correction and cropping method for core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, The data augmentation strategies described in S3 include perspective transformation, rotation within a 10° range, 0.5x scaling, and color space transformation. The training parameters include a default training epoch number of 50, an image size of 640, and a batch size of 16. An early stopping mechanism is enabled, which automatically stops training when the validation metric fails to improve for 30 consecutive epochs. Meanwhile, intermediate checkpoints are saved every 5 epochs.

7. The method for automatic correction and cropping of core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, The perspective transformation described in S4 uses the INTER_LINEAR interpolation method to automatically calculate the output size and add a 10% margin. The blank areas after the perspective transformation are filled with black. The output size does not exceed twice the size of the original image. The cropped image is labeled according to the original image naming rules and then saved to the specified folder.

8. The method for automatic correction and cropping of core images based on YOLOv8-seg and perspective transformation according to claim 1, characterized in that, In S4, core box images to be processed are processed in batches. A fixed batch size of 50 images is used for processing. After each batch is processed, memory is automatically cleared to avoid memory overflow. Memory usage is monitored in real time during processing, and the processing results are automatically associated with the original image paths for traceability and comparison.

9. The method for automatic correction and cropping of core images based on YOLOv8-seg and perspective transformation according to claim 2, characterized in that, The graphical user interface described in S5 supports the selection of the optimal segmentation model file obtained in S3, import of the image directory to be processed, and setting of confidence parameters. It has a real-time display function for processing logs, which are color-coded to distinguish between errors, successes, and progress. It also displays the processing progress, total time consumption, and memory usage.

Citation Information

Patent Citations

  • Drilling image intelligent pickup method and system based on deep learning

    CN119229118A

  • Core photograph processing method based on perspective transformation and graphical user interface

    CN120371188A