Core photograph processing method based on perspective transformation and graphical user interface
Through the method based on perspective transformation and graphical user interface, the rapid and accurate correction and cropping of core photos is achieved, which solves the time-consuming and labor-intensive problem of core photos processing in geological exploration, and improves the applicability and practicality of automation programs.
Patent Information
- Application Number
- CN202510462832.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-14
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2045-04-14
AI Technical Summary
In geological exploration, the mechanical rotational alignment and cutting process of core photos are time-consuming and labor-intensive. The existing automated processing methods require pre-processing marks and cannot correct bending deformation.
Using a method based on perspective transformation and graphical user interface, the Tkinter library and OpenCV library of Python can realize batch selection and interactive perspective transformation of core photos, and automatically correct and crop core photos.
The large number of core photos can be quickly and accurately corrected and cropped without pretreatment, which improves processing efficiency, adapts to bending deformation, and reduces the workload of geologists.
Smart Images

Figure CN120371188A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and particularly to a method for processing core photos based on perspective transformation and graphical user interface. Background Art
[0002] In the field of geological exploration, according to the regulations of the "Original Geological Cataloging Procedures for Solid Mineral Exploration DZT0078-2015", for all core boxes that have completed inspection and sorting, digital photos must be taken box by box for archiving, and finally only one set of horizontal core boxes can appear in each image. However, the core boxes in the core library are usually densely arranged in columns, so it is inevitable to involve adjacent core boxes before and after during the shooting process. At the same time, not only the placement angles of each core box are different, but also when the core box is placed near the field drilling rig platform, it will face the situation of bending and deformation of the core box due to the uneven ground surface. The traditional method is that after the geological staff takes photos of the core boxes in the core library, they manually use image processing software (such as Photoshop) on the computer to rotate, straighten, correct, and crop all the core photos to ensure that each photo only contains one set of horizontal and non-bent and deformed core boxes. However, in geological exploration work involving a large number of deep holes, this mechanical manual processing process is very time-consuming and laborious. Taking the geological exploration project of the Tibet Julong Copper Mine as an example, the drilling depth can usually reach more than 1000 meters, and each core box can usually only hold nearly 5m of core, which means that each drilling hole requires processing 200 to 300 core photos.
[0003] For a large number of core photos to be processed, the traditional method of geological personnel is to use software such as Photoshop or the image viewer built into Windows to manually rotate, straighten, crop each core photo one by one and then save it. This processing method is not only inefficient and inaccurate, but also the processing process of a large number of core photos has poor coherence. At the same time, although the patent "An Automatic Processing Method for Core Photos Based on Template Matching and Hough Transformation" can realize the automatic processing of core photos, realize the automatic rotation, straightening and cropping of core photos, but before the automatic processing of core photos, not only appropriate marks need to be made on the core box for template matching by the automatic program, which takes a lot of time of geological personnel, but also the correction effect cannot be achieved for the bent and deformed core boxes, and there are certain difficulties in actual application and promotion. Summary of the Invention
[0004] The present invention provides a method for processing core photos based on perspective transformation and graphical user interface, which solves the problems of mechanical repetitive labor in the daily work of current geological personnel and the need for preprocessing of core boxes in the past technical solutions in the field. The purpose is to provide a method for geological personnel to quickly and accurately correct bending deformation and crop a large number of core photos based on the graphical user interface library without preprocessing the core box.
[0005] To achieve the above object, the present invention adopts the following technical solutions:
[0006] This specification discloses a method for processing core photos based on perspective transformation and graphical user interface, including:
[0007] S1. Collect high-definition core box images (i.e., core photos) containing adjacent core boxes;
[0008] S2. Define and design an Application class based on the Tkinter graphical user interface library of Python to establish a graphical user interface;
[0009] S3. Define and design an Application.selectFile custom function to batch select core photos, use the tkinter.filedialog.askopenfilenames function to batch obtain the file paths of all core photos to be processed, and save the file paths in the Application.pathImage variable;
[0010] S4. Define and design an Application.interactivePerspectiveWarp custom function to obtain the four corner points of the core photos selected in S3, perform perspective transformation, and then save them;
[0011] S5. Create a "Batch Select Photos" button and an "Interactive Perspective Transformation" button based on the tkinter.button function in the Python graphical user interface Tkinter library, bind the "Batch Select Photos" button to the Application.selectFile custom function for selecting photos in S3, and bind the "Interactive Perspective Transformation" button to the Application.interactivePerspectiveWarp custom function for obtaining the four corner points of the core photos and performing perspective transformation in S4;
[0012] S6. Package S2 to S5 using Anaconda and create an exe executable file.
[0013] In this specification, in S4, first, the four corner coordinates of the core photo to be corrected and cropped are obtained in combination with the graphical user interface. Secondly, the four corner coordinates of the target image of the core photo are calculated based on the four corner coordinates of the source image of the core photo. Thirdly, the perspective transformation of the core photo is performed using the cv2.getPerspectiveTransform function and the cv2.warpPerspective function. Finally, the processed core photo is saved using the cv2.imencode function.
[0014] In this specification, S2 includes:
[0015] S21. Create an Application class with the class parameter Frame and define the __init__ constructor.
[0016] By inheriting tkinter.Frame, the Application class can utilize the graphical interface functions in the Tkinter library, enabling it to create and manage buttons, labels, and other Tkinter widgets within it.
[0017] S22. Design the __init__ constructor of the Application class. In this constructor, use super().__init__(master) to call the constructor of the parent class. At the same time, create an Application.locationList list variable to store the four corner coordinates of the core photo scaled in the image display window obtained through mouse interaction in the graphical user interface, create an Application.pathImage list variable to save the storage path of the core photo to be processed, and create an Application.scale variable to set the scaling ratio of the core photo in the image display window.
[0018] In the main function, first, instantiate the tkinter.Tk class as the object root. Use the tkinter.Tk.geometry function to set the size and position of the graphical user interface for the root object, and use the tkinter.Tk.title function to set the title of the graphical user interface. Secondly, instantiate the Application class as the object app and pass the parameter master = root. Finally, use the tkinter.Tk.mainloop function to start the event loop of Tkinter, allowing the user to interact with the application in real time by continuously listening for events.
[0019] In this specification, a flag variable self.selectImageFlag is created in the __init__ constructor and initialized to False, which is used to determine whether the custom function Application.selectFile has been successfully executed.
[0020] In this specification, in S3, in the custom function Application.selectFile, after calling the tkinter.filedialog.askopenfilenames function, it is necessary to judge the length of the Application.pathImage list variable. If the list is a non-empty list, it means that the user has successfully selected the core photos to be processed, and the Application.selectImageFlag flag is set to True.
[0021] In this specification, S4 includes:
[0022] S41. Judge whether S3 has completed the selection of the core photos to be processed;
[0023] S42. If the selection of the core photos to be processed is completed, that is, the Application.selectImageFlag flag variable is True, at this time, use the tkinter.filedialog.askdirectory function to obtain the storage location of the target image of the cropped core photos, and save the storage location to the local variable SavePath in the Application.interactivePerspectiveWarp custom function. Judge whether the storage path of the cropped core photos has been set by whether the length of the local variable SavePath is empty. If the variable SavePath is empty, call the tkinter.messagebox.showinfo function to prompt the user that the storage path of the target image of the core photos has not been selected, and use return to exit the function;
[0024] S43. Use the tkinter.simpledialog.askstring function to prompt the user to input a value to assign a value to the scaling ratio Application.scale variable;
[0025] S44. Traverse the Application.pathImage variable in S3, use the cv2.imdecode function to open the core photos. For each core photo to be processed, save the unscaled core photo to the Application.imgOriginCopy variable. Use the scaling ratio Application.scale variable obtained in S43 and the cv2.resize function to scale and display the source image of the current core photo to be processed in the graphical user interface, and save the currently processed and scaled core photo to the Application.imgScale variable and the Application.imgScaleCopy variable; the Application.imgScale variable is used to display the thumbnail of the core photo in the image display window; the Application.imgScaleCopy variable is used for echo display in the image display window after the user executes the withdrawal operation; the Application.imgOriginCopy variable is used for perspective transformation on the unscaled core photo with higher resolution finally.
[0026] S45. Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handling function to monitor mouse operations, and implement the visualization of the correction and cropping range by clicking the left mouse button 4 times, the withdrawal function by clicking the right mouse button to reposition the cropping range, and the rotation of the core photo by pressing ctrl + left mouse button in the Application.onmouse custom mouse event handling function.
[0027] S46. Use the cv2.waitKey function and the cv2.destroyAllWindows function to end the mouse monitoring of the current core photo to be processed by the Application.onmouse custom mouse event handling function.
[0028] After the user clicks the left mouse button to locate the 4 corner points, visually display the expected automatic correction and cropping range on the graphical user interface. If the previewed cropping range meets the expectations, the user can press any key to end the processing of the current core photo by closing the thumbnail of the current core photo to be processed. At this time, the 4 corner point coordinates obtained by clicking the left mouse button in S45 have been appended to the Application.locationList list variable for subsequent perspective transformation and cropping of the core photo.
[0029] S47. Use the 4 pixel points stored in the Application.locationList list variable, which represent the thumbnail correction and cropping ranges in the image display window, to perform inverse scaling processing to obtain the source pixel coordinate positions of the un-scaled core photo to be corrected and cropped. Construct 4 target pixel coordinate positions for the corrected and cropped core photo. Use the cv2.getPerspectiveTransform function and the cv2.warpPerspective function to perform perspective transformation on the Application.imgOriginCopy variable, and use the cv2.imencode function to store the target image of the cropped core photo.
[0030] In this specification, in S45, the automatic correction and cropping range determined by the 4 corner points of the core photo located by left-clicking the mouse is visually presented. Geological personnel decide whether to end the processing of the current core photo based on the judgment of this range. If the effect meets the expectation, press any key to process the next core photo. If the previewed cropping range does not meet the expectation and it is necessary to re-click to locate the 4 corner points to determine the correction and cropping range again, then right-click the mouse to process the current core photo again;
[0031] Through the cv2.setMouseCallback function in the OpenCV library, bind the Application.onmouse custom mouse event handling function to the image display window, monitor the user's mouse operations in real time, and execute 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 photo to be processed, the Application.imgScale variable;
[0034] S452. Use the len function to determine whether the length of the Application.locationList list variable is 4. If it is 4, it indicates 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 connect the 4 corner point coordinates obtained by left-clicking the mouse in sequence to draw the automatic correction and cropping range;
[0035] S453. If only the mouse operation event `event` is detected as `cv2.EVENT_LBUTTONDOWN`, it means the user is clicking with the left mouse button. At this time, use the `append` function on the `Application.locationList` list variable to append the corner pixel coordinates obtained from the latest positioning to the list;
[0036] S454. If the mouse operation event `event` is detected as `cv2.EVENT_FLAG_CTRLKEY` and `cv2.EVENT_LBUTTONDOWN` at the same time, it means the user is clicking with the left mouse button while holding down the `Ctrl` key. At this time, use the `cv2.rotate` function to rotate the `Application.imgScale` variable, `Application.imgOriginCopy` variable, and `Application.imgScaleCopy` variable clockwise by 90° respectively;
[0037] S455. If the mouse operation event `event` is detected as `cv2.EVENT_RBUTTONDOWN`, it means the user is clicking with the right mouse button. At this time, set the `Application.locationList` list variable to an empty list. At the same time, assign the backed-up `Application.imgScaleCopy` variable to the `Application.imgScale` variable on which the preview range has been drawn on the image display window using the `cv2.line` function to achieve echo display.
[0038] In this specification, in S47, the four corner coordinates of the core photo after scaled display obtained in the custom mouse event handling function according to `Application.onmouse` are inversely scaled using the `Application.scale` variable to obtain the four corner coordinates (tl, tr, br, bl) of the original image of the core photo. Use (tl, tr, br, bl) to calculate the width value and height value of the target image of the core photo, that is, select the maximum value of the horizontal difference and the maximum value of the vertical difference as the width value and height value for calculating the target image of the core photo respectively. And calculate the four corner coordinates of the target image of the core photo based on the above four corner coordinates of the original image of the core photo and the width value and height value of the target image of the core photo; Use the `cv2.getPerspectiveTransform` function to obtain the perspective transformation matrix; Use the `cv2.warpPerspective` function to perform perspective transformation on the core photo to be processed to achieve the functions of automatic correction and cropping.
[0039] In this specification, S47 includes:
[0040] S471. Reverse scale the four coordinate points saved in the Application.locationList list variable 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 photo is as follows:
[0041] Select the maximum value of the horizontal difference as the width value widthNew of the target image of the cropped core photo;
[0042]
[0043] widthNew = max(widthTop, widthBottom);
[0044] Select the maximum value of the vertical difference as the height value heightNew of the cropped core photo;
[0045]
[0046] heightNew = max(heightRight, heightLeft);
[0047] Based on the width value and height value of the target image of the cropped core photo, the four-point coordinates of the target image of the cropped core photo can be obtained;
[0048] tlNew = (0, 0)
[0049] trNew = (widthNew - 1, 0)
[0050] brNew = (widthNew - 1, heightNew - 1)
[0051]
[0052] S472. Use the cv2.getPerspectiveTransform function to calculate the perspective transformation matrix M;
[0053] The mathematical form of the 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 photo and a point (tlNew_x, tlNew_y) in the target image of the core photo is expressed as:
[0056]
[0057] After expansion, we get:
[0058]
[0059]
[0060] For each pair of corresponding points (x i , y i ) and (x' i , y' i ), the following two equations are constructed:
[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] The above system of equations is arranged into the form of 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-definition unscaled core photo;
[0066] S474. Use the cv2.imencode function to store the cropped target image of the core photo.
[0067] In summary, the present invention has at least the following beneficial effects:
[0068] By combining the OpenCv library for image processing and the Tkinter library for graphical user interface in the Python language, the present invention realizes the rapid correction and cropping of core photos. First, high-definition images of the core box are collected using digital devices, and 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 photo are obtained. The image is accurately corrected and cropped using the perspective transformation method.
[0069] Compared with the previous situation where geological workers used Photoshop software to manually rotate and crop 150 core photos per hour on average, the present invention can quickly process core photos through computer language, improving the efficiency of cropping core photos. At the same time, compared with the patent "An Automatic Processing Method for Core Photos Based on Template Matching and Hough Transformation", the present invention not only does not require preprocessing operations such as drawing matching templates on the core box outdoors, but also can automatically correct and crop curved and deformed core photos through perspective transformation. Therefore, the present invention is overall highly adaptable and easy to implement in terms of technology selection for the target problem, greatly shortening the overall processing time of core photos by avoiding prior manual operations, and is a mature and robust comprehensive technical solution. In short, the present invention can quickly and accurately batch-process core photos, avoiding a large number of mechanical repetitions in traditional manual processing and preprocessing of the core box in advance, greatly improving the work efficiency of geological personnel, effectively solving the problem of curved and deformed core photos, and improving the applicability and practicality of the automation program.
[0070] The present invention realizes the rapid processing of core photos based on perspective transformation and graphical user interface, and can quickly process core photos without preprocessing the outdoor marking of the core box. Therefore, compared with the patent "An Automatic Processing Method for Core Photos Based on Template Matching and Hough Transformation", the present invention can greatly shorten the processing time of core photos overall. At the same time, the patent "An Automatic Processing Method for Core Photos Based on Template Matching and Hough Transformation" cannot automatically correct a core box with curved and deformed conditions, while the solution adopted by the present invention can quickly correct curved and deformed core photos even if the core box in the core photo has curved and deformed conditions due to the use of perspective transformation, and is more suitable for core photos taken outdoors. This solution has a wider overall applicability and stronger practicality. BRIEF DESCRIPTION OF THE DRAWINGS
[0071] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for description in the embodiments. Obviously, the following drawings are only some embodiments of the present invention, and those of ordinary skill in the art can obtain other drawings without creative efforts based on these drawings.
[0072] Figure 1 It is a schematic diagram of the method for processing core photos based on perspective transformation and graphical user interface involved in the present invention.
[0073] Figure 2 It is a schematic diagram of the dense arrangement of core boxes in the core library involved in the present invention.
[0074] Figure 3 Schematic diagrams of the target core box, upper core box, and lower core box involved in the present invention.
[0075] Figure 4a 、 Figure 4b 、 Figure 4c and Figure 4d All are bent and deformed core boxes photographed on the field slope with uneven ground surface involved in the present invention.
[0076] Figure 5 Set the thumbnail scale diagram of the core photo to be processed involved in the present invention.
[0077] Figure 6a and Figure 6b Comparison diagrams of the display sizes of core photos with the same resolution but different scaling ratios of 0.2 and 0.25 respectively on the computer screen involved in the present invention.
[0078] Figure 7 Draw the automatically corrected and cropped quadrilateral range represented by the 4 pixel coordinates located by clicking the left mouse button involved in the present invention.
[0079] Figure 8a and Figure 8b 、 Figure 8c and Figure 8d 、 Figure 8e and Figure 8f Comparison diagrams of the core photo before and after cropping involved in the present invention respectively.
[0080] Figure 9 The created graphical user interface involved in the present invention. Detailed implementation manners
[0081] The embodiments of the present invention will be described in detail below with reference to the accompanying drawings.
[0082] As Figure 1 shown, this embodiment provides a core photo processing method based on perspective transformation and graphical user interface, which specifically includes the following steps:
[0083] S1: Use a digital device to take photos of the core box and collect high-definition core box images that may include adjacent core boxes;
[0084] S2: Define and design an Application class based on the Tkinter graphical user interface library of Python to establish a graphical user interface and initialize related parameters;
[0085] S3: Define and design the Application.selectFile custom function for batch selecting core photos. Use the tkinter.filedialog.askopenfilenames function to batch obtain the file paths of all core photos to be processed, and save the paths in the Application.pathImage variable;
[0086] S4: Define and design the Application.interactivePerspectiveWarp custom function to obtain the four corner points of the core photos selected in S3, perform perspective transformation, and save them. First, use the graphical user interface to interact with the user's mouse 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 function and the cv2.warpPerspective function to perform perspective transformation on the core photo. Finally, use the cv2.imencode function to encode and save the processed core photo;
[0087] S5: Create a "Batch Select Photos" button and an "Interactive Perspective Transformation" button based on the tkinter.button function in the Python graphical user interface Tkinter library. Bind the "Batch Select Photos" button to the Application.selectFile custom function for selecting photos in S3, and bind the "Interactive Perspective Transformation" button to the Application.interactivePerspectiveWarp custom function for obtaining the coordinates of the four corner points of the core photo and performing perspective transformation in S4;
[0088] S6: Use Anaconda to package the automated processing program based on perspective transformation and graphical user interface and create an exe executable file.
[0089] In some embodiments, in S1, since this technical solution is based on perspective transformation and graphical user interface, there is no need to preprocess the core photos. Just take pictures of the core box with a digital device.
[0090] In some embodiments, in S4, due to the different resolutions of each digital device, a window for setting the scaling ratio parameter needs to be designed in the graphical user interface to implement the scaling function of the display size of the core photo on the computer screen, so as to improve the robustness and applicability of this 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 on the four corner points of the target core box in the core photo in the graphical user interface, a thick red line is used to display the cropping range for judging whether the automatic correction and the cropping range are appropriate.
[0092] In some embodiments, in S4, when it is determined that the cropping range is inappropriate, a withdrawal function is implemented by designing relevant functions to facilitate secondary cropping by geological personnel.
[0093] In some embodiments, in S4, when the selected core photo is in a state of waiting to be rotated due to reasons such as the shooting position angle, the rotation of the core photo is implemented by designing relevant functions.
[0094] In some embodiments, in S4, since an error will be reported when using the cv2.imread function to read pictures with Chinese paths, therefore, to be able to compatibly open core photos with Chinese and English paths, this technical solution uses the cv2.imdecode function for picture decoding.
[0095] In some embodiments, in S4, since this technical solution needs to obtain the correction and cropping ranges of each core photo, the graphical user interface is used to interact with the user's mouse, so as to obtain the coordinate values of the correction and cropping ranges of the core photo, perform perspective transformation and then save them. The Tkinter standard graphical user interface library is used to encapsulate a series of operations on the core photo in the corresponding functions and bind them to the corresponding buttons.
[0096] In some embodiments, in S6, to facilitate the use by geological personnel, the Tkinter library is used to create a graphical user interface (GUI). Tkinter is a standard graphical user interface library for Python, which provides functions for creating windows, buttons, text boxes and other GUI elements. The Tkinter library binds the corresponding event functions through the command attribute when defining components, and is easy to use. This method mainly uses two buttons, "Batch Rotate Photos" and "Interactive Perspective Transformation" at the front end, and uses the graphical user interface library to encapsulate and package the above steps. It can not only enable users to not need to pre-install any computer language running environment, but also does not depend on external library files, greatly reducing the compatibility problems caused by different running environments, and facilitating the use of this system and method by geological personnel.
[0097] In some embodiments, in S1, a digital device is used to take pictures of the core box to collect high-definition core box images. In the field of geological exploration, according to the regulations of the "Regulations for Original Geological Logging of Solid Mineral Exploration DZT0078-2015", for all core boxes that have completed inspection and sorting, digital photography and archiving must be carried out box by box. Finally, only a group of core box photos (i.e., core photos) that are horizontal and have no significant deformation and contain cores can appear in each image. However, the core boxes in the core library are usually densely arranged in rows ( Figure 2 ), so it is inevitable to involve adjacent core boxes before and after during the shooting process ( Figure 3 ). At the same time, not only is the placement angle of each core box different, but when the core box is placed near the field drilling rig platform, it will also face the situation of bending and deformation of the core box due to the uneven surface of the ground. Therefore, geological personnel usually need to use computer image processing software to rotate, straighten and crop each core box image one by one after taking pictures and collecting the core box images.
[0098] Since interactive cropping is carried out based on the graphical user interface and the user's mouse, geological personnel do not need to pre-mark the core box in the field. They only need to use a digital device to take pictures of the core box to collect high-definition core box images. Therefore, this technical solution can greatly shorten the time for geological personnel to process core photos as a whole. At the same time, since the thumbnail of the core photo to be processed will be displayed in the graphical user interface, and the user only needs to use mouse interaction to determine the correction and cropping range, this technical solution can calculate the four corner coordinates of the target image of the core photo based on the four corner coordinates of the source image of the core photo, and then realize automatic correction and cropping of the core photo through perspective transformation. Therefore, this technical solution not only does not need to use technical means such as HSV mask, Hough transform, Canny edge detection, and template matching to process the core photo organically, but also does not need to pre-mark the core photo outdoors before automated processing. The overall logic of the technical solution is clear, easy to implement and has strong applicability.
[0099] In some embodiments, since this technical solution is based on perspective transformation and graphical user interface, compared with the template matching technology, it does not need to determine the matching template and draw it at a specific position of the core box in the field for computer program recognition, which greatly shortens the processing time of core photos as a whole.
[0100] In some embodiments, when the core box taken in the field is located on a slope or the ground is uneven, the core photo will be bent and deformed ( Figure 4a 、 Figure 4b 、 Figure 4c and Figure 4d) After calculating the coordinates of the four corner points of the target image of the core photo based on the graphical user interface interaction, the technical solution corrects and crops the core photo using perspective transformation. Therefore, not only is it unnecessary to preprocess the marker matching template for the core box in the field, but it is also possible to automatically correct and crop the core photo with bending deformation.
[0101] In some embodiments, in S2, based on the Tkinter graphical user interface library of Python, the Application class is defined and designed to establish the graphical user interface and initialize relevant parameters.
[0102] S21: Create the Application class with the class parameter being Frame, and define the __init__ constructor.
[0103] By creating the Application class and inheriting from tkinter.Frame, the Application class can utilize the graphical interface functions in the Tkinter library, enabling the class to create and manage buttons, labels, and other Tkinter widgets within it.
[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 perform necessary initialization operations automatically when the object is created, such as setting default parameters and creating data structures.
[0105] S22: Design the __init__ constructor of the Application class. In this constructor, super().__init__(master) is used to call the constructor of the parent class. At the same time, create the Application.locationList list variable to store the coordinates of the four corner points of the scaled core photo in the image display window obtained through mouse interaction in the graphical user interface in S45, create the Application.pathImage list variable to save the storage path of the core photo to be processed selected by the user in S3, and create the Application.scale variable to set the scaling ratio of the core photo in the image display window in S43.
[0106] S23: In the main function, first, instantiate the tkinter.Tk class as the object root. For the root object, use the tkinter.Tk.geometry function to set the size and position of the graphical user interface, and use the tkinter.Tk.title function to set the title of the graphical user interface. Second, instantiate the Application class as the object app and pass the parameter master = root. Finally, use the tkinter.Tk.mainloop function to start the event loop of Tkinter, enabling 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 and is used to create the main window of the application. By instantiating the tkinter.Tk class, the main window object root is created. This object will serve as the container for the entire application, hosting all other controls. The Application class is a custom class inherited from Tkinter.Frame. By instantiating the Application class and passing the parameter master = root, the instance object app of the Application class is associated with the main window root, meaning that all controls in the object app will be placed in the main window root.
[0108] In some embodiments, to improve the robustness of the program and avoid the exception that the user does not select the save path of the core photo to be processed when processing the core photo to be processed in S4, a flag variable self.selectImageFlag is created in the __init__ constructor and initialized to False. It is used to determine whether the Application.selectFile function has been successfully executed, that is, to determine whether the user has selected the core photo to be processed.
[0109] In some embodiments, in S23 of this implementation scheme, the parameter of the tkinter.Tk.geometry(”length x width + X coordinate + Y coordinate“) function is 400x150+1000+200. Its meaning is 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, define and design the Application.selectFile custom function, use the tkinter.filedialog.askopenfilenames function to batch obtain the save paths of all core photos to be processed, and save these paths in the Application.pathImage list variable.
[0111] The Application.pathImage list variable is used to save the core photos to be processed selected in batches by the tkinter.filedialog.askopenfilenames function, and is used to traverse each core photo to be processed during the subsequent core photo processing operation 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 judge the length of the Application.pathImage list variable. If the list is a non-empty list, it means that the user has successfully selected the core photos to be processed, and the Application.selectImageFlag flag is set to True.
[0113] In some embodiments, in S4, the Application.interactivePerspectiveWarp custom function is defined and designed to obtain the four corner coordinates of the core photo selected in S3, perform perspective transformation, and then save them. First, the four corner coordinates of the core photo to be cropped are obtained in combination with the graphical user interface. Secondly, the four corner coordinates of the target image of the core photo are calculated according to the four corner coordinates of the source image of the core photo. Thirdly, the cv2.getPerspectiveTransform function and the cv2.warpPerspective function are used to perform perspective transformation on the core photo. Finally, the cv2.imencode function is used to save the processed core photo.
[0114] S41: Judge whether S3 has completed the selection of the core photos to be processed.
[0115] Judge the Application.selectImageFlag flag bit in S3. If it is False, call the tkinter.messagebox.showinfo function to prompt the user "Please first complete the step - batch select pictures", and use return to exit the function. At this time, the Application.selectImageFlag flag is still False. Only when the user correctly selects the save path of the core photos to be processed in the Application.selectFile custom function, the Application.selectImageFlag flag will be set to True.
[0116] S42: If the selection of the core photo to be processed is completed, that is, the Application.selectImageFlag flag variable is True, at this time, use the tkinter.filedialog.askdirectory function to set the storage location of the cropped core photo, and save the storage location to the local variable SavePath of the Application.interactivePerspectiveWarp custom function. Then, use the len function to determine whether the length of the local variable SavePath is empty to determine whether the user has set the save path of the cropped core photo. If the local variable SavePath is empty, call the tkinter.messagebox.showinfo function to prompt the user "The target image save path of the core photo has not been selected", and use return to exit the function.
[0117] S43: Use the tkinter.simpledialog.askstring function to prompt the user to enter a value to assign a value to the scaling ratio Application.scale variable.
[0118] Since it is necessary to obtain the positions of the 4 corners of the core box to be cropped by clicking the mouse in the graphical user interface, it is necessary to display the 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 solve the inconsistency of the resolutions of different users' digital devices, before interacting with the core photo, use the tkinter.simpledialog.askstring function to pop up a window to set and assign the scaling ratio ( Figure 5 ), and the user can appropriately adjust the scaling ratio according to the size of the thumbnail in the image display window so that the core photos taken by digital devices with different pixels can be displayed in an appropriate size on the screen as a whole. The display sizes of the core photos on the computer screen at the same resolution but different scaling ratios are as Figure 6a and 6b shown.
[0120] In some embodiments, since the tkinter.simpledialog.askstring function is used to pop up a window to set the scaling ratio, to improve the robustness of the technical solution, the ValueError exception is used to capture the exception of the user entering non-numeric information. When the user enters non-numeric information, call the tkinter.messagebox.showinfo function to prompt the user "The scaling ratio should be of numeric type rather than string, etc.".
[0121] S44: Traverse the Application.pathImage variable in S3, use the cv2.imdecode function to open the core photos. For each core photo to be processed, save the unscaled core photo to the Application.imgOriginCopy variable. Use the scaling ratio Application.scale variable obtained in S43 and the cv2.resize function to scale and display the source image of the current core photo to be processed in the graphical user interface, and save the current core photo to be processed and scaled to the Application.imgScale variable and the Application.imgScaleCopy variable. The Application.imgScale variable is used to display the thumbnail of the core photo in the image display window in S451; the Application.imgScaleCopy variable is used for the echo display in the image display window after the user performs the withdrawal operation in S455; the Application.imgOriginCopy variable is used for perspective transformation on the unscaled core photo with higher resolution in S473.
[0122] Since cv2.imread directly reads the core photo file through the file path, and the file path may contain Chinese characters or other non-ASCII characters. In some operating systems (such as Windows) or environments, the encoding method of the file path may be incompatible with the default encoding method of OpenCV, resulting in a reading failure. Therefore, an error will be reported when using the cv2.imread function to read pictures with Chinese paths.
[0123] In some embodiments, to achieve compatibility in reading core photos with Chinese and English paths, this technical solution uses the cv2.imdecode function for image decoding. The cv2.imdecode function does not directly process the file path, but decodes the image data in memory into the OpenCV image format (NumPy array). This design makes it more reliable and flexible when dealing with paths containing non-ASCII characters. Therefore, the cv2.imdecode function bypasses the encoding limitations of the file system for paths, thus achieving compatibility in reading core photos with Chinese and English paths.
[0124] In some embodiments, when the geological personnel determine that the cropping range is inappropriate based on the visualization quadrilateral range after positioning by left-clicking the mouse, they can use the withdrawal function implemented by right-clicking the mouse in the Application.onmouse custom mouse event handling function of S455 to reposition the cropping range. At this time, the geological personnel need to left-click the mouse 4 times again on the core photo without drawing the quadrilateral range to confirm the cropping range again. At the same time, considering that the original resolution of the core photo is usually high, if it is not scaled, it usually cannot be well displayed in the image display window, and the resolutions of different users' digital devices are different. Therefore, it is necessary to scale and display the core photo in the image display window through S44, and then perform perspective transformation on the core photo with higher resolution without scaling. Therefore, full consideration must be given to the backup of the core photo to be processed.
[0125] Therefore, since this technical solution requires adding secondary positioning of withdrawing the cropping range and zooming and displaying the core photo to be cropped in the image display window during implementation, it is necessary not only to back up the original image of the core photo to be processed after zooming for the user to perform operations again when they are not satisfied with the cropping range, but also to back up the original image of the core photo to be processed without zooming for finally implementing perspective transformation for correction and cropping. Therefore, it is necessary to use the cv2.imdecode function to decode and read the source image of the core photo to be processed during the traversal of the Application.pathImage variable, and then use the os.copy function to back up the core photo to be processed without zooming and name it as the Application.imgOriginCopy variable, so as to finally perform perspective transformation on the core photo with higher resolution without scaling. Name the core photo to be processed after being scaled by S44 as the Application.imgScale variable, and use the os.copy function to back up and name it as the Application.imgScaleCopy variable. This is because when the withdrawal operation is executed, the Application.imgScale variable has used the cv2.line function to draw the line segment of the preview effect. At this time, it is necessary to use the Application.imgScaleCopy variable to enable the user to redisplay the thumbnail of the original image on the computer screen during the withdrawal operation.
[0126] S45: Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handling function to continuously monitor mouse operations, and implement the visualization of left-clicking the mouse 4 times to present the corrected cropping range, the withdrawal function implemented by right-clicking the mouse to reposition the cropping range, and the rotation of the core photo implemented by ctrl + left-clicking the mouse in the Application.onmouse custom mouse event handling function.
[0127] In some embodiments, to improve the practicability 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 visually presented. Geological personnel can decide whether to end the processing of the current photo by judging this range. If the effect meets the expectation, press any key to process the next photo. If the previewed cropping range does not meet the expectation and it is necessary to click to locate the correction and cropping range again, then right-click the mouse to perform secondary processing on the current core photo.
[0128] In some embodiments, through the cv2.setMouseCallback function in the OpenCV library, the Application.onmouse custom mouse event handling function is bound to the image display window to monitor the user's mouse operations in the image display window in real time and execute corresponding image processing tasks according to the type of mouse operation. In this technical solution, left-clicking the mouse 4 times visualizes the correction and cropping range, right-clicking the mouse withdraws the current positioning result to facilitate repositioning the cropping range, and ctrl + left-clicking the mouse realizes the rotation of the core photo.
[0129] S451: Use the cv2.imshow function to display the scaled core photo to be processed, which is the Application.imgScale variable.
[0130] S452: Use the len function to determine whether the length of the Application.locationList list variable is 4. If it is 4, it indicates 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 connect the coordinates of the four corner points obtained by left-clicking the mouse to draw the automatic correction and cropping range.
[0131] In some embodiments, to visually reflect the automatic correction and cropping range determined by left-clicking the mouse, in the Application.onmouse custom mouse event handling function, after left-clicking the mouse 4 times, draw the represented automatic correction and cropping quadrilateral range according to the pixel coordinate points of the four corner points ( Figure 7 ), for geological personnel to judge whether the cropping range is appropriate.
[0132] When the length of the Application.locationList list variable is 4, it indicates that the user has completed 4 left mouse button clicks in the graphical user interface. If the length of the list variable is not equal to 4, it indicates that the automatic correction of the core photo and the box selection of the cropping range have not been completed yet. It should be noted that only after the left mouse button is clicked 4 times to locate the pixel points, the Application.onmouse custom mouse event handling function will enter this conditional statement and use the cv2.line function to draw a straight line on the Application.imgScale variable of the scaled core photo to be processed. Subsequently, in the next mouse monitoring loop for the current core photo to be processed, the automatic correction and cropping range will be visually presented through S451.
[0133] S453: If only the mouse operation event event is detected as cv2.EVENT_LBUTTONDOWN, it means that the user is using the left mouse button click operation. At this time, use the append function for the Application.locationList list variable to append the corner pixel coordinates obtained from the latest positioning to the list.
[0134] It should be noted that when 4 left mouse button clicks are detected in the Application.onmouse custom mouse event handling function, the length of the Application.locationList list variable is 4 at this time. Therefore, in the loop monitoring of this function, in S452, the cv2.line function can be used to connect the 4 corner coordinates obtained by the left mouse button click positioning end to end to draw the automatic correction and cropping range for the user to judge whether the correction and cropping range is appropriate.
[0135] S454: If the mouse operation event event is detected as cv2.EVENT_FLAG_CTRLKEY and cv2.EVENT_LBUTTONDOWN, it means that the user is using the ctrl + left mouse button click operation. At this time, use the cv2.rotate function to rotate the Application.imgScale variable, the Application.imgOriginCopy variable, and the Application.imgScaleCopy variable clockwise by 90°.
[0136] It should be noted that the Application.imgScale variable is rotated 90° clockwise because what is displayed in S451 is the core photo being processed in the current image display window. When the user performs the rotation operation, the rotated core photo should be displayed in the image display window; the Application.imgOriginCopy variable is rotated 90° clockwise because after the core photo in the image processing window is rotated 90°, the coordinates of the four corner points that need to be clicked and located in S453 are also rotated, so the image that undergoes perspective transformation in S47 should also be rotated accordingly; the Application.imgScaleCopy variable is rotated 90° clockwise because when the user performs the undo operation in S455, the automatic correction and cropping quadrilateral range drawn by cv2.line in S452 needs to be cleared without changing the existing rotation angle of the core photo in the image, so this variable needs to be used to save the image rotation state. In summary, this technical solution achieves memory of the current rotation state of the core photo by backing up the unscaled core photo and the scaled core photo, that is, it avoids the situation where the core photo in the image display window needs to be rotated again after the user right-clicks the mouse to withdraw the positioning operation, and the same applies to the unscaled core photo.
[0137] In some embodiments, in order to avoid the core box being blocked by the sun's shadow when taking photos of some core boxes, geologists usually choose to stand on the upper core box, the lower core box or the side of the target core box to take photos of the target core box according to the actual situation. Because, in order to avoid the subsequent manual rotation of some reverse-shot core photos by 90° or 180°, the core photo can be rotated by implementing ctrl+left mouse button operation in the Application.onmouse custom mouse event processing function, thereby avoiding the user from finding the core box image that needs to be rotated and straightened from many core photos and manually rotating it to the same direction. The user only needs to use ctrl+left mouse button to click the current core photo to be processed to complete the clockwise rotation of the core photo by 90°.
[0138] The cv2.rotate function is a function used in OpenCV to rotate images. It only supports rotations of integer multiples of 90° (such as 90°, 180°, and 270°). Its mathematical principle is based on matrix operations of coordinate transformation, but there is a simplified implementation for rotations at 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 any rotation angle θ, the transformation matrix is:
[0140]
[0141] Pixel coordinate transformation formula:
[0142]
[0143] However, cv2.rotate only supports rotation in multiples of 90°. Therefore, the formula can be greatly simplified. That is, when the rotation angle is 90°, 180°, or 270°, interpolation or complex calculations are not required, and 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 at this time is:
[0144]
[0145] S455: If it is detected that the mouse operation event event is cv2.EVENT_RBUTTONDOWN, it means that the user is right-clicking with the mouse. At this time, the Application.locationList list variable is set to an empty list. At the same time, the backed-up Application.imgScaleCopy variable is assigned to the Application.imgScale variable that has used the cv2.line function to draw the preview range on the image in the image display window, thereby achieving echo.
[0146] It should be noted that when the Application.locationList list variable is set to an empty list, in each loop monitoring of the Application.onmouse mouse event handling function, the len function in S452 is used to determine that the length of the Application.locationList list variable is not equal to 4. Therefore, the automatic correction and cropping range of the current core photo to be processed located by the user through mouse clicks will not be visually displayed in the core photo to be processed, and the backed-up Application.imgScaleCopy variable is assigned to the Application.imgScale variable, thereby achieving the withdrawal operation. Only when the user is satisfied with the cropping range, any key on the keyboard is pressed to start processing the next core photo to be processed.
[0147] S46: Use the cv2.waitKey function and the cv2.destroyAllWindows function to end the mouse monitoring of the current core photo to be processed by the Application.onmouse custom mouse event handling function.
[0148] After the user clicks the left mouse button to locate 4 points, the expected automatic correction and cropping range will be visually displayed on the graphical user interface. If the previewed cropping range meets the expectations, the user can press any key to end the processing of the current core photo and close the thumbnail of the current core photo. At this time, the 4 corner point coordinates obtained by clicking the left mouse button in S45 have been appended to the Application.locationList list variable for subsequent perspective transformation and cropping of the core photo.
[0149] The cv2.waitKey function is a keyboard binding function used to implement the interaction between the keyboard and the image display window. Its parameter represents the time to wait for keyboard input. In this technical solution, this parameter is set to 0, indicating that the subsequent program can continue to execute only after any key on the keyboard is pressed. The cv2.destroyAllWindows function is used to destroy all currently displayed image display windows. This function has no parameters. Since only the source image window of the current core photo to be processed is displayed each time in this technical solution, using this function can achieve the closing of the window of the current core photo to be processed.
[0150] S47: Use the 4 pixel coordinate positions representing the cropping range in the core photo thumbnail saved in the Application.locationList list variable for reverse scaling processing. By calculating, construct the 4 target pixel positions of the target image of the cropped core photo. Use the cv2.getPerspectiveTransform function and the cv2.warpPerspective function to perform perspective transformation on the unscaled Application.imgOriginCopy variable, and use the cv2.imencode function to store the target image of the cropped core photo.
[0151] First, according to the four corner coordinates in the thumbnail of the core photo obtained in the custom mouse event handling function of Application.onmouse and using the Application.scale variable for reverse scaling, the four corner coordinates (tl, tr, br, bl) of the source image of the core photo are obtained. The width value and height value of the target image of the core photo are calculated using (tl, tr, br, bl), that is, the maximum horizontal difference and the maximum vertical difference are respectively selected as the width value and height value for calculating the target image of the core photo. And based on the above four corner coordinates of the source image of the core photo and the width value and height value of the target image of the core photo, the four corner coordinates of the target image of the core photo are calculated; Secondly, the perspective transformation matrix is calculated using the cv2.getPerspectiveTransform function; Finally, the cv2.warpPerspective function is used to perform perspective transformation on the Application.imgOriginCopy variable that stores the un-scaled core photo to be processed, thus realizing the functions of automatic correction and cropping.
[0152] S471: According to the four coordinate points saved in the Application.locationList list variable, using the Application.scale variable for reverse scaling, the four corner coordinates (tl, tr, br, bl) of the un-scaled source image of the core photo are obtained, and based on this, the four target coordinate points (tlNew, trNew, brNew, blNew) of the cropped target image of the core photo are calculated.
[0153] It should be noted that the image in the image display window is the scaled core photo, and the image finally subjected to perspective transformation is the un-scaled core photo. Therefore, in order to avoid the inability to fully display the entire image in the image window due to the too large resolution of the core photo, in S44, the image is scaled and displayed according to the scaling ratio Application.scale variable set by the user, and four corner coordinates are obtained based on the scaled image. Therefore, when actually performing perspective transformation on the core photo with a higher original resolution and un-scaled, it is necessary to perform reverse scaling on the four corner coordinates in the Application.locationList list variable using the Application.scale variable. For example, when the image is scaled by 0.25 times in S44, it is necessary to perform reverse scaling on the four corner coordinates saved in the Application.locationList list variable in S45, that is, the coordinate points need to be multiplied by 4 times to reflect the correct corner pixel positions in the un-scaled core photo.
[0154] In some embodiments, the specific steps for calculating the four target coordinate points (tlNew, trNew, brNew, blNew) of the target image of the core photo after cropping are as follows: the four corner point coordinates (tl, tr, br, bl) of the source image of the core photo without scaling are obtained by performing inverse scaling processing on the four coordinate points saved in the Application.locationList list variable using the Application.scale variable.
[0155] (1) Select the maximum value of the horizontal difference as the width value widthNew of the target image of the core photo after cropping
[0156]
[0157] widthNew = max(widthTop, widthBottom);
[0158] (2) Select the maximum value of the vertical difference as the height value heightNew of the target image of the core photo after cropping
[0159]
[0160] heightNew = max(heightRight, heightLeft);
[0161] (3) According to the width value and height value of the target image of the core photo after cropping, the four-point coordinates of the target image of the core photo after cropping 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 value and height value of the image start counting from 1, while the index of the pixel position starts counting from 0, when calculating the four-point pixel coordinate values of the core photo after cropping, it is necessary to subtract 1 from the width value and height value.
[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 the 4 corner points of the image. Perspective transformation is a non-linear transformation that can project an image from one perspective to another. This transformation preserves linearity but does not preserve parallelism, so it is often used for perspective transformation, such as correcting an image taken at an angle to a frontal perspective.
[0169] The mathematical form of the perspective transformation is a 3x3 transformation matrix, usually called the homography matrix, and this matrix form is as follows:
[0170]
[0171] The mapping relationship between a point (tl_x, tl_y) in the source image of the core photo and a point (tlNew_x, tlNew_y) in the target image of the core photo can be expressed as:
[0172]
[0173] After expansion, it becomes:
[0174]
[0175]
[0176] The perspective transformation matrix M has 8 degrees of freedom. Therefore, at least 4 pairs of point coordinates are required to solve this matrix. In this embodiment, the graphical user interface is used to interactively locate 4 corner point coordinates using the mouse for calculating the perspective transformation matrix. Thus, for each pair of corresponding points (x i , y i ) and (x' i , y' i ), the following two equations can be constructed:
[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] Arrange the above system of equations into the form of 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-definition unscaled core photo.
[0182] cv2.warpPerspective is a function in OpenCV used to perform perspective transformation. This function remaps the core photo according to the given perspective transformation matrix M, projecting the original image from one perspective to another. Comparison before and after cropping of the core photo is shown as 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 cropped target image of the core photo.
[0184] In some embodiments, since an error will occur when using the cv2.imwrite function to read pictures with Chinese paths, therefore, to achieve compatibility with storing core photos with Chinese path storage locations, this technical solution uses the cv2.imencode function for picture encoding.
[0185] S5: Create a "Batch Select Photos" button and an "Interactive Perspective Transformation" button based on the tkinter.button function in the Python-based graphical user interface Tkinter library. Bind the "Batch Select Photos" button to the Application.selectFile custom function used to select photos in S3, and bind the "Interactive Perspective Transformation" button to the Application.interactivePerspectiveWarp custom function used to obtain 4 corner points of the core photo and perform perspective transformation in S4;
[0186] Since this technical solution needs to obtain the cropping range of each core photo, a graphical user interface is used to interact with the user's mouse, so as to obtain the coordinate values of the core photo correction and cropping range. To facilitate the use of geological personnel and improve the practicality of the program, the Tkinter standard graphical user interface library is used to package the automation program. Therefore, it is necessary to package a series of operations on the core photo in the corresponding function and bind it to the corresponding button. By creating an interactive button and binding the button to a specific function, the user can perform complex image processing tasks through simple click operations. In addition, by encapsulating each function in an independent function, the code coupling is relatively low, which is convenient for subsequent function expansion and maintenance.
[0187] S6: Use Anaconda to package the core photo processing method based on the perspective transformation principle and the graphical user interface created based on the Tkinter library into an exe executable file.
[0188] In some embodiments, to facilitate the use of geological personnel, a graphical user interface is created using the Tkinter library, as Figure 9 shown. Tkinter is a standard graphical user interface library for Python, which provides functions for creating windows, buttons, text boxes, and other GUI elements. The Tkinter library binds the corresponding event functions through the command attribute when defining components, and is easy to use. The front end mainly uses two buttons, "Batch Rotate Photos" and "Interactive Perspective Transformation". Packaging and packing the above steps using the graphical user interface library can not only enable users to run without pre-installing any computer language running environment, but also does not depend on external library files, greatly reducing the compatibility problems caused by different running environments.
[0189] In some embodiments, when importing a large number of third-party libraries in the code, there are usually problems with the mismatch between the Pyinstaller version and the Python version or the third-party library version when using the pyinstaller command to package Python into an exe file in the command prompt (CMD). It is usually difficult to package the Python code into an exe executable file. However, using Anaconda for packaging can solve various function library compatibility problems, and the exe executable file generated in the virtual environment of Anaconda is relatively small.
[0190] The present invention is implemented using the Python language, mainly by using the OpenCV computer vision library and the Tkinter graphical user interface library; the use of the openCV image processing library and the Tkinter standard graphical user interface library realizes the rapid automatic correction and cropping of a batch of core photos, achieving the rapid and precise processing of a large number of pictures. Through this method, not only a large amount of repetitive mechanical labor is avoided, improving the daily work efficiency of geological workers, but also the rapid automatic correction of core photos with bending and deformation in the field can be realized, and the cropping effect is greatly improved, enhancing the reliability of image processing.
[0191] The above-described embodiments are used to illustrate the present invention and are not intended to limit the present invention. Therefore, changes in the exemplified numerical values or replacement of equivalent elements should still fall within the scope of the present invention.
Claims
1. A method for processing core photos based on perspective transformation and graphical user interface, characterized in that Including: S1. Collect core photos including adjacent core boxes; S2. Define and design an Application class based on the Python's Tkinter graphical user interface library to create a graphical user interface; S3. Define and design an Application.selectFile custom function to batch select core photos, use the tkinter.filedialog.askopenfilenames function to batch obtain the file paths of all core photos to be processed, and save the file paths in the Application.pathImage variable; S4. Define and design an Application.interactivePerspectiveWarp custom function to obtain the four corner points of the core photos selected in S3, perform perspective transformation, and save them; S5. Create a batch select photo button and an interactive perspective transformation button based on the tkinter.button function in the Python graphical user interface Tkinter library, bind the batch select photo button to the Application.selectFile custom function for selecting photos in S3, and bind the interactive perspective transformation button to the Application.interactivePerspectiveWarp custom function for obtaining the four corner points of the core photo and performing perspective transformation in S4; S6. Package S2 to S5 using Anaconda and create an exe executable file.
2. The core photo processing method based on perspective transformation and graphical user interface according to claim 1, wherein In S4, first, combine the graphical user interface to obtain the coordinates of the four corner points of the core photo to be corrected and cropped. Secondly, 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. Thirdly, use the cv2.getPerspectiveTransform function and the cv2.warpPerspective function to perform perspective transformation on the core photo. Finally, use the cv2.imencode function to save the processed core photo.
3. The core photo processing method based on perspective transformation and graphical user interface according to claim 1, wherein S2 Including: S21. Create an Application class with the class parameter Frame and define the __init__ constructor; Create an Application class. By inheriting tkinter.Frame, the Application class can utilize the graphical interface functions in the Tkinter library, enabling the class to create and manage buttons, labels, and other Tkinter widgets within it; S22. Design the __init__ constructor of the Application class. In this constructor, call the constructor of the parent class using super().__init__(master). At the same time, create the Application.locationList list variable to store the coordinates of the four corner points of the core photo that is scaled and displayed in the image display window through mouse interaction in the graphical user interface, create the Application.pathImage list variable to save the storage path of the core photo to be processed, and 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 instantiate the tkinter.Tk class as the object root. Use the tkinter.Tk.geometry function to set the size and position of the graphical user interface for the root object, and use the tkinter.Tk.title function to set the title of the graphical user interface. Secondly, instantiate the Application class as the object app and pass the parameter master = root. Finally, use the tkinter.Tk.mainloop function to start the event loop of Tkinter, enabling the user to interact with the application in real time by continuously listening for events.
4. The method for processing core photos based on perspective transformation and graphical user interface according to claim 3, wherein Create a flag variable self.selectImageFlag in the __init__ constructor, initialized to Flase, to determine whether the Application.selectFile function has been successfully executed.
5. The core photo processing method based on perspective transformation and graphical user interface according to claim 1, wherein In the Application.selectFile custom function of S3, after calling the tkinter.filedialog.askopenfilenames function, it is necessary to judge the length of the Application.pathImage list variable. If the list is not an empty list, it means that the user has successfully selected the core photo to be processed, and set the Application.selectImageFlag flag to True.
6. The core photo processing method based on perspective transformation and graphical user interface according to claim 1, wherein S4 Include: S41. Judge whether S3 has completed the selection of the core photo to be processed; S42. If the selection of the core photo to be processed is completed, that is, the Application.selectImageFlag flag variable is True. At this time, use the tkinter.filedialog.askdirectory function to set the storage location of the target image of the cropped core photo, and save the storage location to the local variable SavePath in the Application.interactivePerspectiveWarp custom function. Determine whether the save path of the cropped core photo has been set by judging the length of the local variable SavePath. If the local variable SavePath is empty, call the tkinter.messagebox.showinfo function to prompt the user "The save path of the target image of the core photo has not been selected", and use return to exit the function; S43. Use the tkinter.simpledialog.askstring function to prompt the user to enter a value to assign a value to the scaling ratio Application.scale variable; S44. Traverse the Application.pathImage variable in S3, use the cv2.imdecode function to open the core photo. For each core photo to be processed, save the unscaled core photo to the Application.imgOriginCopy variable. Use the scaling ratio Application.scale variable obtained in S43 and the cv2.resize function to scale and display the source image of the current core photo to be processed in the graphical user interface, and save the current core photo to be processed and scaled to the Application.imgScale variable and the Application.imgScaleCopy variable; The Application.imgScale variable is used to display the thumbnail of the core photo in the image display window; The Application.imgScaleCopy variable is used for the echo display in the image display window after the user performs the withdrawal operation; The Application.imgOriginCopy variable is used to finally perform perspective transformation on the unscaled core photo with higher resolution; S45. Use cv2.setMouseCallback to bind the Application.onmouse custom mouse event handling function to monitor mouse operations, and implement the visualization of the correction and cropping range by clicking the left mouse button 4 times, the withdrawal function by clicking the right mouse button to reposition the correction and cropping range, and the 90° clockwise rotation of the core photo by pressing ctrl + left mouse button in the Application.onmouse custom mouse event handling function; S46. Use the cv2.waitKey function and the cv2.destroyAllWindows function to end the mouse monitoring of the current core photo to be processed by the Application.onmouse custom mouse event handling function; After the user clicks the left mouse button to locate the 4 corner points, visualize and display the expected automatic correction and cropping range on the graphical user interface. If the previewed cropping range meets the expectations, the user can press any key to end the processing of the current core photo by closing the thumbnail of the current core photo. At this time, the 4 corner point pixel coordinates obtained by left mouse button clicking in S45 have been appended to the Application.locationList list variable for subsequent perspective transformation of the core photo; S47. Use the 4 pixel points saved in the Application.locationList list variable, which represent the correction and cropping range of the thumbnail in the image display window, to perform inverse scaling processing to obtain the source pixel coordinate positions of the core photo to be corrected and cropped without scaling, construct the 4 target pixel coordinate positions of the corrected and cropped core photo, use the cv2.getPerspectiveTransform function and the cv2.warpPerspective function to perform perspective transformation on the Application.imgOriginCopy variable, and use the cv2.imencode function to store the target image of the cropped core photo.
7. The method for processing core photos based on perspective transformation and graphical user interface according to claim 6, characterized in that In S45, visualize the automatic correction and cropping range determined by the 4 corner points of the core photo located by left mouse button clicking. Geological personnel decide whether to end the processing of the current core photo based on the judgment of this range. If the effect meets the expectations, press any key to process the next core photo. If the previewed cropping range does not meet the expectations and it is necessary to re-click to locate the 4 corner points to determine the correction and cropping range again, then click the right mouse button to process the current core photo again; Bind the Application.onmouse custom mouse event handling function to the image display window through the cv2.setMouseCallback function in the OpenCV library to monitor the user's mouse operations in real time and execute corresponding image processing tasks according to the operation type.
8. The core photo processing method based on perspective transformation and graphical user interface according to claim 6, characterized in that S45 includes: S451. Use the cv2.imshow function to display the scaled core photo to be processed, the Application.imgScale variable; S452. Use the len function to determine whether the length of the Application.locationList list variable is 4. If it is 4, it indicates 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 connect the 4 corner point coordinates obtained by left mouse button clicking end to end to draw the automatic correction and cropping range; S453. If only the mouse operation event `event` is detected as `cv2.EVENT_LBUTTONDOWN`, it means the user is performing a left - click operation with the mouse. At this time, use the `append` function on the `Application.locationList` list variable to append the corner pixel coordinates obtained from the latest positioning to the list. S454. If the mouse operation event `event` is detected as `cv2.EVENT_FLAG_CTRLKEY` and `cv2.EVENT_LBUTTONDOWN`, it means the user is performing a `ctrl + left - click` operation. At this time, use the `cv2.rotate` function to rotate the `Application.imgScale` variable, `Application.imgOriginCopy` variable, and `Application.imgScaleCopy` variable clockwise by 90° respectively. S455. If the mouse operation event `event` is detected as `cv2.EVENT_RBUTTONDOWN`, it means the user is performing a right - click operation with the mouse. At this time, set the `Application.locationList` list variable to an empty list. Meanwhile, replace the `Application.imgScale` variable, which has been used to draw the preview range on the image using the `cv2.line` function in the image display window, with the backed - up `Application.imgScaleCopy` variable to achieve echo display.
9. The core photo processing method based on perspective transformation and graphical user interface according to claim 6, characterized in that, In S47, the four corner coordinates of the source image of the core photo after scaled display obtained in the custom mouse event handling function `Application.onmouse` are reversed - scaled using the `Application.scale` variable to get `(tl, tr, br, bl)`. Calculate the width value and height value of the target image of the core photo, that is, select the maximum value of the horizontal difference and the maximum value of the vertical difference as the width value and height value of the target image of the core photo respectively. And calculate the four corner coordinate points of the target image of the core photo based on the above four corner coordinates of the source image of the core photo and the width value and height value of the target image of the core photo; use the `cv2.getPerspectiveTransform` function to obtain the perspective transformation matrix; use the `cv2.warpPerspective` function to perform perspective transformation on the core photo to be processed to achieve the functions of automatic correction and cropping.
10. The core photo processing method based on perspective transformation and graphical user interface according to claim 6, characterized in that S47 includes: S471. Reverse - scale the four coordinate points saved in the `Application.locationList` list variable using the `Application.scale` variable to get `(tl, tr, br, bl)`. The specific calculation of the four target coordinate points `(tlNew, trNew, brNew, blNew)` of the core photo after cropping is as follows: Select the maximum value of the horizontal difference as the width value widthNew of the target image of the core photo after cropping; widthNew = max(widthTop, widthBottom); Select the maximum value of the vertical difference as the height value heightNew of the core photo after cropping; heightNew = max(heightRight, heightLeft); Based on the width value and height value of the target image of the core photo after cropping, the four-point coordinates of the target image of the core photo after cropping can be obtained; tlNew = (0, 0) trNew = (widthNew - 1, 0) brNew = (widthNew - 1, heightNew - 1) blNew = (0, heightNew - 1); S472. Use the cv2.getPerspectiveTransform function to calculate the perspective transformation matrix M; The mathematical form of the perspective transformation is a 3x3 transformation matrix: The mapping relationship between a point (tl_x, tl_y) in the source image of the core photo and a point (tlNew_x, tlNew_y) in the target image of the core photo is expressed as: After expansion, it is obtained: For each pair of corresponding points (x i , y i ) and (x' i , y' i ), the following two equations are constructed: 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 ; Organize the above equations into the form of a linear equation system: S473. Use the cv2.warpPerspective function to perform perspective transformation and cropping on the variable Application.imgOriginCopy of the high-definition unscaled core photo; S474. Use the cv2.imencode function to store the target image of the core photo after cropping.
Citation Information
Patent Citations
Method and system for processing core photo
CN102129701A
Image rasterization processing method and device
CN107230183A
Image processing memory management method and management system for target detection
CN109118424A
Image processing method and device, electronic device, and storage medium
CN109285126A
Rock soil automatic recognition naming method and system based on deep learning
CN110490100A
Cited By
Rock core photograph automatic correction and cutting method based on YOLOv8-seg and perspective transformation
CN121707887A
Core photo automatic correction and cropping method based on YOLOv8-seg and perspective transformation
CN121707887B
Batch automatic correction method and system for rock core photos
CN122155959A
