Core photo processing method based on perspective transformation and graphical user interface
By using a perspective transformation and graphical user interface-based approach, and leveraging Python and OpenCV libraries, core images are automatically corrected and cropped. This solves the problem of low processing efficiency in core images during geological exploration, enabling fast and accurate batch processing and bending deformation correction.
Patent Information
- Application Number
- CN202510462832.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-14
- Publication Date
- 2026-01-23
- Estimated Expiration
- 2045-04-14
AI Technical Summary
Mechanical rotation, alignment, and cropping of core images in geological exploration are inefficient. Traditional methods are time-consuming and labor-intensive, and existing automated processing methods require pre-processing of markings and cannot correct bending deformations.
A perspective transformation and graphical user interface (GUI) approach was adopted. The GUI was defined using Python's Tkinter library, and the coordinates of the four corner points of the core image were obtained through mouse interaction. Perspective transformation and cropping were then performed, and automatic correction and cropping were achieved by combining the OpenCV library.
Core images can be processed quickly and accurately in batches without the need for pre-processing markings, improving processing efficiency, adapting to curved and deformed images, shortening processing time, and enhancing the applicability and practicality of the automated program.
Smart Images

Figure CN120371188B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a method for processing core photographs based on perspective transformation and a graphical user interface. Background Technology
[0002] In the field of geological exploration, according to the "Regulations for Original Geological Logging of Solid Mineral Exploration (DZT0078-2015)," all core boxes that have completed inspection and organization must be digitally photographed and archived one by one, with each image showing only one set of horizontal core boxes. However, core boxes in the core repository are usually arranged in dense rows, so during the photography process, adjacent core boxes inevitably come into play. Furthermore, not only are the angles of each core box different, but when core boxes are placed near drilling rigs in the field, they may be bent or deformed due to uneven ground. The traditional approach is for geological workers to take photos of the core boxes in the core repository and then manually rotate, straighten, correct, and crop all the photos using image processing software (such as Photoshop) on a computer to ensure that each photo contains only one set of horizontal, undeformed core boxes. However, in geological exploration work involving a large number of deep boreholes, this manual mechanical processing is extremely time-consuming and labor-intensive. Taking the geological exploration project of the Julong Copper Mine in Tibet as an example, the drilling depth can usually reach more than 1,000 meters, while each core box can usually only hold a core of about 5 meters. This means that each borehole needs to process 200 to 300 core photos.
[0003] For a large number of core images to be processed, geologists traditionally use software such as Photoshop or Windows' built-in image viewer to manually rotate, straighten, and crop each core image before saving it. This method is not only inefficient and inaccurate, but also lacks consistency in processing a large number of core images. While the patent "An Automated Processing Method for Core Images Based on Template Matching and Hough Transform" can automate the processing of core images, including automatic rotation, straightening, and cropping, this method requires marking the core boxes before automated processing for template matching by the automated program. This process consumes a significant amount of geologists' time and cannot correct bent or deformed core boxes, posing challenges for practical application and promotion. Summary of the Invention
[0004] This invention provides a core image processing method based on perspective transformation and a graphical user interface, which solves the problems of repetitive mechanical labor in the daily work of geologists and the need for preprocessing of core boxes in the field in previous technical solutions. The aim is to provide geologists with a fast and accurate method for correcting bending deformation and cropping a large number of core images without the need for preprocessing of core boxes, based on a graphical user interface library.
[0005] To achieve the above objectives, the present invention adopts the following technical solution:
[0006] This specification discloses a method for processing core photographs based on perspective transformation and a graphical user interface, including:
[0007] S1. Collect high-resolution images (i.e., core photographs) of adjacent core boxes;
[0008] S2. Define and design the Application class based on the Python Tkinter graphical user interface library to build graphical user interfaces;
[0009] S3. Define and design the Application.selectFile custom function to select core images in batches, use the tkinter.filedialog.askopenfilenames function to obtain the file paths of all core images to be processed in batches, and save the file paths in the Application.pathImage variable;
[0010] S4. Define and design the Application.interactivePerspectiveWarp custom function to obtain the four corner points of the core image selected in S3, perform perspective transformation, and then save it;
[0011] S5. Based on the Python graphical user interface Tkinter library, the tkinter.button function creates a "Batch Select Photos" button and an "Interactive Perspective Transformation" button. The "Batch Select Photos" button is bound to the Application.selectFile custom function in S3, which is used to select photos. The "Interactive Perspective Transformation" button is bound to the Application.interactivePerspectiveWarp custom function in S4, which is used to obtain the four corner points of the core photo and perform perspective transformation.
[0012] S6. Use Anaconda to package S2 to S5 and create exe executable files.
[0013] In this manual, in step S4, the coordinates of the four corner points of the core image to be corrected and cropped are first obtained using the graphical user interface. Then, the coordinates of the four corner points of the target image of the core image are calculated based on the coordinates of the four corner points of the source image of the core image. Next, the perspective transformation of the core image is performed using the cv2.getPerspectiveTransform and cv2.warpPerspective functions. Finally, the processed core image is saved using the cv2.imencode function.
[0014] In this specification, S2 includes:
[0015] S21. Create an Application class with Frame as the class parameter, and define the __init__ constructor;
[0016] Create an Application class that inherits from tkinter.Frame. The Application class can utilize the graphical interface features in the Tkinter library, enabling it to create and manage buttons, labels, and other Tkinter widgets.
[0017] S22. Design the __init__ constructor of the Application class. In this constructor, use super().__init__(master) to call the parent class's constructor. At the same time, create the Application.locationList variable to store the coordinates of the four corner points of the core image obtained through mouse interaction in the graphical user interface and displayed in the image display window. Create the Application.pathImage variable to store the storage path of the core image to be processed. Create the Application.scale variable to set the scaling ratio of the core image in the image display window.
[0018] S23. In the main function, first, the tkinter.Tk class is instantiated as an object root. The tkinter.Tk.geometry function is used to set the size and position of the graphical user interface for the root object, and the tkinter.Tk.title function is used to set the title of the graphical user interface. Next, the Application class is instantiated as an object app and the master=root parameter is passed. Finally, the tkinter.Tk.mainloop function is used to start the Tkinter event loop, which allows the user to interact with the application in real time by continuously listening for events.
[0019] In this manual, a flag variable self.selectImageFlag is created in the __init__ constructor and initialized to Flase, which is used to determine whether the Application.selectFile custom function has been successfully executed.
[0020] In this manual, in S3, within the custom function Application.selectFile, after calling the function tkinter.filedialog.askopenfilenames, it is necessary to determine the length of the Application.pathImage list variable. If the list is not empty, it means that the user has successfully selected the core image to be processed, and the Application.selectImageFlag flag is set to True.
[0021] In this specification, S4 includes:
[0022] S41. Determine whether S3 has completed the selection of core images to be processed;
[0023] S42. If the selection of the core image to be processed is completed, i.e., the Application.selectImageFlag flag variable is True, the tkinter.filedialog.askdirectory function is used to obtain the storage location of the target image of the cropped core image, and the storage location is saved to the local variable SavePath in the Application.interactivePerspectiveWarp custom function. The length of the local variable SavePath is checked to determine whether the storage path of the cropped core image has been set. If the variable SavePath is empty, the tkinter.messagebox.showinfo function is called to prompt the user that no storage path of the target image of the core image has been selected, and the function is exited using return.
[0024] S43. Use the tkinter.simpledialog.askstring function to prompt the user to enter a value to assign a value to the Application.scale variable, which represents the scaling ratio;
[0025] S44. Iterate through the Application.pathImage variable in S3, open the core image using the cv2.imdecode function, and for each core image to be processed, save the unscaled core image to the Application.imgOriginCopy variable. Using the scaling ratio obtained in S43, the Application.scale variable uses the cv2.resize function to scale the source image of the current core image to be processed in the graphical user interface, and save the current scaled core image to be processed to the Application.imgScale and Application.imgScaleCopy variables. The Application.imgScale variable is used to display the core image thumbnail in the image display window; the Application.imgScaleCopy variable is used to display the echo in the image display window after the user performs an undo operation; the Application.imgOriginCopy variable is used to perform perspective transformation on the higher resolution unscaled core image.
[0026] S45. Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handler to monitor mouse operations, and implement the following in the Application.onmouse custom mouse event handler: four left mouse clicks to visually present the correction and cropping range, right mouse click to implement the undo function to reposition the cropping range, and Ctrl + left mouse click to rotate the core image.
[0027] S46. Use the cv2.waitKey and cv2.destroyAllWindows functions to terminate the mouse monitoring of the current core image to be processed by the Application.onmouse custom mouse event handling function;
[0028] After the user locates the four corner points with the left mouse button, the expected automatic correction and cropping range is displayed on the graphical user interface. If the previewed cropping range meets expectations, the user can press any key to close the thumbnail of the current core photo to end the processing of the current core photo. At this time, the coordinates of the four corner points obtained by the left mouse button in S45 have been added to the Application.locationList list variable for subsequent perspective transformation and cropping of the core photo.
[0029] S47. Using the four pixels representing the thumbnail correction and cropping range in the image display window stored in the Application.locationList list variable, reverse scaling is performed to obtain the source pixel coordinates of the unscaled core photo to be corrected and cropped. The four target pixel coordinates of the corrected and cropped core photo are constructed. The cv2.getPerspectiveTransform and cv2.warpPerspective functions are used to perform perspective transformation on the Application.imgOriginCopy variable. The cv2.imencode function is used to store the target image of the cropped core photo.
[0030] In this manual, in section S45, the automatic correction and cropping range determined by clicking the four corner points of the core image located by left-clicking is visualized. Geologists determine whether to end the processing of the current core image by judging this range. If the effect meets expectations, press any key to process the next core image. If the previewed cropping range does not meet expectations, it is necessary to click the four corner points again to determine the correction and cropping range again. Then, click the right mouse button to process the current core image again.
[0031] By using the cv2.setMouseCallback function in the OpenCV library, the custom mouse event handler function Application.onmouse is bound to the image display window to monitor the user's mouse operations in real time and perform corresponding image processing tasks according to the operation type.
[0032] In this specification, S45 includes:
[0033] S451. Use the cv2.imshow function to display the scaled core image to be processed using the Application.imgScale variable;
[0034] S452. Use the len function to determine if the length of the Application.locationList variable is 4. If it is 4, it means that the user has completed the selection of the automatic correction and cropping range of the current core photo to be processed. Use the cv2.line function to draw the automatic correction and cropping range by connecting the coordinates of the four corner points obtained by the left mouse click position.
[0035] S453. If only the mouse operation event cv2.EVENT_LBUTTONDOWN is detected, it means that the user is using the left mouse button to click. At this time, use the append function to add the latest corner pixel coordinates obtained by positioning to the Application.locationList list variable;
[0036] S454. If mouse operation events cv2.EVENT_FLAG_CTRLKEY and cv2.EVENT_LBUTTONDOWN are detected, it means that the user is using Ctrl+left mouse button click operation. At this time, use the cv2.rotate function to rotate the Application.imgScale, Application.imgOriginCopy and Application.imgScaleCopy variables 90° clockwise respectively.
[0037] S455. If a mouse operation event cv2.EVENT_RBUTTONDOWN is detected, it indicates that the user is using a right-click operation. At this time, the Application.locationList variable is set to an empty list. At the same time, the backed-up Application.imgScaleCopy variable is assigned to the Application.imgScale variable in the image display window, which has already been used to draw the preview range on the image using the cv2.line function, thus achieving the echo.
[0038] In this specification, step S47 obtains the coordinates of the four corner points of the core image after scaling by using the custom mouse event handler function Application.onmouse, and then performs reverse scaling using the Application.scale variable to obtain the coordinates (tl, tr, br, bl) of the four corner points of the source image of the core image. The width and height values of the target image of the core image are calculated using (tl, tr, br, bl), specifically by selecting the maximum horizontal difference and the maximum vertical difference as the values for calculating the width and height of the target image. Based on the coordinates of the four corner points of the source image and the width and height values of the target image, the coordinates of the four corner points of the target image are calculated. The perspective transformation matrix is obtained using the cv2.getPerspectiveTransform function. The perspective transformation is performed on the core image to be processed using the cv2.warpPerspective function, thereby achieving automatic correction and cropping functions.
[0039] In this specification, S47 includes:
[0040] S471. The four coordinate points stored in the Application.locationList variable are reverse-scaled using the Application.scale variable to obtain (tl, tr, br, bl). The specific calculation of the four target coordinate points (tlNew, trNew, brNew, blNew) of the cropped core image is as follows:
[0041] The maximum value of the horizontal difference is selected as the width value of the target image after cropping the core photograph;
[0042]
[0043] widthNew=max(widthTop,widthBottom);
[0044] The maximum vertical difference is selected as the height value (heightNew) of the cropped core image.
[0045]
[0046] heightNew=max(heightRight,heightLeft);
[0047] Based on the width and height values of the target image of the cropped core photograph, the coordinates of four points in the target image of the cropped core photograph can be obtained;
[0048] tlNew=(0,0)
[0049] trNew = (widthNew - 1, 0)
[0050] brNew=(widthNew-1,heightNew-1)
[0051]
[0052] S472. Calculate the perspective transformation matrix M using the cv2.getPerspectiveTransform function;
[0053] The mathematical form of perspective transformation is a 3x3 transformation matrix:
[0054]
[0055] The mapping relationship between a point (tl_x, tl_y) in the source image of the core photograph and a point (tlNew_x, tlNew_y) in the target image of the core photograph is expressed as follows:
[0056]
[0057] After unfolding, we get:
[0058]
[0059]
[0060] For each pair of corresponding points (x) i ,y i ) and (x' i ,y' i And, construct the following two equations:
[0061] x' i (h 31 x i +h 32 y i +h 33 ) = h 11 x i +h 12 y i +h 13
[0062] y i '(h 31 xi+h 32 yi+h 33 ) = h 21 xi+h 22 yi+h 23 ;
[0063] Rearrange the above system of equations into a linear system of equations:
[0064]
[0065] S473. Use the cv2.warpPerspective function to perform perspective transformation and cropping on the Application.imgOriginCopy variable of the high-resolution, unscaled core image;
[0066] S474. Use the cv2.imencode function to store the cropped core image target image.
[0067] In summary, the present invention has at least the following beneficial effects:
[0068] This invention achieves rapid correction and cropping of core images by combining the OpenCV image processing library in Python with the Tkinter graphical user interface library. First, high-resolution images of the core chamber are acquired using a digital device. Then, through interaction with the user via the graphical user interface, images to be processed are selected in batches, and the coordinates of the four corner points of the core image are obtained. Perspective transformation is then used to precisely correct and crop the images.
[0069] Compared to the previous method where geologists manually rotated and cropped an average of 150 core photos per hour using Photoshop, this invention rapidly processes core photos using computer language, significantly improving cropping efficiency. Furthermore, compared to the patent "An Automated Processing Method for Core Photos Based on Template Matching and Hough Transform," this invention eliminates the need for outdoor preprocessing by drawing matching templates on the core box. It also automatically corrects and crops warped core photos through perspective transformation. Therefore, this invention demonstrates strong adaptability and ease of implementation in addressing the target problem. By avoiding pre-processing manual operations, it greatly reduces the overall processing time for core photos, representing a mature and robust comprehensive technical solution. In conclusion, this invention enables rapid and accurate batch processing of core photos, avoiding extensive mechanical repetition and pre-processing of the core box in traditional manual processing. This significantly improves the work efficiency of geologists and effectively solves the problem of warped core photos, enhancing the applicability and practicality of the automated program.
[0070] This invention achieves rapid processing of core photographs based on perspective transformation and a graphical user interface. It eliminates the need for outdoor marking preprocessing of the core boxes, thus significantly reducing processing time compared to the patented method "An Automated Processing Method for Core Photographs Based on Template Matching and Hough Transform." Furthermore, while the patented method cannot automatically correct bent or deformed core boxes, the solution employed in this invention, due to its perspective transformation, can quickly correct even bent or deformed core boxes in the photographs. This makes it more suitable for core photographs taken in the field, resulting in wider applicability and greater practicality. Attached Figure Description
[0071] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0072] Figure 1 This is a schematic diagram of the core image processing method based on perspective transformation and graphical user interface involved in this invention.
[0073] Figure 2 This is a schematic diagram showing the core boxes involved in this invention arranged in a dense row in the core storage.
[0074] Figure 3 This is a schematic diagram of the target core box, upper core box, and lower core box involved in this invention.
[0075] Figure 4a , Figure 4b , Figure 4c and Figure 4d All of these are bent and deformed core boxes photographed on uneven outdoor slopes, as described in this invention.
[0076] Figure 5 This is a scaled-down diagram of the core image to be processed involved in this invention.
[0077] Figure 6a and Figure 6b This is a size comparison chart of core images with the same resolution but different scaling ratios (0.2 and 0.25) displayed on a computer screen.
[0078] Figure 7 The automatic correction and cropping quadrilateral range represented by the four pixel coordinates of the left mouse button click location involved in this invention is drawn.
[0079] Figure 8a and Figure 8b , Figure 8c and Figure 8d , Figure 8e and Figure 8f These are before-and-after comparison images of the core photographs involved in this invention.
[0080] Figure 9 The graphical user interface created in this invention. Detailed Implementation
[0081] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0082] like Figure 1 As shown, this embodiment provides a core image processing method based on perspective transformation and a graphical user interface, specifically including the following steps:
[0083] S1: Use digital devices to photograph the core boxes and collect high-resolution images of the core boxes that may contain adjacent core boxes;
[0084] S2: Define and design the Application class based on the Python Tkinter graphical user interface library to create the graphical user interface and initialize related parameters;
[0085] S3: Define and design the Application.selectFile custom function to select core images in batches, use the tkinter.filedialog.askopenfilenames function to obtain the file paths of all core images to be processed in batches, and save the path in the Application.pathImage variable;
[0086] S4: Define and design the Application.interactivePerspectiveWarp custom function to obtain the four corner points of the core photo selected in S3, perform perspective transformation, and save it. First, use the graphical user interface and user mouse interaction to obtain the coordinates of the four corner points of the core photo that needs to be corrected and cropped. Second, calculate the coordinates of the four corner points of the target image of the core photo based on the coordinates of the four corner points of the source image of the core photo. Third, use the cv2.getPerspectiveTransform and cv2.warpPerspective functions to perform perspective transformation on the core photo. Finally, use the cv2.imencode function to encode and save the processed core photo.
[0087] S5: Based on the Tkinter library's tkinter.button function in Python's graphical user interface, create a "Batch Select Photos" button and an "Interactive Perspective Transformation" button. Bind the "Batch Select Photos" button to the Application.selectFile custom function in S3, which is used to select photos, and bind the "Interactive Perspective Transformation" button to the Application.interactivePerspectiveWarp custom function in S4, which is used to obtain the coordinates of the four corner points of the core image and perform perspective transformation.
[0088] S6: Use Anaconda to encapsulate automation processes based on perspective transformation and graphical user interfaces and create executable (exe) files.
[0089] In some embodiments, in S1, since the technical solution is based on perspective transformation and graphical user interface, there is no need to preprocess the core photos; it is only necessary to take photos of the core box using a digital device.
[0090] In some embodiments, in S4, since the resolutions of various digital devices are different, a window for setting the scaling ratio parameter needs to be designed in the graphical user interface to realize the scaling function of the core photo displayed on the computer screen, so as to improve the robustness and applicability of the technical solution.
[0091] In some embodiments, in S4, to improve the robustness and practicality of the technical solution, when obtaining the cropping range of the core photo by clicking the four corner points of the target core box in the core photo in the graphical user interface, the cropping range is displayed with a thick red line to determine whether the automatic correction and cropping range are appropriate.
[0092] In some embodiments, in S4, when it is determined that the trimming range is inappropriate, a withdrawal function is implemented by designing relevant functions so that geological personnel can perform secondary trimming.
[0093] In some embodiments, in S4, when the selected core image is in a state to be rotated due to factors such as the shooting position angle, the core image is rotated by designing relevant functions.
[0094] In some embodiments, in S4, since the cv2.imread function will report an error when reading images with Chinese paths, in order to achieve compatibility with opening core photos with Chinese and English paths, this technical solution uses the cv2.imdecode function for image decoding.
[0095] In some embodiments, in S4, since this technical solution needs to obtain the correction and cropping range of each core photo, a graphical user interface is used to interact with the user's mouse to obtain the coordinate values of the correction and cropping range of the core photo, perform perspective transformation, and save it. The Tkinter standard graphical user interface library is used to encapsulate a series of operations on the core photo in corresponding functions and bind them to corresponding buttons.
[0096] In some embodiments, in S6, a graphical user interface (GUI) is created using the Tkinter library for ease of use by geologists. Tkinter is Python's standard GUI library, providing functionality for creating windows, buttons, text boxes, and other GUI elements. The Tkinter library binds corresponding event functions via the `command` attribute when defining components, making it concise. The front-end of this method primarily uses two buttons: "Batch Rotate Photos" and "Interactive Perspective Transformation." By encapsulating and packaging these steps using a GUI library, users do not need to pre-install any computer language runtime environment, and the system is independent of external library files, significantly reducing compatibility issues caused by different runtime environments and making the system and method readily available for use by geologists.
[0097] In some embodiments, in S1, a digital device is used to photograph the core box, collecting high-resolution images of the core box. In the field of geological exploration, according to the "Regulations for Original Geological Logging of Solid Mineral Exploration DZT0078-2015," all core boxes that have completed inspection and organization must be digitally photographed and archived one by one. Ultimately, each image can only contain one set of horizontal, undistorted core box photographs (i.e., core photographs). However, core boxes in core repositories are usually arranged in dense rows (…). Figure 2 Therefore, during the filming process, it is inevitable to involve adjacent core boxes. Figure 3 Furthermore, not only are the placement angles of each core box different, but when the core boxes are placed near the drilling rig in the field, they may also be bent and deformed due to the unevenness of the ground surface. Therefore, after taking pictures and collecting images of the core boxes, geologists usually need to use computer image processing software to rotate, straighten, and crop each image of the core boxes.
[0098] Because this technology uses an interactive cropping method based on a graphical user interface and the user's mouse, geologists no longer need to pre-process and mark core boxes in the field. They only need to use digital devices to photograph the core boxes and collect high-resolution images. Therefore, this technical solution can significantly reduce the time geologists spend processing core photos. Furthermore, since the graphical user interface displays thumbnails of the core photos to be processed, users only need to use the mouse to define the correction and cropping range. This technology can calculate the coordinates of the four corner points of the target image of the core photo based on the coordinates of the four corner points of the source image, and then automatically correct and crop the core photo through perspective transformation. Therefore, this technical solution not only eliminates the need for a combination of techniques such as HSV masking, Hough transform, Canny edge detection, and template matching to process core photos, but also eliminates the need for outdoor marking and pre-processing of core photos before automated processing. The overall logic of the technical solution is clear, easy to implement, and highly applicable.
[0099] In some embodiments, since this technical solution is based on perspective transformation and a graphical user interface, compared with template matching technology, it is not necessary to determine a matching template and draw it in a specific position in the core box in the field for computer program recognition, which greatly shortens the overall processing time of core photos.
[0100] In some embodiments, when the core box photographed in the field is located on a slope or uneven ground, the core photographs will appear distorted. Figure 4a , Figure 4b , Figure 4c and Figure 4dThis technical solution calculates the coordinates of the four corner points of the target image of the core photo based on the graphical user interface, and then uses perspective transformation to correct and crop the core photo. Therefore, it not only eliminates the need for marking and matching templates for the core box in the field, but also enables automatic correction and cropping of bent and deformed core photos.
[0101] In some embodiments, in S2, the Tkinter graphical user interface library based on Python defines and designs the Application class to establish the graphical user interface and initialize related parameters.
[0102] S21: Create an Application class with Frame as the class parameter and define the __init__ constructor.
[0103] Create an Application class that inherits from tkinter.Frame. The Application class can utilize the graphical interface features in the Tkinter library, enabling it to create and manage buttons, labels, and other Tkinter widgets.
[0104] The __init__ constructor is a special method in Python used to initialize newly created class objects. When a new object of the Application class is created, the __init__ method is automatically called to automatically perform necessary initialization operations when the object is created, such as setting default parameters and creating data structures.
[0105] S22: Design the `__init__` constructor for the `Application` class. This constructor calls the parent class's constructor using `super().__init__(master)`. Simultaneously, create the `Application.locationList` variable (used in S45 to store the coordinates of the four corner points of the core image (scaled and displayed in the image window via mouse interaction in the graphical user interface), create the `Application.pathImage` variable (used in S3 to store the storage path of the core image to be processed selected by the user), and create the `Application.scale` variable (used in S43 to set the scaling ratio of the core image in the image display window).
[0106] S23: In the main function, first, the tkinter.Tk class is instantiated as an object root. The tkinter.Tk.geometry function is used to set the size and position of the image user interface for the root object, and the tkinter.Tk.title function is used to set the title of the image user interface. Next, the Application class is instantiated as an object app and the master=root parameter is passed. Finally, the tkinter.Tk.mainloop function is used to start the Tkinter event loop, allowing the user to interact with the application in real time by continuously listening for events.
[0107] `Tk` is the core class in the Tkinter library, used to create the application's main window. Instantiating the `tkinter.Tk` class creates the main window object `root`, which acts as the container for the entire application, holding all other controls. The `Application` class is a custom class that inherits from `Tkinter.Frame`. By instantiating the `Application` class and passing the parameter `master = root`, an instance of the `Application` class, `app`, is associated with the main window `root`. This means that all controls in the `app` object will be placed within the main window `root`.
[0108] In some embodiments, to improve program robustness and avoid the exception that the user has not selected a save path for the core image to be processed when processing the core image in S4, a flag variable self.selectImageFlag is created in the __init__ constructor and initialized to Flase. This flag is used to determine whether the Application.selectFile function has been successfully executed, that is, to determine whether the user has selected the core image to be processed.
[0109] In some embodiments, the parameter of the tkinter.Tk.geometry("length x width + X coordinate + Y coordinate") function in S23 of this implementation scheme is 400x150+1000+200, which means that the length and width of the graphical user interface are 400px and 150px respectively, and the window is located at (1000px, 20px) on the computer screen. The specific parameters of this function can be modified according to personal preferences.
[0110] In some embodiments, in S3, a custom function Application.selectFile is defined and designed to use the tkinter.filedialog.askopenfilenames function to obtain the save path of all core images to be processed in batches, and save the path in the Application.pathImage list variable.
[0111] The `Application.pathImage` list variable is used to store the core images to be processed selected in batches by the `tkinter.filedialog.askopenfilenames` function. This is used to iterate through each core image during subsequent core image processing operations in S44.
[0112] In some embodiments, to improve the robustness of this technical solution, in the Application.selectFile custom function, after calling the tkinter.filedialog.askopenfilenames function, it is necessary to determine the length of the Application.pathImage list variable. If the list is not empty, it means that the user has successfully selected the core image to be processed, and the Application.selectImageFlag flag is set to True.
[0113] In some embodiments, in S4, the custom function `Application.interactivePerspectiveWarp` is defined and designed to obtain the coordinates of the four corner points of the core image selected in S3, perform perspective transformation, and then save it. First, the coordinates of the four corner points of the core image to be cropped are obtained in conjunction with the graphical user interface. Second, the coordinates of the four corner points of the target image of the core image are calculated based on the coordinates of the four corner points of the source image of the core image. Third, the perspective transformation of the core image is performed using the functions `cv2.getPerspectiveTransform` and `cv2.warpPerspective`. Finally, the processed core image is saved using the function `cv2.imencode`.
[0114] S41: Determine whether S3 has completed the selection of core images to be processed.
[0115] The `Application.selectImageFlag` flag in S3 is checked. If it is False, the `tkinter.messagebox.showinfo` function is called to prompt the user "Please complete the step - batch select images" and then the function is exited using `return`. At this time, the `Application.selectImageFlag` flag is still False. The `Application.selectImageFlag` flag will only be set to True when the user correctly selects the save path for the core images to be processed in the custom function `Application.selectFile`.
[0116] S42: If the selection of the core image to be processed is complete, i.e., the Application.selectImageFlag flag variable is True, then the tkinter.filedialog.askdirectory function is used to set the storage location of the cropped core image, and the storage location is saved to the local variable SavePath of the Application.interactivePerspectiveWarp custom function. Then, the len function is used to check whether the length of the local variable SavePath is empty, thereby determining whether the user has set the storage path of the cropped core image. If the local variable SavePath is empty, the tkinter.messagebox.showinfo function is called to prompt the user "No core image target image storage path selected", and the function is exited using return.
[0117] S43: Use the tkinter.simpledialog.askstring function to prompt the user to enter a value to assign a value to the Application.scale variable, which represents the scaling ratio.
[0118] Since it is necessary to locate the four corner points of the core box to be cropped by clicking the mouse in the graphical user interface, it is necessary to display a thumbnail of the core photo to be processed in the graphical user interface so that the user can interact with the core photo to be processed in the image display window using the mouse.
[0119] In some embodiments, to improve the robustness of the technical solution and address the inconsistency in resolution between different users' digital devices, the scaling ratio is set and assigned using the tkinter.simpledialog.askstring function in a pop-up window before interacting with the core image. Figure 5 Users can adjust the scaling ratio appropriately based on the size of the thumbnail in the image display window, so that core photos taken by digital devices with different pixel counts can be displayed at the appropriate size on the screen. The display size of core photos on a computer screen at the same resolution but different scaling ratios is as follows: Figure 6a and 6b As shown.
[0120] In some embodiments, since the scaling ratio is set in a pop-up window using the tkinter.simpledialog.askstring function, to improve the robustness of the technical solution, a ValueError exception is used to capture exceptions where the user inputs non-numeric information. When the user inputs non-numeric information, the tkinter.messagebox.showinfo function is called to prompt the user that "the scaling ratio should be a numeric type and not a string, etc."
[0121] S44: Iterate through the `Application.pathImage` variable in S3, open the core image using the `cv2.imdecode` function, and for each core image to be processed, save the unscaled core image to the `Application.imgOriginCopy` variable. Using the scaling ratio obtained in S43, the `Application.scale` variable is used to scale the source image of the current core image to be processed in the graphical user interface using the `cv2.resize` function. The scaled core image to be processed is then saved to the `Application.imgScale` and `Application.imgScaleCopy` variables. The `Application.imgScale` variable is used in S451 to display the core image thumbnail in the image display window; the `Application.imgScaleCopy` variable is used in S455 to display the image in the image display window after the user performs an undo operation; and the `Application.imgOriginCopy` variable is used in S473 to perform perspective transformation on the higher-resolution unscaled core image.
[0122] Since cv2.imread reads core image files directly from file paths, and file paths may contain Chinese characters or other non-ASCII characters, the encoding of file paths may be incompatible with OpenCV's default encoding in some operating systems (such as Windows) or environments, causing reading failures. Therefore, using the cv2.imread function to read images with Chinese path encodings will result in an error.
[0123] In some embodiments, to achieve compatibility with reading core images with Chinese and English file paths, this technical solution uses the `cv2.imdecode` function for image decoding. The `cv2.imdecode` function does not directly process file paths; instead, it decodes the image data in memory into OpenCV's image format (NumPy array). This design makes it more reliable and flexible when handling paths containing non-ASCII characters. Therefore, the `cv2.imdecode` function bypasses the file system's restrictions on path encoding, thus achieving compatibility with reading core images with Chinese and English file paths.
[0124] In some embodiments, when geologists determine that the cropping range is unsuitable after positioning by clicking the left mouse button and visualizing the quadrilateral range, they can use the right-click undo function implemented in the Application.onmouse custom mouse event handler of S455 to reposition the cropping range. In this case, geologists need to click the left mouse button four times again on the core image where the quadrilateral range has not been drawn to confirm the cropping range. Meanwhile, considering that the original core images are usually high-resolution, they often cannot be displayed well in the image display window without scaling, and different users' digital devices have different resolutions. Therefore, it is necessary to scale the core images in the image display window via S44, and then perform perspective transformation on the unscaled, higher-resolution core images. Therefore, sufficient consideration must be given to backing up the core images to be processed.
[0125] Therefore, since the implementation of this technical solution requires adding secondary positioning of the undo cropping range and scaling the display of the core image to be cropped in the image display window, it is necessary not only to back up the original image of the core image to be processed after scaling so that the user can operate again if they are not satisfied with the cropping range, but also to back up the original image of the core image to be processed without scaling so that the perspective transformation can be performed for correction and cropping at the end. Therefore, it is necessary to decode and read the source image of the core image to be processed using the cv2.imdecode function during the iteration of the Application.pathImage variable, and then back up the unscaled core image to be processed using the os.copy function and name it the Application.imgOriginCopy variable. This is so that perspective transformation can be performed on the higher-resolution unscaled core image. The core image to be processed after being scaled by S44 is named the Application.imgScale variable, and backed up using the os.copy function and named the Application.imgScaleCopy variable. This is because when the undo operation is performed, the Application.imgScale variable has already used the cv2.line function to draw the line segment of the preview effect. At this time, the Application.imgScaleCopy variable is needed to enable the user to re-display the thumbnail of the original image on the computer screen when the undo operation is performed.
[0126] S45: Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handler to continuously monitor mouse operations. In the Application.onmouse custom mouse event handler, implement the following: four left mouse clicks to visually correct the cropping range, right mouse clicks to undo the action and reposition the cropping range, and Ctrl + left mouse click to rotate the core image.
[0127] In some embodiments, to improve the practicality of the technical solution, the automatic correction and cropping range determined by the four corner points of the core photo located by left-clicking the mouse is visualized. Geologists can determine whether to end the processing of the current photo by judging the range. If the effect meets expectations, they can press any key to process the next photo. If the previewed cropping range does not meet expectations, they need to click again to locate, correct, and crop the range. Then, they can right-click the mouse to perform secondary processing on the current core photo.
[0128] In some embodiments, the `cv2.setMouseCallback` function from the OpenCV library is used to bind the custom `Application.onmouse` mouse event handling function to the image display window. This allows for real-time monitoring of user mouse operations within the image display window and the execution of corresponding image processing tasks based on the type of mouse operation. In this technical solution, four left-clicks visually present the corrected cropping range, a right-click undoes the current positioning result for easy repositioning of the cropping range, and Ctrl+left-click rotates the core image.
[0129] S451: Use the cv2.imshow function to display the scaled core image to be processed using the Application.imgScale variable.
[0130] S452: Use the len function to determine if the length of the Application.locationList variable is 4. If it is 4, it means that the user has completed the selection of the automatic correction and cropping range of the current core image to be processed. Use the cv2.line function to draw the automatic correction and cropping range by connecting the coordinates of the four corner points obtained by the left mouse click.
[0131] In some embodiments, to visually reflect the auto-correction and cropping range determined by a left mouse click, the Application.onmouse custom mouse event handler is configured to draw the auto-correction and cropping quadrilateral range represented by the four corner pixel coordinates after four left mouse clicks. Figure 7 This information is used by geologists to determine whether the cutting area is appropriate.
[0132] When the length of the `Application.locationList` variable is 4, it indicates that the user has completed 4 left-click operations in the graphical user interface. If the length of the list variable is not equal to 4, it indicates that the selection of the automatic correction and cropping range for the core image has not yet been completed. It should be noted that only after the left mouse button has been clicked 4 times to locate the pixel will the custom mouse event handler function of `Application.onmouse` enter this conditional statement and use the `cv2.line` function to draw a straight line on the scaled core image to be processed, using the `Application.imgScale` variable. Subsequently, in the next mouse monitoring loop for the current core image to be processed, the automatic correction and cropping range will be visualized using S451.
[0133] S453: If only the mouse operation event cv2.EVENT_LBUTTONDOWN is detected, it means that the user is using the left mouse button. At this time, the append function is used to add the latest corner pixel coordinates obtained by positioning to the Application.locationList list variable.
[0134] It should be noted that when the Application.onmouse custom mouse event handler detects four left mouse clicks, the Application.locationList variable has a length of 4. Therefore, in the loop monitoring of this function, S452 can use the cv2.line function to draw the automatic correction and clipping range by connecting the coordinates of the four corner points obtained from the left mouse click location, so that the user can judge whether the correction and clipping range is suitable.
[0135] S454: If mouse operation events cv2.EVENT_FLAG_CTRLKEY and cv2.EVENT_LBUTTONDOWN are detected, it means that the user is using Ctrl+left mouse button click operation. At this time, use the cv2.rotate function to rotate the Application.imgScale, Application.imgOriginCopy and Application.imgScaleCopy variables 90° clockwise.
[0136] It should be noted that rotating the Application.imgScale variable 90° clockwise is because S451 displays the core image being processed in the current image display window. After the user performs a rotation operation, the rotated core image should be displayed in the image display window. Rotating the Application.imgOriginCopy variable 90° clockwise is because after the core image in the image processing window is rotated 90°, the coordinates of the four corner points that need to be clicked and positioned in S453 also rotate. Therefore, the image undergoing perspective transformation in S47 should also be rotated accordingly. Rotating the Application.imgScaleCopy variable 90° clockwise is because after the user performs an undo operation in S455, it is necessary to clear the automatically corrected and cropped quadrilateral range drawn using cv2.line in S452 without changing the existing rotation angle of the core image in the image. Therefore, this variable is needed to save the image rotation state. In summary, this technical solution achieves the memorization of the current rotation state of the core photos by backing up both the unscaled and scaled core photos. This avoids the need for the core photos in the image display window to be re-rotated after the user clicks the right mouse button to undo the positioning operation. The same principle applies to unscaled core photos.
[0137] In some embodiments, to avoid the core boxes being obscured by sunlight when taking photos of some core boxes, geologists typically choose to stand on the side of the upper core box, the lower core box, or the target core box to take photos, depending on the actual situation. To avoid manually rotating some of the reverse-shot core photos by 90° or 180° later, the rotation of the core photos can be achieved by using Ctrl+left mouse button in the custom mouse event handler of Application.onmouse. This avoids the user having to manually find the core box image that needs to be rotated and aligned from among many core photos and rotate it to the same direction. The user only needs to use Ctrl+left mouse button on the current core photo to complete the 90° clockwise rotation.
[0138] The `cv2.rotate` function in OpenCV is used to rotate images. It only supports rotations in multiples of 90° (such as 90°, 180°, and 270°). Its mathematical principle is based on matrix operations of coordinate transformation, but a simplified implementation is available for rotations of specific angles. The following is a detailed analysis of its mathematical principle:
[0139] Image rotation is essentially a geometric transformation of the coordinate system. For a rotation of any angle θ, the transformation matrix is:
[0140]
[0141] Pixel coordinate transformation formula:
[0142]
[0143] However, `cv2.rotate` only supports rotations in multiples of 90°. Therefore, the formula can be significantly simplified. That is, when the rotation angle is 90, 180, or 270°, no interpolation or complex calculations are needed; it can be achieved directly through coordinate exchange and sign flipping. When the parameter in the `cv2.rotate` function is set to `ROTATE_90_CLOCKWISE`, indicating a 90° clockwise rotation, the rotation matrix R is:
[0144]
[0145] S455: If a mouse operation event cv2.EVENT_RBUTTONDOWN is detected, it indicates that the user is using a right-click operation. At this time, the Application.locationList variable is set to an empty list. At the same time, the backed-up Application.imgScaleCopy variable is assigned to the Application.imgScale variable in the image display window, which has already been used to draw the preview range on the image using the cv2.line function, thus achieving the echo.
[0146] It should be noted that when the Application.locationList variable is set to an empty list, in each loop monitoring of the Application.onmouse mouse event handler function, S452 uses the len function to determine that the length of the Application.locationList variable is not equal to 4. Therefore, the automatic correction and cropping range of the current core image to be processed, located by the user through mouse click, will not be visually displayed in the core image to be processed. The backed-up Application.imgScaleCopy variable is assigned to the Application.imgScale variable, thereby realizing the undo operation. Only when the user is satisfied with the cropping range can they press any key on the keyboard to start processing the next core image to be processed.
[0147] S46: Use the cv2.waitKey and cv2.destroyAllWindows functions to terminate the mouse monitoring of the currently processed core image using the Application.onmouse custom mouse event handler.
[0148] After the user locates the four points with the left mouse button, the expected automatic correction and cropping range will be visually displayed on the graphical user interface. If the previewed cropping range meets expectations, the user can press any key to end the processing of the current core image and close the thumbnail of the current core image. At this time, the coordinates of the four corner points obtained by the left mouse button in S45 have been appended to the Application.locationList list variable for subsequent perspective transformation and cropping of the core image.
[0149] The `cv2.waitKey` function is a keyboard binding function used to implement interaction between the keyboard and the image display window. Its parameter represents the waiting time for keyboard input. In this technical solution, this parameter is set to 0, meaning that subsequent program execution will only continue after any key is pressed. The `cv2.destroyAllWindows` function is used to destroy all currently displayed image display windows. This function has no parameters. Since this technical solution only displays the current core image source window at a time, this function can be used to close the current core image window.
[0150] S47: Reverse scaling is performed using the four pixel coordinates representing the cropping range in the core image thumbnail stored in the Application.locationList list variable. The four target pixel positions of the cropped core image target image are calculated and constructed. The perspective transformation of the unscaled Application.imgOriginCopy variable is performed using the cv2.getPerspectiveTransform and cv2.warpPerspective functions. The cropped core image target image is stored using the cv2.imencode function.
[0151] First, based on the coordinates of the four corner points in the core image thumbnail obtained from the custom mouse event handler `Application.onmouse`, and after reverse scaling using the `Application.scale` variable, the coordinates of the four corner points (tl, tr, br, bl) of the core image source image are obtained. The width and height values of the target image of the core image are then calculated using (tl, tr, br, bl), specifically by selecting the maximum horizontal and vertical differences as the values for calculating the target image's width and height. The coordinates of the four corner points of the target image are then calculated based on the aforementioned coordinates of the four corner points of the core image source image and the width and height values of the target image. Second, the perspective transformation matrix is calculated using the `cv2.getPerspectiveTransform` function. Finally, the perspective transformation is performed on the `Application.imgOriginCopy` variable of the unscaled core image to be processed using the `cv2.warpPerspective` function, thereby achieving automatic correction and cropping functions.
[0152] S471: Based on the four coordinate points stored in the Application.locationList list variable, the coordinates of the four corner points (tl, tr, br, bl) of the unscaled core image source are obtained by reverse scaling using the Application.scale variable. The four target coordinate points (tlNew, trNew, brNew, blNew) of the cropped core image target image are then calculated.
[0153] It should be noted that the image displayed in the image display window is a scaled core photograph, while the final image undergoing perspective transformation is an unscaled core photograph. Therefore, to prevent the core photograph from being too large to be displayed in its entirety in the image window, the image is scaled according to the user-defined scaling ratio (Application.scale variable) in S44, and the coordinates of the four corner points are obtained based on the scaled image. Therefore, when performing perspective transformation on the original, higher-resolution, unscaled core photograph, the four corner point coordinates in the Application.locationList variable need to be scaled inversely using the Application.scale variable. For example, after scaling the image by 0.25 times in S44, the four corner point coordinates obtained in the Application.locationList variable stored in S45 need to be scaled inversely, that is, the coordinates need to be multiplied by 4 to reflect the correct corner pixel positions in the unscaled core photograph.
[0154] In some embodiments, the four coordinate points stored in the Application.locationList list variable are reverse scaled using the Application.scale variable to obtain the coordinates of the four corner points (tl, tr, br, bl) of the unscaled core image source. The specific steps for calculating the four target coordinate points (tlNew, trNew, brNew, blNew) of the cropped core image target image are as follows:
[0155] (1) Select the maximum horizontal difference as the width value of the target image after cropping the core photograph.
[0156]
[0157] widthNew=max(widthTop,widthBottom);
[0158] (2) Select the maximum vertical difference as the height value of the target image of the cropped core photograph.
[0159]
[0160] heightNew=max(heightRight,heightLeft);
[0161] (3) Based on the width and height values of the target image of the cropped core photograph, the coordinates of the four points of the target image of the cropped core photograph can be obtained.
[0162] tlNew=(0,0)
[0163] trNew = (widthNew - 1, 0)
[0164] brNew=(widthNew-1,heightNew-1)
[0165] blNew = (0, heightNew - 1);
[0166] It should be noted that since the width and height values of the image are counted starting from 1, while the pixel position index is counted starting from 0, when calculating the 4-point pixel coordinate values of the cropped core image, it is necessary to subtract 1 from the width and height values.
[0167] S472: Calculate the perspective transformation matrix M using the cv2.getPerspectiveTransform function.
[0168] `cv2.getPerspectiveTransform` is a function in OpenCV used to calculate the perspective transformation matrix. Perspective transformation is one of the common geometric transformations in computer vision and image processing. It achieves the perspective effect of an image by transforming the coordinates of its four corner points. Perspective transformation is a non-linear transformation that projects an image from one viewpoint to another. This transformation preserves linearity but does not preserve parallelism, so it is often used for viewpoint transformation, such as correcting a tilted image to a frontal viewpoint.
[0169] The mathematical form of perspective transformation is a 3x3 transformation matrix, commonly known as the homography matrix, which has the following form:
[0170]
[0171] The mapping relationship between a point (tl_x, tl_y) in the source image of the core photograph and a point (tlNew_x, tlNew_y) in the target image of the core photograph can be represented as:
[0172]
[0173] After unfolding, we get:
[0174]
[0175]
[0176] The perspective transformation matrix M has 8 degrees of freedom, therefore at least 4 pairs of point coordinates are needed to solve this matrix. In this embodiment, a graphical user interface is used to interactively locate the coordinates of 4 corner points for calculating the perspective transformation matrix. Thus, for each pair of corresponding points (x... i ,y i ) and (x' i ,y' i ) and , which can be used to construct the following two equations:
[0177] x' i (h 31 x i +h 32 y i +h 33 ) = h 11 x i +h 12 y i +h 13
[0178] y i '(h 31 xi+h 32 yi+h 33 ) = h21 xi+h 22 yi+h 23 ;
[0179] Rearrange the above system of equations into a linear system of equations:
[0180]
[0181] S473: Use the cv2.warpPerspective function to perform perspective transformation and cropping on the Application.imgOriginCopy variable of the high-resolution, unscaled core image.
[0182] `cv2.warpPerspective` is an OpenCV function used to perform perspective transformations. This function remaps a core photograph based on a given perspective transformation matrix `M`, projecting the original image from one viewpoint to another. (Comparison of core photographs before and after cropping is shown below.) Figure 8a and Figure 8b , Figure 8c and Figure 8d , Figure 8e and Figure 8f As shown.
[0183] S474: Use the cv2.imencode function to encode and store the target image of the cropped core photograph.
[0184] In some embodiments, since the cv2.imwrite function will report an error when reading images with Chinese paths, this technical solution uses the cv2.imencode function for image encoding in order to be compatible with core photos stored in Chinese paths.
[0185] S5: Based on the Tkinter library's tkinter.button function, which is a Python graphical user interface, a "Batch Select Photos" button and an "Interactive Perspective Transformation" button are created. The "Batch Select Photos" button is bound to the Application.selectFile custom function in S3, which is used to select photos, and the "Interactive Perspective Transformation" button is bound to the Application.interactivePerspectiveWarp custom function in S4, which is used to obtain the four corner points of the core image and perform perspective transformation.
[0186] Since this technical solution requires obtaining the cropping range of each core image, a graphical user interface (GUI) is used to interact with the user's mouse to obtain the coordinate values of the core image correction and cropping range. To facilitate use by geologists and improve the program's usability, the Tkinter standard GUI library is used to encapsulate the automation program. Therefore, it is necessary to encapsulate a series of operations on the core images into corresponding functions and bind them to corresponding buttons. By creating interactive buttons and binding them to specific function calls, users can perform complex image processing tasks with simple clicks. Furthermore, by encapsulating each function in an independent function, the code coupling is low, facilitating subsequent function expansion and maintenance.
[0187] S6: Using Anaconda, the core image processing method based on the perspective transformation principle and the graphical user interface created based on the Tkinter library are packaged into an executable file (exe).
[0188] In some embodiments, to facilitate use by geologists, a graphical user interface is created using the Tkinter library, such as... Figure 9 As shown. Tkinter is Python's standard graphical user interface library, providing functionality for creating windows, buttons, text boxes, and other GUI elements. The Tkinter library binds corresponding event functions via the `command` attribute when defining components, making it concise. The front-end primarily uses two buttons: "Batch Rotate Photos" and "Interactive Perspective Transformation." By encapsulating and packaging these steps using a graphical user interface library, users can avoid pre-installing any computer language runtime environment and it does not depend on external library files, greatly reducing compatibility issues caused by different runtime environments.
[0189] In some embodiments, 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.
[0190] This invention employs the Python language and primarily utilizes the OpenCV computer vision library and the Tkinter graphical user interface library. It uses the OpenCV image processing library and the Tkinter standard graphical user interface library to achieve rapid and automatic correction and cropping of batches of core photographs, enabling fast and accurate processing of large numbers of images. This method not only avoids a large amount of repetitive mechanical labor, improving the daily work efficiency of geologists, but also enables rapid and automatic correction of bent and deformed core photographs from the field, greatly improving the cropping effect and enhancing the reliability of image processing.
[0191] The embodiments described above are for illustrative purposes only and are not intended to limit the invention. Therefore, any changes in numerical values or substitutions of equivalent elements should still fall within the scope of this invention.
Claims
1. A method for processing core photographs based on perspective transformation and a graphical user interface, characterized in that, include: S1. Collect core photos containing adjacent core boxes. No preprocessing is required for the core photos. Users only need to open the unprocessed core photos in the software. S2. Define and design the Application class based on the Python Tkinter graphical user interface library to build graphical user interfaces; S3. Define and design the Application.selectFile custom function to select core images in batches, use the tkinter.filedialog.askopenfilenames function to obtain the file paths of all core images to be processed in batches, and save the file paths in the Application.pathImage variable; S4. Define and design the Application.interactivePerspectiveWarp custom function to retrieve... S3 selects four corner points of the core image, performs perspective transformation, and saves the image. In S4, the coordinates of the four corner points of the core image that needs to be corrected and cropped are first obtained by combining the graphical user interface. Then, the coordinates of the four corner points of the target image of the core image are calculated based on the coordinates of the four corner points of the source image of the core image. Next, the perspective transformation of the core image is performed by the cv2.getPerspectiveTransform and cv2.warpPerspective functions. Finally, the processed core image is saved by the cv2.imencode function. S4 includes: S41. Determine whether S3 has completed the selection of core images to be processed; S42. If the selection of the core images to be processed is complete, i.e., the Application.selectImageFlag flag variable is True, then use the tkinter.filedialog.askdirectory function to set the storage location of the target image of the cropped core image, and save the storage location to a local variable in the Application.interactivePerspectiveWarp custom function. In SavePath, the length of the local variable SavePath is used to determine whether the save path for the cropped core image has been set. If the local variable SavePath is empty, the tkinter.messagebox.showinfo function is called to prompt the user "No target image save path for core image has been selected", and the function is exited using return. S43. Use the tkinter.simpledialog.askstring function to prompt the user to enter a value to assign a value to the Application.scale variable, which represents the scaling ratio; S44. Iterate through the Application.path Image variable in S3, open the core image using the cv2.imdecode function, and for each core image to be processed, save the unscaled core image to the Application.imgOriginCopy variable. Using the scaling ratio obtained in S43, the Application.scale variable is used to scale the source image of the current core image to be processed in the graphical user interface using the cv2.resize function, and the scaled core image to be processed is saved to the Application.imgScale and Application.imgScaleCopy variables. The Application.imgScale variable is used to display the core image thumbnail in the image display window; the Application.imgScaleCopy variable is used to display the echo in the image display window after the user performs an undo operation; the Application.imgOriginCopy variable is used to perform perspective transformation on the higher resolution unscaled core image. S45. Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handler to monitor mouse operations. In the Application.onmouse custom mouse event handler, implement the following: four left mouse clicks to visually present the correction and cropping range, right mouse click to implement the undo function to reposition the correction and cropping range, and Ctrl + left mouse click to rotate the core image 90° clockwise. S46. Use the cv2.waitKey and cv2.destroyAl Windows functions to end the mouse monitoring of the current core image to be processed by the Application.onmouse custom mouse event handling function; After the user locates the four corner points with the left mouse button, the expected automatic correction and cropping range is displayed on the graphical user interface. If the previewed cropping range meets expectations, the user can press any key to close the thumbnail of the current core photo to end the processing of the current core photo. At this time, the pixel coordinates of the four corner points obtained by the left mouse button in S45 have been added to the Application.locationList list variable for subsequent perspective transformation of the core photo. S47. Using the four pixels representing the thumbnail correction and cropping range in the image display window stored in the Application.locationList list variable, reverse scaling is performed to obtain the source pixel coordinates of the unscaled core photo to be corrected and cropped. The four target pixel coordinates of the corrected and cropped core photo are constructed. The cv2.getPerspectiveTransform and cv2.warpPerspective functions are used to perform perspective transformation on the Application.imgOriginCopy variable. The cv2.imencode function is used to store the target image of the cropped core photo. In S45, the automatic correction and cropping range determined by the four corner points of the core photo located by left-clicking is visualized. Geologists can determine whether to end the processing of the current core photo by judging the range. If the effect is as expected, press any key to process the next core photo. If the previewed cropping range is not as expected, it is necessary to click the four corner points again to determine the correction and cropping range again. Then, click the right mouse button to process the current core photo again. By using the cv2.setMouseCallback function in the OpenCV library, the custom mouse event handler function Application.onmouse is bound to the image display window to monitor the user's mouse operations in real time and perform corresponding image processing tasks according to the operation type. In S47, the coordinates of the four corner points of the core image source (after scaling) are obtained from the custom mouse event handler `Application.onmouse`. After reverse scaling using the `Application.scale` variable, (tl, tr, br, bl) are obtained. The width and height values of the target image are then calculated by selecting the maximum horizontal and vertical differences as the target image's width and height. Based on the coordinates of the four corner points of the source image and the target image's width and height, the coordinates of the four corner points are calculated. The perspective transformation matrix is obtained using the `cv2.getPerspectiveTransform` function. Finally, the `cv2.warpPerspective` function performs a perspective transformation on the core image to achieve automatic correction and cropping. S5. Based on the Tkinter library's tkinter.button function in Python's graphical user interface, a batch photo selection button and an interactive perspective transformation button are created. The batch photo selection button is bound to the Application.selectFile custom function in S3, which is used to select photos. The interactive perspective transformation button is bound to the Application.interactivePerspectiveWarp custom function in S4, which is used to obtain the four corner points of the core photo and perform perspective transformation. S6. Use Anaconda to package S2 to S5 and create exe executable files.
2. The core image processing method based on perspective transformation and graphical user interface according to claim 1, characterized in that, S2 include: S21. Create an Application class with Frame as the class parameter, and define the __init__ constructor; Create an Application class that inherits from tkinter.Frame. The Application class can utilize the graphical interface features in the Tkinter library, enabling it to create and manage buttons, labels, and other Tkinter widgets. S22. Design the __init__ constructor of the Application class. In this constructor, use super().__init__(master) to call the parent class's constructor. At the same time, create the Application.locationList variable to store the coordinates of the four corner points of the core photo obtained through mouse interaction in the graphical user interface and displayed in the image display window after scaling. Create the Application.pathImage variable to save the storage path of the core photo to be processed. Create the Application.scale variable to set the scaling ratio of the core photo in the image display window. S23. In the main function, first, the tkinter.Tk class is instantiated as an object root. The tkinter.Tk.geometry function is used to set the size and position of the graphical user interface for the root object, and the tkinter.Tk.title function is used to set the title of the graphical user interface. Next, the Application class is instantiated as an object app and the master=root parameter is passed. Finally, the tkinter.Tk.mainloop function is used to start the Tkinter event loop, which allows the user to interact with the application in real time by continuously listening for events.
3. The core image processing method based on perspective transformation and graphical user interface according to claim 2, characterized in that, In the __init__ constructor, a flag variable self.selectImageFlag is created and initialized to Flase, which is used to determine whether the Application.selectFile function has been successfully executed.
4. The core photograph processing method based on perspective transformation and graphical user interface according to claim 1, characterized in that, In the custom function `Application.selectFile` in S3, after calling the `tkinter.filedialog.askopenfilenames` function, it is necessary to check the length of the `Application.pathImage` list variable. If the list is not empty, it means that the user has successfully selected the core image to be processed, and the `Application.selectImageFlag` flag is set to `True`.
5. The core photograph processing method based on perspective transformation and graphical user interface according to claim 1, characterized in that, S45 includes: S451. Use the cv2.imshow function to display the scaled core image to be processed using the Application.imgScale variable; S452. Use the len function to determine if the length of the Application.locationList variable is 4. If it is 4, it means that the user has completed the selection of the automatic correction and cropping range of the current core photo to be processed. Use the cv2.line function to draw the automatic correction and cropping range by connecting the coordinates of the four corner points obtained by the left mouse click position. S453. If only the mouse operation event cv2.EVENT_LBUTTONDOWN is detected, it means that the user is using the left mouse button to click. At this time, use the append function to add the latest corner pixel coordinates obtained by positioning to the Application.locationList list variable; S454. If mouse operation events cv2.EVENT_FLAG_CTRLKEY and cv2.EVENT_LBUTTONDOWN are detected, it means that the user is using Ctrl+left mouse button click operation. At this time, use the cv2.rotate function to rotate Application.imgScale, Application.imgOriginCopy and Application.imgScaleCopy variables 90° clockwise respectively. S455. If a mouse operation event cv2.EVENT_RBUTTONDOWN is detected, indicating that the user is using a right-click operation, the Application.locationList variable is set to an empty list. At the same time, the backup Application.imgScaleCopy variable is used to replace the Application.imgScale variable that has already been used to draw the preview range on the image using the cv2.line function in the image display window, thus achieving echo display.
6. The core image processing method based on perspective transformation and graphical user interface according to claim 1, characterized in that, S47 includes: S471. The four coordinate points stored in the Application.locationList list variable are reverse scaled using the Application.scale variable to obtain (tl,tr,br,bl). The four target coordinate points (tlNew,trNew,brNew,blNew) of the cropped core image are calculated as follows: the maximum value of the horizontal difference is selected as the width value widthNew of the target image of the cropped core image. widthNew=max(widthTop,widthBottom); The maximum vertical difference is selected as the height value (heightNew) of the cropped core image. heightNew=max(heightRight,heightLeft); Based on the width and height values of the target image of the cropped core photograph, the coordinates of four points in the target image of the cropped core photograph can be obtained; tlNew=(0,0) trNew = (widthNew - 1, 0) brNew=(widthNew-1,heightNew-1) blNew = (0, heightNew - 1); S472. Calculate the perspective transformation matrix M using the cv2.getPerspectiveTransform function; The mathematical form of perspective transformation is a 3x3 transformation matrix: The mapping relationship between a point (tl_x, tl_y) in the source image of the core photograph and a point (tlNew_x, tlNew_y) in the target image of the core photograph is expressed as follows: After unfolding, we get: For each pair of corresponding points (x) i ,y i ) and (x' i ,y' i And, construct the following two equations: x' i (h 31 x i +h 32 y i +h 33 )=h 11 x i +h 12 y i +h 13 y i '(h 31 xi+h 32 yi+h 33 )=h 21 xi+h 22 yi+h 23 ; Rearrange the above system of equations into a linear system of equations: S473. Use the cv2.warpPerspective function to perform perspective transformation and cropping on the Application.imgOriginCopy variable of the high-resolution, unscaled core image; S474. Use the cv2.imencode function to store the cropped core image target image.
Citation Information
Patent Citations
Method and system for processing core photo
CN102129701A
Image processing method and device, electronic device, and storage medium
CN109285126A
Artificial intelligence rock mass RQD calculation method based on MaskRCNN combined with U-Net
CN116109906A