Image processing method
By generating a cropping mask and using sliding window matching similarity calculation, the problems of subject offset and misjudgment of similar images in image cropping are solved, and high-accuracy image cropping is achieved.
Patent Information
- Application Number
- CN202510741705.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-05
- Publication Date
- 2025-09-12
AI Technical Summary
Existing image cropping algorithms easily cause subject shift when processing images and cannot effectively distinguish similar images, resulting in deviation in cropping results.
The cropped image is used to generate a cropping mask, which is divided into n×n regions. The central area is extracted as the template image, and the similarity value is calculated by sliding window matching with the original image. An alpha channel fusion mask is created to accurately locate the subject and avoid misjudgment of similar images.
It achieves precise positioning of the subject after cropping and effectively distinguishes similar images, improving the accuracy and reliability of image cropping.
Smart Images

Figure CN120635113A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of image processing technology, and in particular to an image processing method. Background Art
[0002] In the existing field of image cropping technology, image processing faces many problems.
[0003] On the one hand, traditional cropping algorithms have significant flaws when processing images. Existing algorithms often operate based on simple rules or preset parameters, lacking a deep understanding of body features and complex scenes. In practical applications, traditional cropping algorithms can easily cause the subject to appear offset in the cropped image, meaning the subject is not positioned exactly as expected in the image.
[0004] On the other hand, the similarity in appearance between similar images can interfere with image cropping. In similar industries, many images have a high degree of similarity in appearance. Due to the lack of an effective differentiation mechanism, these similar images can easily be mistaken for the same dish during the cropping process, resulting in biased cropping results. Summary of the Invention
[0005] In order to help solve the above technical problems, this application provides an image processing method, which adopts the following technical solutions:
[0006] An image processing method, wherein the image processing method comprises:
[0007] S1: Cropping the original image to obtain a cropped image, generating a cropping mask from the cropped image, dividing the cropped image into n×n regions, extracting a central region as a template image, where n is a positive integer and an odd number;
[0008] S2: Matching the template image as a sliding window with the original image based on a preset step size, calculating similarity values of multiple positions in each window, and obtaining the highest similarity value and the coordinates corresponding to the highest similarity value;
[0009] S3: When the highest similarity value is less than the preset highest similarity threshold, discard the current highest similarity value and the coordinates corresponding to the highest similarity value; when all the highest similarity values are less than the preset highest similarity threshold, return to step S1;
[0010] S4: creating an Alpha channel with the same size as the original image, mapping the cropped image mask to the Alpha channel, and outputting a fused final mask.
[0011] Preferably, S2 includes: calculating the highest similarity value and the coordinates corresponding to the highest similarity value by:
[0012]
[0013] Where T(x, y) is the pixel value of the template image, I(x+u, y+v) is the pixel value of the sliding window with (u, v) as the upper left corner in the original image, is the mean of the template image, is the mean value of the original image at position (u, v), NCC(u, v) is the highest similarity value, and (u, v) is the coordinate corresponding to the highest similarity value.
[0014] Preferably, S4 includes:
[0015] S41: Generate a blank transparency layer with the same size as the original image as the fusion base;
[0016] S42: Calculate the position of the cropped image in the original image and determine the rectangular area to be filled based on the coordinates corresponding to the highest similarity value in step S2;
[0017] S43: Copying the Alpha channel data of the cropped image to a corresponding area of the Alpha channel of the original image.
[0018] Preferably, n is 3, the highest similarity threshold is 0.8, and the preset step size is 1 pixel.
[0019] Preferably, the image processing method further comprises S5: cropping the original image based on the final mask to obtain a repaired cropped image.
[0020] To sum up, this application can accurately locate the subject and place it in the expected position. At the same time, in order to address the problem of similar appearance of similar images interfering with cropping, this application will construct an effective distinction mechanism to avoid similar images being misjudged during cropping, thereby effectively reducing the deviation of the cropping results and greatly improving the accuracy and reliability of image cropping. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A schematic diagram of an embodiment of an original image of the present application;
[0022] Figure 2 is a schematic diagram of cropping an image;
[0023] Figure 3 is a schematic diagram of the cropped image after restoration;
[0024] Figure 4 This is a flow chart of subject positioning based on template matching in this application;
[0025] Figure 5 Flowchart of mask synthesis and training data generation for this application. DETAILED DESCRIPTION
[0026] The present invention will be further described below with reference to the accompanying drawings. The structure and principle of the present invention will be very clear to those skilled in the art. It should be understood that the specific embodiments described herein are only intended to explain the present invention and are not intended to limit the present invention.
[0027] Figure 1 A schematic diagram of an embodiment of the original image of this application, Figure 2 is a schematic diagram of cropping an image. Figure 3 Schematic diagram of the cropped image after restoration.
[0028] Combine Figures 1 to 3 It can be understood that the image processing method of the present application includes:
[0029] S1: Cropping the original image to obtain a cropped image, generating a cropping mask from the cropped image, dividing the cropped image into n×n regions, and extracting the central region as a template image, where n is a positive and odd integer. In the embodiment of the present application, n is 3.
[0030] S2: Based on a preset step size, the template image is used as a sliding window to match the original image. For each window, similarity values are calculated for multiple positions to obtain the highest similarity value and the coordinates corresponding to the highest similarity value. S2 includes: calculating the highest similarity value and the coordinates corresponding to the highest similarity value by the following method:
[0031]
[0032] Where T(x, y) is the pixel value of the template image, I(x+u, y+v) is the pixel value of the sliding window with (u, v) as the upper left corner in the original image, is the mean of the template image, is the mean value of the original image at position (u, v), NCC(u, v) is the highest similarity value, and (u, v) is the coordinate corresponding to the highest similarity value. In the embodiment of the present application, the preset step size is 1 pixel.
[0033] S3: When the highest similarity value is less than the preset highest similarity threshold, the current highest similarity value and the coordinate corresponding to the highest similarity value are discarded. When all the highest similarity values are less than the preset highest similarity threshold, the process returns to step S1. In the embodiment of the present application, the highest similarity threshold is 0.8.
[0034] S4: Create an alpha channel with the same size as the original image, map the cropped image mask to the alpha channel, and output the final fused mask. S4 includes:
[0035] S41: Generate a blank transparency layer with the same size as the original image as the fusion base;
[0036] S42: Calculate the position of the cropped image in the original image and determine the rectangular area to be filled based on the coordinates corresponding to the highest similarity value in step S2;
[0037] S43: Copying the Alpha channel data of the cropped image to a corresponding area of the Alpha channel of the original image.
[0038] S5: Crop the original image based on the final mask to obtain a repaired cropped image.
[0039] The following is a detailed description of each step.
[0040] 1. Benchmark image preprocessing
[0041] 1.1 Generate a mask for the cropped image (cutted_img). This step prepares the reference data for subsequent template matching and mask fusion. This involves generating the mask and extracting the central region as the template image. Converting the color image to grayscale simplifies subsequent processing. Binarize the grayscale image by setting a threshold to generate the mask. For a binary mask, a pixel value ≥ the threshold is considered background (mask value 0); a pixel value < the threshold is considered foreground (mask value 1).
[0042] 1.2 Extract the center area of the reference image: Divide the image into a 3×3 grid and extract the central area as the template image (template_img). In this embodiment, a 3×3 grid can be used, evenly dividing the image into 9 areas (3 rows × 3 columns). The center grid (i.e., row 2, column 2) is extracted as the template image. The central area usually contains the main target and is more stable to transformations such as rotation and scaling.
[0043] 2. Template matching positioning
[0044] 2.1 Use a sliding window algorithm to calculate the pixel-by-pixel similarity between the template image and the original image (origin_img). Specifically, consider template_img as a "sliding window" and traverse all possible positions on origin_img with a fixed step size (usually 1 pixel). The similarity between the area within the window and the template is calculated.
[0045] Assume that origin_img is of size H × W and template_img is of size h × w (h ≤ H, w ≤ W). The sliding window search range is: horizontal: x∈[0, Ww], vertical: y∈[0, Hh]. Therefore, we need to calculate the similarity of a total of (H - h + 1) × (W - w + 1) positions, corresponding to the "multiple positions" in step S2.
[0046] 2.2 Obtain the maximum similarity value (max_val) and corresponding coordinates (max_loc) through normalized cross-correlation.
[0047] 3. Quality screening mechanism
[0048] 3.1 Set the similarity threshold (≥0.8) to filter low-quality matches.
[0049] 3.2 Dynamically adjust the matching area: When max_val ≥ 0.8, record the coordinates (x, y); otherwise, discard the current matching combination.
[0050] 4. Mask fusion processing
[0051] 4.1 Create an alpha channel (origin_alpha) of the same size as the original image.
[0052] Create the original image alpha channel (full background mask) and generate a blank transparency layer with the same size as the original image as the base for fusion.
[0053] 4.2 Map the cropped image mask (cutted_alpha) to the [y1:y2,x1:x2] coordinate interval of origin_alpha.
[0054] Based on the template matching coordinates (max_loc_x, max_loc_y), calculate the exact position of the cropped image in the original image and determine the rectangular area that needs to be filled. Assume that the size of the cropped image cutted_img is (h, w), and its alpha channel cutted_alpha has the same size.
[0055] Rectangular region coordinates:
[0056] Upper left corner coordinates: (x1, y1) = (max_loc_x-w / / 2, max_loc_y-h / / 2);
[0057] Coordinates of the lower right corner: (x2, y2) = (x1 + w, y1 + h);
[0058] 4.3 Output the final mask after fusion (output_mask).
[0059] Copies the alpha channel data of the cropped image to the corresponding area of the alpha channel of the original image.
[0060] Extract the valid area: Cut out a sub-area from cutted_alpha that matches the size of the target area [y1:y2,x1:x2].
[0061] Region alignment: copy the sub-region to the corresponding position of origin_alpha.
[0062] Boundary processing: If the cropped image exceeds the original image range, only the valid part is filled (the uncovered area remains transparent).
[0063] Figure 4 This is a flow chart of subject positioning based on template matching in this application.
[0064] 1. Benchmark image processing
[0065] 1.1 Extraction of the central area of the cropped image: Divide the cropped image (cutted_img) into a 3×3 grid and extract the central area as the template image (template_img).
[0066] 1.2 Sliding window matching: Perform a full image scan on the original image (origin_img) with a step size of 1 pixel, continuously calculate the similarity, and obtain the highest similarity value (max_val ≥ 0.8) and its coordinates (max_loc_x, max_loc_y).
[0067] 2. Graphic elements
[0068] 2.1 The nine-square grid dividing line of the cropped image (red dotted line).
[0069] Figure 5 Flowchart of mask synthesis and training data generation for this application.
[0070] 1. Coordinate mapping module
[0071] Generates a blank transparency layer of the same size as the original image to serve as a base for blending.
[0072] 1.1 Calculation of the expanded area: Using max_loc as a reference, calculate the position of the cropped image in the original image and generate a rectangular coordinate range (x1, y1, x2, y2). Based on the coordinates corresponding to the highest similarity value in step S2, calculate the position of the cropped image in the original image and determine the rectangular area to be filled.
[0073] 1.2 Full background mask creation: Generate a pure black matrix (origin_alpha) of the same size as the original image.
[0074] 2. Layer Fusion Module
[0075] 2.1 Alpha channel transplantation: Map the transparent channel of the cropped image (cutted_alpha) to the [y1:y2,x1:x2] interval of origin_alpha, and copy the alpha channel data of the cropped image to the corresponding area of the alpha channel of the original image.
[0076] 2.2 Cropping the original image based on the final mask to obtain a repaired cropped image.
Claims
1. An image processing method, characterized in that: The image processing method comprises: S1: Cropping the original image to obtain a cropped image, generating a cropping mask from the cropped image, dividing the cropped image into n×n regions, extracting a central region as a template image, where n is a positive integer and an odd number; S2: Matching the template image as a sliding window with the original image based on a preset step size, calculating similarity values of multiple positions in each window, and obtaining the highest similarity value and the coordinates corresponding to the highest similarity value; S3: when the highest similarity value is less than the preset highest similarity threshold, discard the current highest similarity value and the coordinates corresponding to the highest similarity value; when all the highest similarity values are less than the preset highest similarity threshold, return to step S1; S4: creating an Alpha channel with the same size as the original image, mapping the cropped image mask to the Alpha channel, and outputting a fused final mask.
2. The image processing method according to claim 1, wherein: S2 includes: The highest similarity value and the coordinates corresponding to the highest similarity value are calculated as follows: Where T(x, y) is the pixel value of the template image, I(x+u, y+v) is the pixel value of the sliding window with (u, v) as the upper left corner in the original image, is the mean of the template image, is the mean value of the original image at position (u, v), NCC(u, v) is the highest similarity value, and (u, v) is the coordinate corresponding to the highest similarity value.
3. The image processing method according to claim 1, wherein: S4 includes; S41: Generate a blank transparency layer with the same size as the original image as the fusion base; S42: Calculate the position of the cropped image in the original image and determine the rectangular area to be filled based on the coordinates corresponding to the highest similarity value in step S2; S43: Copying the Alpha channel data of the cropped image to a corresponding area of the Alpha channel of the original image.
4. The image processing method according to claim 1, wherein: n is 3, the highest similarity threshold is 0.8, and the preset step size is 1 pixel.
5. The image processing method according to claim 1, wherein: The image processing method further includes S5: cropping the original image based on the final mask to obtain a repaired cropped image.