Image cropping method and device, terminal and readable storage medium
By performing binary processing of the image and analyzing the mask diagram, the maximum inline border is obtained for cropping, which solves the problem of low cropping efficiency after image stitching, and achieves more efficient effective area retention.
Patent Information
- Application Number
- CN202210137158.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-15
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2042-02-15
Smart Images

Figure CN114549551B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of imaging technology, and more specifically, to an image cropping method, an image cropping device, a terminal, and a non-volatile computer-readable storage medium. Background Art
[0002] Image stitching is a technique that stitches multiple overlapping images of the same scene into a larger image. It is of great value in the fields of photography, image editing, medical imaging, remote sensing, etc. However, the image stitching process requires perspective transformation of the images to be stitched, which will inevitably lead to the existence of black invalid areas around the stitching results. In order to remove the black invalid areas and retain the valid areas of the image as much as possible, the image stitching results need to be cropped to obtain satisfactory results. However, most of the cropping of the stitching results is done manually, which has low cropping efficiency. Summary of the invention
[0003] The embodiments of the present application provide an image cropping method, an image cropping device, a terminal and a non-volatile computer-readable storage medium, which are at least used to solve the problem of low efficiency in cropping images.
[0004] The image cropping method of the embodiment of the present application includes: binarizing the image to be cropped to obtain a binary image. Obtaining a valid area mask image and a border mask image according to the binary image. Obtaining a maximum inscribed border of the image to be cropped according to the valid area mask image and the border mask image. Cropping the image to be cropped according to the maximum inscribed border to obtain a target image.
[0005] The image cropping device of the embodiment of the present application includes a processing module, a first acquisition module, a second acquisition module and a cropping module. The processing module is used to perform a binarization process on the image to be cropped to obtain a binary image. The first acquisition module is used to obtain a valid area mask image and a border mask image according to the binary image. The second acquisition module is used to obtain the maximum inscribed border of the image to be cropped according to the valid area mask image and the border mask image. The cropping module is used to crop the image to be cropped according to the maximum inscribed border to obtain a target image.
[0006] The terminal of the embodiment of the present application includes one or more processors, a memory and one or more programs. Among them, one or more of the programs are stored in the memory and executed by one or more of the processors, and the program includes instructions for executing the cropping method of the embodiment of the present application. The cropping method includes: binarizing the image to be cropped to obtain a binary image. Obtaining a valid area mask image and a border mask image based on the binary image. Obtaining the maximum inscribed border of the image to be cropped based on the valid area mask image and the border mask image. Crop the image to be cropped according to the maximum inscribed border to obtain a target image.
[0007] The non-volatile computer-readable storage medium of the embodiment of the present application includes a computer program. When the computer program is executed by one or more processors, the processors perform the following cropping method: binarize the image to be cropped to obtain a binary image. Obtain a valid area mask image and a border mask image according to the binary image. Obtain the maximum inscribed border of the image to be cropped according to the valid area mask image and the border mask image. Crop the image to be cropped according to the maximum inscribed border to obtain a target image.
[0008] In the cropping method, cropping device, terminal and non-volatile computer-readable storage medium of the present application, an effective area mask map and a border mask map are obtained according to a binary image. The maximum inscribed border of the image to be cropped can be obtained by combining the effective area mask map and the border mask map. The image to be cropped is cropped using the maximum inscribed border. Compared with the manual cropping method, the cropping efficiency is greatly improved, and the target image can contain the largest possible effective area in the image to be cropped, resulting in a better cropping effect.
[0009] Additional aspects and advantages of the embodiments of the present application will be given in part in the description below, and in part will become apparent from the description below, or will be learned through the practice of the embodiments of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the description of the embodiments in conjunction with the following drawings, in which:
[0011] Figure 1 is a flowchart of an image cropping method in certain embodiments of the present application;
[0012] Figure 2 is a schematic structural diagram of an image cropping device in certain embodiments of the present application;
[0013] Figure 3 is a schematic diagram of the structure of a terminal in some implementation modes of the present application;
[0014] Figure 4 It is a schematic diagram of obtaining a binary image in a method for cropping an image in certain embodiments of the present application;
[0015] Figure 5 It is a schematic diagram of obtaining a valid area mask map and a border mask map in a method for cropping an image in certain embodiments of the present application;
[0016] Figure 6 It is a schematic diagram of obtaining a target image according to a maximum inscribed frame in an image cropping method of certain embodiments of the present application;
[0017] Figure 7 is a flowchart of an image cropping method in certain embodiments of the present application;
[0018] Figure 8 It is a schematic diagram of obtaining a binary image according to an edge filling image in a method for cropping an image in certain embodiments of the present application;
[0019] Fig. 9 and Fig.10 is a flowchart of an image cropping method in certain embodiments of the present application;
[0020] Fig.11 It is a schematic diagram of graying an edge filling image to obtain a grayscale image in an image cropping method in certain embodiments of the present application;
[0021] Fig.12 is a flowchart of an image cropping method in certain embodiments of the present application;
[0022] Fig.13 It is an enlarged schematic diagram of the existence of black spots in the effective area of the binary image in the image cropping method of certain embodiments of the present application;
[0023] Figures 14 to 16 is a flowchart of an image cropping method in certain embodiments of the present application;
[0024] Fig.17 It is a schematic diagram of obtaining a difference mask map according to a valid area mask map and an updated border mask map in an image cropping method in certain embodiments of the present application;
[0025] Fig.18 It is a schematic diagram of obtaining a computing kernel by a method for cropping an image in certain embodiments of the present application;
[0026] Fig.19 is a flowchart of an image cropping method in certain embodiments of the present application;
[0027] Fig. 20 It is a connection diagram of a non-volatile computer-readable storage medium and a processor in certain embodiments of the present application. DETAILED DESCRIPTION
[0028] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals represent the same or similar elements or elements having the same or similar functions from beginning to end. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the embodiments of the present application, and cannot be understood as limiting the embodiments of the present application.
[0029] See also Figures 1 to 3 The present application embodiment provides a method for cropping an image. Figures 4 to 6 The cropping method includes:
[0030] 01: Binarize the cropped image PA to obtain a binary image PB;
[0031] 03: Obtain the effective area mask image PC and the border mask image PD according to the binary image PB;
[0032] 05: Obtain the maximum inscribed frame of the image to be cropped PA according to the effective area mask image PC and the frame mask image PD; and
[0033] 07: Crop the image to be cropped PA according to the maximum inscribed border to obtain the target image PF.
[0034] Please combine Figure 2 , the embodiment of the present application provides an image cropping device 10. The cropping device 10 includes a processing module 11, a first acquisition module 13, a second acquisition module 15 and a cropping module 17. The cropping method of the embodiment of the present application can be applied to the cropping device 10. Among them, the processing module 11 is used to execute the method in 01, the first acquisition module 13 is used to execute the method in 03, the second acquisition module 15 is used to execute the method in 05, and the cropping module 17 is used to execute the method in 07. That is, the processing module 11 is used to perform binarization processing on the image to be cropped PA to obtain a binary image. The first acquisition module 13 is used to obtain the effective area mask image PC and the border mask image PD according to the binary image PB. The second acquisition module 15 is used to obtain the maximum inscribed frame of the image to be cropped PA according to the effective area mask image PC and the border mask image PD. The cropping module 17 is used to crop the image to be cropped PA according to the maximum inscribed frame to obtain the target image PF.
[0035] Please combine Figure 3, the embodiment of the present application also provides a terminal 100, specifically, the terminal 100 includes one or more processors 30, a memory 50, and one or more programs. Among them, the one or more programs are stored in the memory 50 and executed by the one or more processors 30, and the program includes instructions for executing the cropping method of the embodiment of the present application. That is, when the processor 30 executes the program, the processor 30 can implement the methods in steps 01, 03, 05 and 07. That is, the one or more processors 30 are used to: binarize the image PA to be cropped to obtain a binary image PB; obtain a valid area mask image PC and a border mask image PD according to the binary image PB; obtain the maximum inscribed border of the image PA to be cropped according to the valid area mask image PC and the border mask image PD; crop the image PA to be cropped according to the maximum inscribed border to obtain the target image PF.
[0036] Specifically, the terminal 100 may include but is not limited to electronic devices such as mobile phones, laptops, smart TVs, tablet computers, smart watches, or computers. The cutting device 10 may be an integration of functional modules integrated in the terminal 100. This application only takes the case where the terminal 100 is a mobile phone as an example for description. The case where the terminal 100 is other types of devices is similar to that of a mobile phone and will not be described in detail.
[0037] Image stitching is a technique that stitches multiple overlapping images of the same scene into a larger image. It is of great value in the fields of photography, image editing, medical imaging, remote sensing, etc. However, the image stitching process requires perspective transformation of the images to be stitched, which will inevitably lead to the existence of black invalid areas around the stitching results. In order to remove the black invalid areas and retain the valid areas of the image as much as possible, the image stitching results need to be cropped to obtain satisfactory results. However, most of the cropping of the stitching results is done manually, which has low cropping efficiency.
[0038] In the cropping method of the present application, the effective area mask map PC and the border mask map PD are obtained according to the binary map PB. The maximum inscribed border of the image to be cropped PA can be obtained by combining the effective area mask map PC and the border mask map PD. The image to be cropped PA is cropped using the maximum inscribed border. Compared with the manual cropping method, the cropping efficiency is greatly improved, and the target image PF can contain the largest possible effective area in the image to be cropped PA, resulting in a better cropping effect.
[0039] Specifically, in method 03 and method 05, the first acquisition module 13 or the processor 30 obtains the effective area mask map PC according to the binary map PB to distinguish the effective area and the invalid area in the image to be cropped PA. The invalid area is the area in the image to be cropped PA that does not contain the image to be spliced, and the effective area is the area other than the invalid area. After the first acquisition module 13 or the processor 30 obtains the border mask map PD, the maximum inscribed border of the effective area can be quickly found by combining the effective area mask map PC and the border mask map PD, thereby effectively improving the cropping efficiency.
[0040] See also Figure 7 and Figure 8 In some implementations, 01: performing binarization processing on the cropped image PA to obtain a binary image PB, including:
[0041] 011: Use the first preset color pixels to perform edge filling on the image to be cropped PA to obtain an edge filling image PA1; and
[0042] 013: Binarize the edge filling image PA1 to obtain a binary image PB.
[0043] Please combine Figure 2 The processing module 11 is also used to execute the methods in 011 and 013. That is, the processing module 11 is also used to: fill the edge of the image PA to be cropped with the first preset color pixels to obtain an edge filling image PA1; and perform binarization processing on the edge filling image PA1 to obtain a binary image PB.
[0044] Please combine Figure 3 The processor 30 is further used to execute the methods in 011 and 013. That is, the processor 30 is further used to: perform edge filling on the cropped image PA using the first preset color pixels to obtain an edge filling image PA1; and perform binarization processing on the edge filling image PA1 to obtain a binary image PB.
[0045] When the processing module 11 or the processor 30 performs binarization processing on the image to be cropped PA, the first preset color pixel is first used to fill the edge of the image to be cropped PA, so as to ensure that a complete closed contour of the effective area in the image to be cropped PA can be found in the subsequent method, so as to ensure that the first acquisition module 13 or the processor 30 obtains the effective area mask image PC (such as Figure 5The first preset color pixel is consistent with the pixel of the invalid area in the image to be cropped PA. For example, if the pixel in the invalid area in the image to be cropped PA is a black pixel, the processing module 11 or the processor 30 uses the black pixel to fill the edge of the image to be cropped PA to obtain the edge filling image PA1. After the edge of the image to be cropped PA is filled with the first preset color pixel, the processing module 11 or the processor 30 performs a binarization process on the edge filling image PA1 to obtain a binary image PB.
[0046] See also Figure 8 and Fig. 9 In some embodiments, 011: edge filling the cropped image PA using first preset color pixels to obtain an edge filling image PA1, including:
[0047] 0111: Fill the image to be cropped PA with a preset number of pixels of a first preset color to obtain an edge filling image PA1.
[0048] Please combine Figure 2 The processing module 11 is further used to execute the method in 0111. That is, the processing module 11 is further used to fill the image to be cropped PA with a preset number of pixels of a first preset color to obtain an edge filling map PA1.
[0049] Please combine Figure 3 The processor 30 is further configured to execute the method in 0111. That is, the processor 30 is further configured to fill the image to be cropped PA with a preset number of pixels of a first preset color to obtain an edge filling map PA1.
[0050] When the processing module 11 or the processor 30 performs edge filling on the image to be cropped PA, the first preset color pixels of a preset number of pixels are filled around the image to be cropped PA, so that the image to be cropped PA has an invalid area of a certain width around it, so that the valid area in the image to be cropped PA includes an invalid area of a certain width around it, which is convenient for finding the complete closed contour of the valid area in the subsequent method. Specifically, if the resolution size of the image to be cropped PA is 100*100, and the size of each first preset color pixel is 1*1, the processing module 11 or the processor 30 can fill the first preset color pixels of the same width around the image to be cropped PA, and make the resolution size of the edge filling image PA1 less different from the resolution size of the image to be cropped PA. For example, a square ring-shaped pixel filling area is filled around the image PA to be cropped, and the width of the square ring is equal everywhere. Assuming that the width of the square ring is the width of ten first preset color pixels placed side by side, the size of the edge filling image PA1 is (100+2*10)*(100+2*10)=120*120, that is, the preset number of first preset color pixels filled is 120*120-100*100=4400.
[0051] See also Fig.10 and Fig.11 In some embodiments, 013: binarizing the edge filling image PA1 to obtain a binary image PB, including:
[0052] 0131: grayscale the edge filling image PA1 to obtain a grayscale image PA2; and
[0053] 0133: Update the pixel values corresponding to the pixel points with pixel values greater than 0 in the grayscale image PA2 to 255 to obtain the binary image PB.
[0054] Please combine Figure 2 The processing module 11 is also used to execute the methods in 0131 and 0133. That is, the processing module 11 is also used to: grayscale the edge filling image PA1 to obtain the grayscale image PA2; and update the pixel values corresponding to the pixel points with pixel values greater than 0 in the grayscale image PA2 to 255 to obtain the binary image PB.
[0055] Please combine Figure 3 The processor 30 is also used to execute the methods in 0131 and 0133. That is, the processor 30 is also used to: grayscale the edge filling image PA1 to obtain a grayscale image PA2; and update the pixel values corresponding to the pixel points with pixel values greater than 0 in the grayscale image to 255 to obtain a binary image PB.
[0056] After the processing module 11 or the processor 30 obtains the edge fill image PA1, it binarizes the edge fill image PA1. The edge fill image PA1 is first grayscaled to obtain a grayscale image PA2 corresponding to the edge fill image PA1, and then the grayscale image PA2 is binarized. That is, the pixel values corresponding to the pixels in the grayscale image whose pixel values are greater than 0 are updated to 255. In other words, the non-black pixels in the grayscale image PA2 are reset to white pixels. The obtained binary image only includes black pixels and white pixels, thereby obtaining the binary image PB.
[0057] See also Figure 5 and Fig.12 In some embodiments, 03: obtaining a valid area mask image PC and a border mask image PD according to the binary image PB, including:
[0058] 031: Get the maximum contour of the valid area in the binary image PB;
[0059] 033: Obtaining a valid area mask map PC according to the maximum contour; and
[0060] 035: Get the border mask map PD according to the minimum circumscribed border polygon of the maximum contour.
[0061] Please combine Figure 2 , the first acquisition module 13 is also used to execute the methods in 031, 033 and 035. That is, the first acquisition module 13 is also used to: obtain the maximum contour of the effective area in the binary image PB; obtain the effective area mask image PC according to the maximum contour; and obtain the frame mask image PD according to the minimum circumscribed frame polygon of the maximum contour.
[0062] Please combine Figure 3 , the processor 30 is also used to execute the methods in 031, 033 and 035. That is, the processor 30 is also used to: obtain the maximum contour of the valid area in the binary image PB; obtain the valid area mask image PC according to the maximum contour; and obtain the frame mask image PD according to the minimum circumscribed frame polygon of the maximum contour.
[0063] Specifically, after the first acquisition module 13 or the processor 30 obtains the binary image PB, it first obtains the maximum contour of the valid area in the binary image PB (such as Figure 5 The white lines in PB1 in the figure), the areas surrounded by the maximum contour are all valid areas in the image to be cropped PA after edge filling. Specifically, the edge of the valid area in the binary image PB can be detected by an edge detection algorithm, and the maximum contour of the valid area in the binary image PB is extracted according to the edge detection result. In this way, it is possible to avoid the presence of black spots in the white area of the binary image PB (such as Fig.13As shown in the figure, the black spots interfere with the process of obtaining the maximum inscribed border in the subsequent method. Then, the effective area mask PC is generated according to the maximum contour; the minimum external border polygon of the maximum contour is obtained, and the border mask PD is obtained from the area surrounded by the minimum external border polygon. The minimum external border polygon can be a triangle, a quadrilateral, a pentagon or other polygons, and the specific shape can be determined according to the actual target image PF to be obtained (such as Figure 6 The present application takes the minimum circumscribed frame quadrilateral as an example for detailed description.
[0064] See also Figure 5 and Fig.14 In some embodiments, 033: obtaining a valid area mask map PC according to the maximum contour, comprising:
[0065] 0331: Fill the area enclosed by the maximum contour with pixels of the second preset color to obtain a valid area mask image PC.
[0066] Please combine Figure 2 The first acquisition module 13 is further used to execute the method in 0331. That is, the first acquisition module 13 is further used to fill the area surrounded by the maximum outline with the second preset color pixels to obtain the effective area mask image PC.
[0067] Please combine Figure 3 The processor 30 is further configured to execute the method in 0331. That is, the processor 30 is further configured to fill the area surrounded by the maximum outline with pixels of the second preset color to obtain a valid area mask image PC.
[0068] Specifically, when the first acquisition module 13 or the processor 30 acquires the effective area mask image PC, the area surrounded by the maximum contour is filled with the second preset color pixel. The implementation method is simple and does not require checking each pixel point in the area surrounded by the maximum contour. Thus, the black spots that may exist in the area surrounded by the maximum contour can be quickly eliminated, effectively improving the cropping efficiency. The color of the second preset color pixel is different from the color of the first preset color pixel. In the embodiment of the present application, the first preset color pixel is a black pixel and the second preset color pixel is a white pixel.
[0069] It should be noted that the binary image PB and the effective area mask image PC only look the same. In fact, there may be tiny black spots in the binary image PB (such as Fig.13 As shown in the figure), the effective area mask map PC does not contain any black spots.
[0070] See also Figure 5 and Fig.15In some embodiments, 035: obtaining a border mask image PD according to a minimum bounding box rectangle of a maximum contour comprises:
[0071] 0351: Fill the area enclosed by the minimum circumscribed border polygon with pixels of a second preset color to obtain a border mask image PD.
[0072] Please combine Figure 2 The first acquisition module 13 is further used to execute the method in 0351. That is, the first acquisition module 13 is further used to fill the area surrounded by the minimum circumscribed frame polygon with the second preset color pixels to obtain the frame mask image PD.
[0073] Please combine Figure 3 The processor 30 is further configured to execute the method in 0351. That is, the processor 30 is further configured to fill the area enclosed by the minimum circumscribed frame polygon with pixels of the second preset color to obtain a frame mask image PD.
[0074] Specifically, the first acquisition module 13 or the processor 30 fills the area enclosed by the minimum circumscribed border polygon with the second preset color pixels (white pixels) to obtain the border mask image PD, so that the area where the white pixels in the border mask image PD are located can cover the area where the white pixels within the maximum contour in the effective area mask image PC are located, which facilitates the second acquisition module 15 or the processor 30 to obtain the maximum inscribed border according to the difference between the corresponding positions of the two. The implementation method is simple and effectively improves the cropping efficiency.
[0075] Among them, the shape of the minimum bounding box polygon is consistent with the target image PF (such as Figure 6 In the present application, if the target image PF is a quadrilateral, then the minimum circumscribed frame polygon is a quadrilateral.
[0076] See also Fig.16 and Fig.17 In some embodiments, 05: obtaining the maximum inscribed frame of the image to be cropped PA according to the effective area mask image PC and the frame mask image PD, including:
[0077] 051: performing an erosion operation on the border mask image PD according to a preset operation check to obtain an updated border mask image PD1, wherein the area of the region where the second preset color pixel is located in the updated border mask image PD1 is smaller than the area of the region where the second preset color pixel is located in the unupdated border mask image PD;
[0078] 053: Obtain a difference mask image PE according to the updated border mask image PD1 and the valid area mask image PC; and
[0079] 055: when the number of the second preset color pixels in the difference mask image PE is greater than zero, return to the step of performing the erosion operation; and
[0080] 057: When the number of the second preset color pixels in the difference mask image PE is equal to zero, the maximum inscribed border is obtained according to the area where the second preset color pixels in the updated border mask image PD1 are located.
[0081] Please combine Figure 2 , the second acquisition module 15 is also used to execute the methods in 051, 053, 055 and 057. That is, the second acquisition module 15 is also used to: perform an erosion operation on the border mask image PD according to the preset operation check to obtain an updated border mask image PD1, the area of the region where the second preset color pixels are located in the updated border mask image PD1 is smaller than the area of the region where the second preset color pixels are located in the unupdated border mask image PD; obtain a difference mask image PE according to the updated border mask image PD1 and the effective area mask image PC; and when the number of the second preset color pixels in the difference mask image PE is greater than zero, return to the step of performing the erosion operation; and when the number of the second preset color pixels in the difference mask image PE is equal to zero, obtain the maximum inscribed border according to the region where the second preset color pixels are located in the updated border mask image PD1.
[0082] Please combine Figure 3 , the processor 30 is also used to execute the methods in 051, 053, 055 and 057. That is, the processor 30 is also used to: perform an erosion operation on the border mask image PD according to the preset operation check to obtain an updated border mask image PD1, the area of the region where the second preset color pixel is located in the updated border mask image PD1 is smaller than the area of the region where the second preset color pixel is located in the unupdated border mask image PD; obtain a difference mask image PE according to the updated border mask image PD1 and the effective area mask image PC; and when the number of the second preset color pixels in the difference mask image PE is greater than zero, return to the step of performing the erosion operation; and when the number of the second preset color pixels in the difference mask image PE is equal to zero, obtain the maximum inscribed border according to the region where the second preset color pixel is located in the updated border mask image PD1.
[0083] Specifically, the shape of the preset operation kernel is determined according to the target image PF to be acquired (such as Figure 6 For example, when the target image PF is a triangle, the operation core can be set to a triangle; when the target image PF is a quadrilateral, the operation core can be set to a quadrilateral. This application takes the shape of the operation core as a quadrilateral as an example for explanation.
[0084] The width and height of the operation kernel can be different. If the width and height of the operation kernel are different, the speed of erosion in the horizontal and vertical directions of the border mask PD is also different. It can be set according to the width and height of the border mask PD (that is, the total width and total height of the pixels in the horizontal and vertical directions). For example, if the size of each pixel is 1*1, in order to ensure that the accurate maximum inscribed border can be obtained, when setting the erosion operation, only one row or one column of pixels is eroded each time.
[0085] When the shape of the operation kernel is a quadrilateral, the second acquisition module 15 or the processor 30 may set the width and height of the operation kernel according to the width and height of the effective area mask map PC.
[0086] In one embodiment, the operation kernel is obtained according to the cardinality of the preset operation kernel and the width and height of the effective area mask map PC. Specifically, the width and height of the effective area mask map PC are used for calculation, assuming that the width and height of the effective area mask map PC are respectively recorded as w and h, and the cardinality of the preset operation kernel is recorded as b, and the cardinality of the preset operation kernel can be 7, 9 or 11. The aspect ratio r1=w / h of the effective area mask map PC calculated by the second acquisition module 15 or the processor 30.
[0087] If r1 ≥ 1, that is, the width of the effective area mask map PC is greater than the height of the effective area mask map PC, then the height of the operation kernel kernel_h1 = b, and the width of the operation kernel is kernel_w1, which is expressed by formula (1):
[0088] Formula (1):
[0089] Among them, [] means rounding to the nearest integer. When [b*r1] is an odd number, kernel_w1=[b*r1], otherwise kernel_w1=[b*r1]-1.
[0090] If r1<1, that is, the width w of the effective area mask map PC is less than the height h of the effective area mask map PC, then the width of the operation kernel kernel_w1=b, and the height of the operation kernel is kernel_h1, which is expressed by formula (2):
[0091] Formula (2):
[0092] Among them, [] means rounding to the nearest integer. When [b / r1] is an odd number, kernel_h1=[b / r1], otherwise kernel_h1=[b / r1]-1.
[0093] In summary, the size of the operation kernel kernel_size1=(kernel_w1, kernel_h1).
[0094] In another embodiment, the operation kernel is obtained according to the cardinality of the preset operation kernel and the four convex points of the effective area (white area) in the effective area mask map PC. Specifically, the cardinality of the preset operation kernel is b, and the cardinality of the preset operation kernel can be 7, 9 or 11. The second acquisition module 15 or the processor 30 obtains the coordinates of the four convex points of the effective area mask map PC, and respectively calculates the horizontal distance and vertical distance from the four convex points to the corresponding boundaries, such as Fig.18 As shown in the figure, the horizontal distance from the first salient point to the corresponding boundary is L1, and the vertical distance is V1; the horizontal distance from the second salient point to the corresponding boundary is L2, and the vertical distance is V2; the horizontal distance from the third salient point to the corresponding boundary is L3, and the vertical distance is V3; the horizontal distance from the fourth salient point to the corresponding boundary is L4, and the vertical distance is V4. Then calculate the mean of the four horizontal distances and the mean of the four vertical distances, and record them as the horizontal mean. , vertical mean Then calculate the mean level and vertical mean The ratio between them is r2, .
[0095] If r2≥1, that is, , then the height of the kernel is kernel_h2=b, and the width of the kernel is kernel_w2. kernel_w2 is expressed by formula (3):
[0096] Formula (3):
[0097] Among them, [] means rounding to the nearest integer. When [b*r2] is an odd number, kernel_w2=[b*r2], otherwise kernel_w2=[b*r2]-1.
[0098] If r2<1, that is, , the width of the operation kernel is kernel_w2=b, and the height of the operation kernel is kernel_h2. kernel_h2 is expressed by formula (4):
[0099] Formula (4):
[0100] Among them, [] means rounding to the nearest integer. When [b / r2] is an odd number, kernel_h2=[b / r2], otherwise kernel_h2=[b / r2]-1.
[0101] In summary, the size of the operation kernel kernel_size2=(kernel_w2, kernel_h2).
[0102] When the second acquisition module 15 or the processor 30 performs an erosion operation on the border mask image PD, the black areas around the border mask image PD continue to diffuse inward, and the white areas are continuously eroded by the black areas, so that the area of the area where the second preset color pixels are located in the updated border mask image PD1 is smaller than the area of the area where the second preset color pixels are located in the unupdated border mask image PD. When the operation core performs an erosion operation on the entire image of the border mask image PD, the black area in the border mask image PD diffuses inward once to obtain the updated border mask image PD1.
[0103] After the operation check performs an erosion operation on the border mask PD, the second acquisition module 15 or the processor 30 uses the updated border mask PD1 and the effective area mask PCC to obtain the difference mask PE. The second acquisition module 15 or the processor 30 makes a judgment based on the difference between the updated border mask PD1 and the effective area mask PC. The implementation method is simple, reduces the amount of calculation of the second acquisition module 15 or the processor 30, and effectively improves the efficiency of obtaining the maximum inscribed border, thereby improving the cropping efficiency.
[0104] After obtaining the difference mask image PE, the second acquisition module 15 or the processor 30 counts the number of the second preset color pixels (white pixels) in the difference mask image PE. When the number of the second preset color pixels in the difference mask image PE is greater than zero, the methods in 051 and 053 are re-executed on the border mask image PD1 obtained after an erosion operation, and it is determined whether the number of the second preset color pixels in the obtained difference mask image PE is greater than zero. If the number of the second preset color pixels in the difference mask image PE is equal to zero, the maximum inscribed border is obtained according to the area where the second preset color pixels in the updated border mask image PD1 are located. Otherwise, return to perform the erosion operation.
[0105] Specifically, in method 057, when the number of the second preset color pixels in the difference mask image PE is equal to zero, the vertex coordinates of the area formed by the second preset color pixels in the frame mask image PD1 obtained by the latest update are used as the vertex coordinates of the maximum inscribed frame, and the maximum inscribed frame is obtained according to the four vertex coordinates. Figure 6 Finally, the cropping module 17 or the processor 30 crops the image to be cropped PA using the maximum inscribed frame, and the area covered by the maximum inscribed frame is the target image PF.
[0106] See also Fig.17 and Fig.19 In some implementations, 053: obtaining a difference mask image PE according to the updated border mask image PD1 and the valid area mask image PC, including:
[0107] 0531: Subtract each pixel value in the updated border mask image PD1 from the pixel value at the corresponding position in the effective area mask image PC, and use the calculated difference as the pixel value at the corresponding position in the difference mask image PE.
[0108] Please combine Figure 2 The second acquisition module 15 is also used to execute the method in 0531. That is, the second acquisition module 15 is also used to: subtract each pixel value in the updated border mask image PD1 from the pixel value at the corresponding position in the effective area mask image PC, and calculate the difference as the pixel value at the corresponding position in the difference mask image PE.
[0109] Please combine Figure 3 The processor 30 is further used to execute the method in 0531. That is, the processor 30 is further used to: subtract each pixel value in the updated border mask image PD1 from the pixel value at the corresponding position in the effective area mask image PC, and calculate the difference as the pixel value at the corresponding position in the difference mask image PE.
[0110] Specifically, when the second acquisition module 15 or the processor 30 obtains the difference mask image PE, the pixel value at the corresponding position in the effective mask image PC is subtracted from each pixel value in the updated border mask image PD1 to obtain the pixel value at the corresponding position in the difference mask image PE. Among them, the updated border mask image PD1, the effective area mask image PC and the difference mask image PE are all unsigned eight-bit (the pixel value range is [0, 255]) single-channel images. Since the pixel values of the black area are all 0, the pixel values of the white area are all 255. According to the unsigned operation rules, 0-0=0, 255-255=0, 255-0=255, 0-255=0, among which 0-255=0 is caused by operation overflow.
[0111] In an example, assuming that the resolutions of the updated border mask image PD1, the effective area mask image PC, and the difference mask image PE are all 100*100, the pixel value at the first row and first column in the updated border mask image PD1 is 0, and the pixel value at the first row and first column in the effective area mask image PC is 0. According to the above-mentioned operation rules, the pixel value at the first row and first column in the difference mask image PE is 0, and the position in the difference mask image PE appears black. If the pixel value at the third row and third column in the border mask image PD1 after a certain update is 255, and the pixel value at the third row and third column in the effective area mask image PC is 0, then the pixel value at the third row and third column in the difference mask image PE is 255, and the position in the difference mask image PE appears white. If the pixel value at the fourth row and second column in the updated border mask image PD1 is 0, and the pixel value at the fourth row and second column in the valid area mask image PC is 255, then the pixel value at the fourth row and second column in the difference mask image PE is 0, and this position in the difference mask image PE appears black.
[0112] It should be noted that each time an erosion operation is performed, an updated border mask image and a difference mask image are obtained.
[0113] See also Fig. 20 The present application also provides a non-volatile computer-readable storage medium 300 including a computer program 301. When the computer program 301 is executed by one or more processors 30, the processor 30 executes the cropping methods in 01, 03, 05, 07, 011, 013, 0111, 0131, 0133, 031, 033, 035, 0331, 0351, 051, 053, 055, 057, and 0531.
[0114] In the description of this specification, the descriptions with reference to the terms "certain embodiments", "in an example", "exemplarily", etc., mean that the specific features, structures, materials or characteristics described in conjunction with the embodiments or examples are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, unless they are contradictory.
[0115] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, segment or portion of code that includes one or more executable instructions for implementing the steps of a specific logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.
[0116] Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limitations on the present application. Ordinary technicians in this field can change, modify, replace and modify the above embodiments within the scope of the present application.
Claims
1. A method for cropping an image, characterized in that: include: Binarize the image to be cropped to obtain a binary image; Obtaining a valid area mask map and a border mask map according to the binary image; Acquire the maximum inscribed frame of the image to be cropped according to the effective area mask image and the frame mask image; and Cropping the image to be cropped according to the maximum inscribed frame to obtain a target image; The obtaining the maximum inscribed frame of the image to be cropped according to the effective area mask map and the frame mask map includes: Performing an erosion operation on the border mask image according to a preset operation check to obtain an updated border mask image, wherein the area of a region where the second preset color pixel is located in the updated border mask image is smaller than the area of a region where the second preset color pixel is located in the unupdated border mask image; Obtaining a difference mask map according to the updated border mask map and the valid area mask map; and When the number of the second preset color pixels in the difference mask image is greater than zero, returning to the step of performing the erosion operation; and When the number of the second preset color pixels in the difference mask image is equal to zero, acquiring the maximum inscribed border according to the area where the second preset color pixels in the updated border mask image are located; The step of obtaining a valid area mask map and a border mask map according to the binary map includes: Obtaining the maximum contour of the valid area in the binary image; Acquire the effective area mask map according to the maximum contour; and The frame mask image is obtained according to the minimum circumscribed frame polygon of the maximum contour.
2. The cutting method according to claim 1, characterized in that: The step of performing binarization processing on the image to be cropped to obtain a binary image includes: Filling the edge of the image to be cropped with pixels of a first preset color to obtain an edge-filled image; and The edge filling image is binarized to obtain the binary image.
3. The cutting method according to claim 1, characterized in that: The obtaining the effective area mask map according to the maximum contour includes: The area surrounded by the maximum outline is filled with pixels of a second preset color to obtain the effective area mask map.
4. The cutting method according to claim 1, characterized in that: The step of obtaining the frame mask image according to the minimum bounding box rectangle of the maximum contour includes: The area enclosed by the minimum circumscribed frame polygon is filled with pixels of a second preset color to obtain the frame mask image.
5. The cutting method according to claim 1, characterized in that: The step of obtaining a difference mask map according to the updated border mask map and the valid area mask map includes: Subtract each pixel value in the updated border mask image from the pixel value at the corresponding position in the effective area mask image, and calculate the difference as the pixel value at the corresponding position in the difference mask image.
6. An image cropping device, characterized in that: include: A processing module, used for performing binarization processing on the image to be cropped to obtain a binary image; A first acquisition module, used for acquiring a valid area mask map and a border mask map according to the binary image; A second acquisition module, used for acquiring the maximum inscribed frame of the image to be cropped according to the effective area mask map and the frame mask map; and A cropping module, used for cropping the image to be cropped according to the maximum inscribed frame to obtain a target image; The second acquisition module is also used to perform an erosion operation on the border mask image according to a preset operation check to obtain the updated border mask image, wherein the area of the region where the second preset color pixels are located in the updated border mask image is smaller than the area of the region where the second preset color pixels are located in the unupdated border mask image; obtain a difference mask image according to the updated border mask image and the valid area mask image; and when the number of the second preset color pixels in the difference mask image is greater than zero, return to the step of performing the erosion operation; and when the number of the second preset color pixels in the difference mask image is equal to zero, obtain the maximum inscribed border according to the region where the second preset color pixels are located in the updated border mask image; The first acquisition module is also used to acquire the maximum contour of the valid area in the binary image; acquire the valid area mask image according to the maximum contour; and acquire the border mask image according to the minimum circumscribed border polygon of the maximum contour.
7. A terminal, characterized in that: include: One or more processors, memory; and One or more programs, wherein one or more of the programs are stored in the memory and executed by one or more of the processors, and the programs include instructions for executing the cropping method according to any one of claims 1 to 5.
8. A non-volatile computer-readable storage medium storing a computer program, wherein when the computer program is executed by one or more processors, the processors implement the cropping method according to any one of claims 1 to 5.
