Picture matting method and device based on Ali cloud visual intelligent open platform
By performing equal-scale scaling, cutting and restoring pictures over 2000×2000 pixels, the quality loss problem caused by image size limitation in the prior art is solved, and high-quality cutout effect and efficient picture processing are achieved.
Patent Information
- Application Number
- CN202510108358.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-23
- Publication Date
- 2025-05-30
AI Technical Summary
The cutout function of the existing Alibaba Cloud Vision Intelligent Open Platform has a limit of 2000×2000 pixels on the image size, which leads to users need to compress images to meet the requirements, thus sacrificing image quality.
By determining whether the pixels of the image exceed 2000×2000, if they exceed, use the equal-scale scaling algorithm to reduce the image to a size that meets the platform requirements. After performing the cutout operation, restore the scaled mask image to the original size and merge it with the original image to obtain the final cutout result.
Without sacrificing image quality, high-quality cutout effect is achieved, image processing efficiency is improved, and the powerful functions of Alibaba Cloud Vision Intelligent Open Platform is used to achieve accurate cutout effect.
Smart Images

Figure CN120070466A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image matting, and particularly to an image matting method and device based on the Alibaba Cloud Vision Intelligence Open Platform. Background Art
[0002] In the prior art, the Alibaba Cloud Vision Intelligence Open Platform provides a segmentation and matting function, but this service does have certain limitations when processing images. Specifically, the platform requires that the size of the input image must be less than 2000×2000 pixels, that is, the longest side does not exceed 1999 pixels. This limitation has caused many users to be unable to directly use this function to process the high-definition images in their hands, because most existing image resolutions exceed this limit.
[0003] To solve this problem, users have to take some compromise measures. For example, they first compress the image to a size that meets the platform requirements, and then use the platform's segmentation and matting function for processing. However, this approach will inevitably sacrifice the quality of the image, especially some details and clarity may be lost during the compression process. Summary of the Invention
[0004] The technical problem to be solved by the present invention is to provide an image matting method and device based on the Alibaba Cloud Vision Intelligence Open Platform, which is convenient for users to use and ensures the image quality.
[0005] In a first aspect, the present invention provides an image matting method based on the Alibaba Cloud Vision Intelligence Open Platform, including the following steps:
[0006] Step 1: Judge the pixels of the image uploaded by the user. If the pixels of the image are less than 2000×2000, directly perform a matting operation through the Vision Intelligence Open Platform to obtain the required image; if the pixels of the image are greater than or equal to 2000×2000, proceed to Step 2;
[0007] Step 2: Scale the image proportionally through a scaling algorithm to obtain a scaled image;
[0008] Step 3: Perform a matting operation on the scaled image through the Vision Intelligence Open Platform to obtain a first mask image;
[0009] Step 4: Restore the first mask image to a second mask image of the original size using the scaling algorithm;
[0010] Step 5: Merge the second mask image with the image uploaded by the user to obtain the required matting.
[0011] In a second aspect, the present invention provides an image matting device based on the Alibaba Cloud Vision Intelligence Open Platform, including:
[0012] A judgment module that judges the pixels of the image uploaded by the user. If the pixels of the image are less than 2000×2000, directly perform a matting operation through the Visual Intelligence Open Platform to obtain the required image; if the pixels of the image are greater than or equal to 2000×2000, enter the scaling module;
[0013] A scaling module that scales the image proportionally through a scaling algorithm to obtain a scaled image;
[0014] An operation module that performs a matting operation on the scaled image through the Visual Intelligence Open Platform to obtain a first mask image;
[0015] A restoration module that restores the first mask image to the second mask image of the original size using a scaling algorithm;
[0016] A matting module that merges the second mask image with the image uploaded by the user to obtain the required matting.
[0017] One or more technical solutions provided by the present invention have at least the following technical effects or advantages:
[0018] By performing matting on the image after shrinking using the Visual Intelligence Open Platform to obtain a mask image, and then restoring it to the original proportion and synthesizing it with the original image, a high-quality matting effect can be achieved without sacrificing the image quality; effectively combining image shrinking, matting, restoration, and synthesis technologies, maintaining the image quality, and utilizing the powerful functions of the Alibaba Cloud Visual Intelligence Open Platform to achieve an accurate matting effect, greatly improving the image processing efficiency.
[0019] The above description is only an overview of the technical solution of the present invention. In order to be able to understand the technical means of the present invention more clearly, it can be implemented according to the content of the specification. And in order to make the above and other purposes, features, and advantages of the present invention more obvious and understandable, the following specifically gives the specific embodiments of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The present invention will be further described below with reference to the accompanying drawings in conjunction with embodiments.
[0021] Figure 1 It is a flowchart of the method in Embodiment 1 of the present invention;
[0022] Figure 2 It is a structural schematic diagram of the device in Embodiment 2 of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0023] By providing an image matting method and device based on the Alibaba Cloud Visual Intelligence Open Platform in the embodiments of the present application, the technical problem that cannot be used when exceeding the set pixels in the prior art is solved, which is greatly convenient for users to use and increases the user's options.
[0024] The general idea of the technical solution in the embodiments of this application is as follows:
[0025] Due to the cropping size limit of the Alibaba Cloud Vision Intelligence Open Platform, when the longest side exceeds 2000 pixels, it is necessary to perform proportional scaling. Call Imgproc.resize and use the Imgproc.INTER_LANCZOS4 algorithm to perform image proportional scaling
[0026] Call Imgproc.resize and use the Imgproc.INTER_LANCZOS4 algorithm to perform image proportional scaling. This algorithm can reduce the influence of artifacts while maintaining the edge sharpness.
[0027] Obtain the cropping result based on the black and white image + original image:
[0028] a. Take out the alpha channel of the original image. Call Core.min, which will take the minimum value of the mask and the alpha channel of the original image to ensure that the original information of the alpha channel is retained. Through this step of processing, the lines of the obtained cropping can be made more perfect;
[0029] b. Remove the alpha channel of the original image and add the black and white image as the new alpha channel for layer merging;
[0030] c. Set the color value of the transparent area to black. This step is to reduce the size of the cropping result image and save storage costs.
[0031] The core code is as follows:
[0032] / / Create a fully transparent matrix with the same size as the original image for comparison
[0033] Mat compareAlpha = new Mat(outImg.size(), CvType.CV_8UC1, Scalar.all(0.0));
[0034] / / Used to save the comparison result
[0035] Mat compareResult = new Mat();
[0036] / / Compare the alpha channel of the original image and alpha. The positions where the value in the obtained mask is 0 are the transparent positions in the original image
[0037] Core.compare(outPlanes.get(3), compareAlpha, compareResult, Core.CMP_EQ);
[0038] / / Create a completely black matrix with the same size as the original image
[0039] Mat black = new Mat(outImg.size(), outImg.type(), Scalar.all(0));
[0040] / / Copy the black color in black to the corresponding position in outImg according to the mask
[0041] Core.bitwise_and(black, outImg, outImg, compareResult);
[0042] Example 1
[0043] As Figure 1 shown, this embodiment provides a method for image matting based on the Alibaba Cloud Vision Intelligence Open Platform, including the following steps:
[0044] Step 1: Judge the pixels of the image uploaded by the user. If the pixels of the image are less than 2000×2000, directly perform the matting operation through the Vision Intelligence Open Platform to obtain the required image; if the pixels of the image are greater than or equal to 2000×2000, go to Step 2;
[0045] Step 2: Scale the image proportionally through a scaling algorithm to obtain a scaled image;
[0046] Step 3: Perform the matting operation on the scaled image through the Vision Intelligence Open Platform to obtain a first mask image;
[0047] Step 4: Restore the first mask image to a second mask image of the original size using the scaling algorithm;
[0048] Step 5: Merge the second mask image with the image uploaded by the user to obtain the required matted image.
[0049] In this embodiment, preferably, Step 2 is specifically: Use the Imgproc.resize in OpenCV with the Imgproc.INTER_LANCZ0S4 algorithm to scale the image proportionally to obtain a scaled image.
[0050] In this embodiment, preferably, Step 4 is specifically: Use the Imgproc.resize in OpenCV with the Imgproc.INTER_LANCZ0S4 algorithm to restore the first mask image to a second mask image of the original size proportionally.
[0051] In this embodiment, preferably, step 5 is specifically as follows: read the alpha channels of each pixel in the image uploaded by the user to obtain a first matrix, read the alpha channels of each pixel in the second mask image to obtain a second matrix, call Core.min to merge the first matrix and the second matrix to obtain a third matrix; replace the alpha channel in the image uploaded by the user with the third matrix to obtain the required image.
[0052] In this embodiment, preferably, it further includes step 6: set the color values of the transparent regions in the required image to black and then store them; through this setting, the storage space can be saved.
[0053] Based on the same inventive concept, the present application also provides a device corresponding to the method in Embodiment 1, details of which can be found in Embodiment 2.
[0054] Embodiment 2
[0055] As Figure 2 shown, in this embodiment, a picture matting device based on the Alibaba Cloud Vision Intelligence Open Platform is provided, including:
[0056] A judgment module that judges the pixels of the image uploaded by the user. If the pixels of the image are less than 2000×2000, directly perform matting operation through the Vision Intelligence Open Platform to obtain the required image; if the pixels of the image are greater than or equal to 2000×2000, enter the scaling module;
[0057] A scaling module that scales the image proportionally through a scaling algorithm to obtain a scaled image;
[0058] An operation module that performs a matting operation on the scaled image through the Vision Intelligence Open Platform to obtain a first mask image;
[0059] A reduction module that reduces the first mask image to the original size of the second mask image by using the Imgproc.INTER_LANCZ0S4 algorithm in OpenCV;
[0060] A matting module that combines the second mask image with the image uploaded by the user to obtain the required matting.
[0061] In this embodiment, preferably, the scaling module is specifically as follows: use the Imgproc.resize in OpenCV with the Imgproc.INTER_LANCZ0S4 algorithm to scale the image proportionally to obtain a scaled image.
[0062] In this embodiment, preferably, the reduction module is specifically as follows: use the Imgproc.resize in OpenCV with the Imgproc.INTER_LANCZ0S4 algorithm to restore the first mask image to the original size of the second mask image.
[0063] In this embodiment, preferably, the matte extraction module specifically: reads the alpha channel of each pixel in the image uploaded by the user to obtain a first matrix, reads the alpha channel of each pixel in the second mask image to obtain a second matrix, calls Core.min to merge the first matrix and the second matrix to obtain a third matrix; replaces the alpha channel in the image uploaded by the user with the third matrix to obtain the required image.
[0064] In this embodiment, preferably, it further includes a compression module that sets the color values of the transparent areas in the required image to black and then stores it; by this setting, storage space can be saved.
[0065] Since the device described in the second embodiment of the present invention is the device adopted for implementing the method of the first embodiment of the present invention, based on the method described in the first embodiment of the present invention, those skilled in the art can understand the specific structure and variations of the device, so it will not be elaborated here. Any device adopted for the method of the first embodiment of the present invention falls within the scope of protection of the present invention.
[0066] Although the specific embodiments of the present invention have been described above, those skilled in the art should understand that the specific embodiments we described are illustrative rather than used to limit the scope of the present invention. Equivalent modifications and variations made by those skilled in the art in accordance with the spirit of the present invention should be covered by the scope of protection of the claims of the present invention.
Claims
1. A method for image cutout based on Alibaba Cloud Visual Intelligence Open Platform, characterized in that: The steps include: Step 1: The pixels of the picture uploaded by the user are judged. If the pixels of the picture are less than 2000×2000, the image is cut out directly through the visual intelligence open platform to obtain the required image; if the pixels of the picture are greater than or equal to 2000×2000, step 2 is entered; Step 2: Use a scaling algorithm to scale the image in equal proportion to obtain a scaled image; Step 3: Perform a cutout operation on the zoomed image through the visual intelligence open platform to obtain a first mask image; Step 4: Restore the first mask image to a second mask image of original size using a scaling algorithm; Step 5: Merge the second mask image with the image uploaded by the user to obtain the desired cutout.
2. The image cutout method based on Alibaba Cloud Visual Intelligence Open Platform according to claim 1, characterized in that: The step 2 is specifically: using Imgproc.resize in OpenCV and using the Imgproc.INTER_LANCZ0S4 algorithm to scale the image in equal proportion to obtain a scaled image.
3. The image cutout method based on Alibaba Cloud Visual Intelligence Open Platform according to claim 1, characterized in that: The step 4 is specifically as follows: the first mask image is restored to a second mask image of original size in proportion using the Imgproc.resize algorithm in OpenCV and the Imgproc.INTER_LANCZ0S4 algorithm.
4. The image cutout method based on Alibaba Cloud Visual Intelligence Open Platform according to claim 1, characterized in that: The step 5 specifically includes: reading out the transparent channel of each pixel in the picture uploaded by the user to obtain a first matrix, reading out the transparent channel of each pixel in the second mask image to obtain a second matrix, calling Core.min to merge the first matrix with the second matrix to obtain a third matrix; The transparent channel in the picture uploaded by the user is replaced by the third matrix to obtain the desired image.
5. The image cutout method based on Alibaba Cloud Visual Intelligence Open Platform according to claim 1, characterized in that: The method further includes step 6 of setting the color values of the transparent areas in the required image to black, and then storing the image.
6. A picture cutout device based on Alibaba Cloud visual intelligence open platform, characterized in that: include: The judgment module judges the pixels of the picture uploaded by the user. If the pixels of the picture are less than 2000×2000, the visual intelligence open platform directly performs a cutout operation to obtain the required image; If the image's pixels are greater than or equal to 2000×2000, it enters the scaling module; The scaling module scales the image in equal proportions through a scaling algorithm to obtain a scaled image; The operation module performs a cutout operation on the zoomed image through the visual intelligence open platform to obtain a first mask image; A restoration module, which restores the first mask image to a second mask image of the original size by using a scaling algorithm; The cutout module merges the second mask image with the image uploaded by the user to obtain the required cutout image.
7. The image cutout device based on Alibaba Cloud Visual Intelligence Open Platform according to claim 6, characterized in that: The scaling module specifically includes: using Imgproc.resize in OpenCV and using Imgproc.INTER_LANCZ0S4 algorithm to scale the image in equal proportion to obtain a scaled image.
8. The image cutout method based on Alibaba Cloud Visual Intelligence Open Platform according to claim 6 is characterized in that: The restoration module specifically includes: using Imgproc.resize in OpenCV and Imgproc.INTER_LANCZ0S4 algorithm to proportionally restore the first mask image to a second mask image of the original size.
9. The image cutout device based on Alibaba Cloud Visual Intelligence Open Platform according to claim 6, characterized in that: The cutout module specifically reads out the transparent channel of each pixel in the picture uploaded by the user to obtain a first matrix, reads out the transparent channel of each pixel in the second mask image to obtain a second matrix, and calls Core.min to merge the first matrix with the second matrix to obtain a third matrix; The transparent channel in the picture uploaded by the user is replaced by the third matrix to obtain the desired image.
10. The image cutout device based on Alibaba Cloud Visual Intelligence Open Platform according to claim 6, characterized in that: The invention also includes a compression module, which sets the color values of the transparent areas in the required image to black and then stores the image.