Image color matting method, system, computer equipment and storage medium
1. An image color matting method, comprising:
acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image;
taking a pixel point needing color deduction in the HSV image as a reference pixel point, and acquiring an H component value, an S component value and a V component value corresponding to the reference pixel point;
calculating differences and tolerance values of corresponding components between the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating a difference ratio according to the differences and the tolerance values;
and calculating a transparency map according to the difference and the ratio of the difference, and performing fusion processing on the SRC image and the basic image according to the transparency map to obtain a target image.
2. The method for image color matting according to claim 1, wherein said calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image comprises:
calculating the value range of the V component by using the following formula: v ═ max (R, G, B);
and calculating the value range of the S component by using the V component:
when V ≠ 0, S ═ V-min (R, G, B))/V; when V is 0, S is 0;
and calculating the value range of the H component by using the V component:
when V ═ R, H ═ 60 ═ G-B)/(V-min (R, G, B));
when V ═ G, H ═ 120+60 ═ B-R)/(V-min (R, G, B));
when V is B, H is 240+60 (R-G)/(V-min (R, G, B)).
3. The method for image color matting according to claim 1, wherein the calculating differences and tolerance values of corresponding components between the H component, S component and V component of each pixel point in the HSV image and the H component, S component and V component of the reference pixel point, and calculating a difference ratio according to the differences and tolerance values comprises:
acquiring a low value range larger than 0 and a high value range lower than 360 in the red area of the HSV image, and normalizing the difference value according to the low value range and the high value range to obtain a normalized value;
calculating a reference range of the difference value between the H component of each pixel point and the H component of the reference pixel point and a tolerance value of the reference range;
and calculating the difference ratio of the difference value in the reference range and the range of the tolerance value according to the normalized value and the tolerance value.
4. The method for image color matting according to claim 1, wherein the calculating a transparency map according to the difference and the ratio of the differences comprises:
acquiring the minimum value and the maximum value of the S component and the minimum value and the maximum value of the V component of the reference pixel point;
normalizing the difference ratio to obtain a normalized difference ratio, and calculating the display ratio of each pixel point in the SRC image according to a preset calculation rule by combining the minimum value and the maximum value of the S component and the minimum value and the maximum value of the V component;
and carrying out standardization processing on the display proportion to obtain the transparency map.
5. The image color matting method according to claim 1, wherein the fusing the SRC image and the base image according to the transparency map to obtain an object image includes:
performing edge smoothing operation on the transparency graph to obtain an edge transparency graph;
and carrying out fusion processing on the SRC image and the basic image according to the edge transparency graph, and carrying out expansion operation and corrosion operation on the image subjected to fusion processing to obtain a target image.
6. The image color matting method according to claim 5, wherein the fusing the SRC image and a base image according to the edge transparency map includes:
the fusion process was performed according to the following formula:
combine(i,j)=src(i,j)*mask(i,j)+b_img(i,j)*(1-mask(i,j))
wherein SRC (i, j) is a pixel point in the SRC image, i is a row number, j is a column number, mask (i, j) is a transparency map corresponding to the pixel point in the SRC image, and b _ img (i, j) is one of the pixel points in the base image.
7. The image color matting method according to claim 4, wherein the normalizing the difference ratio includes:
normalizing the difference ratio according to the following formula:
value_h=min(value_h,1.0)
value_h=max(value_h,0.0)
wherein value _ h represents the ratio of the difference values;
the normalizing the display proportion comprises:
the display proportion is normalized according to the following formula:
mask(i,j)=min(mask(i,j),1.0)
mask(i,j)=max(mask(i,j),0.0)
wherein, mask (i, j) represents a pixel point in the transparency map.
8. An image color matting system, comprising:
the space conversion unit is used for acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image;
a reference pixel point component value obtaining unit, configured to use a pixel point in the HSV image, where color subtraction is required, as a reference pixel point, and obtain an H component value, an S component value, and a V component value corresponding to the reference pixel point;
the difference ratio calculation unit is used for calculating the difference and the tolerance value of corresponding components between the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating the difference ratio according to the difference and the tolerance value;
and the target image acquisition unit is used for calculating a transparency map according to the difference value and the difference ratio, and performing fusion processing on the SRC image and the base image according to the transparency map to obtain a target image.
9. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor implements the image color matting method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that the computer-readable storage medium stores a computer program which, when executed by a processor, causes the processor to execute the image color matting method according to any one of claims 1 to 7.
Background
Blue Screen technology (Blue Screen) is the most dominant means of extracting channels. The method is to shoot people or other foreground contents, and then remove a monochromatic background by using the difference of chromaticity. The blue screen technology has a scientific name Chroma key (Chroma Keying). Digital synthesis software allows a user to specify a color range, pixels with colors within this range are treated as background, and the corresponding Alpha channel value is set to 0; pixels outside this range are considered foreground and the corresponding Alpha channel value is set to 1. The primary principle is that the foreground object cannot contain the selected background color. In principle, any color can be used as the background as long as the color used for the background does not exist in the foreground picture, but in practice, both a blue background and a green background are most commonly used. The reason is that the natural color of human body does not contain the two colors, and the two colors are used as background and cannot be mixed with the human body; meanwhile, the two colors are primary colors in an RGB system, and are convenient to process.
To facilitate channel extraction during post-production, there are some problems to be noticed when performing blue (green) screen shots: firstly, the foreground object cannot contain the selected background color, and other background colors can be selected if necessary; secondly, the background color must be consistent, the illumination is uniform, and the background or illumination depth should be avoided as much as possible. In the middle of the process of making the color of scratching like this at the later stage, can obtain better effect.
The primary detection of the matting technology and the related color matting technology mainly carry out matting on a green curtain or a blue curtain, leave a corresponding foreground image and fuse the foreground image with a new background image. When performing color matting, firstly, the image color is subjected to space conversion, the image color is converted from BGR to HSV space, and the BGR of the pixels which are determined as the background color in the original image is all set to be 0 by setting the approximate range of the color H (hue) needing matting. However, most of the methods are directed to blue curtains and green curtains, and few methods are available for performing matting on any color. Most importantly, when the background of the green (blue) curtain is not uniform or the lighting condition is not ideal, the background is easy to remain or pixels in the foreground are directly scratched, and the edges are easy to have a sawtooth condition.
Disclosure of Invention
The embodiment of the invention provides an image color matting method, an image color matting system, computer equipment and a storage medium, and aims to solve the problem that matting colors in the prior art cannot be suitable for all background colors.
In a first aspect, an embodiment of the present invention provides an image color matting method, including:
acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image;
taking a pixel point needing color deduction in the HSV image as a reference pixel point, and acquiring an H component value, an S component value and a V component value corresponding to the reference pixel point;
calculating differences and tolerance values of corresponding components between the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating a difference ratio according to the differences and the tolerance values;
and calculating a transparency map according to the difference and the ratio of the difference, and performing fusion processing on the SRC image and the basic image according to the transparency map to obtain a target image.
In a second aspect, an embodiment of the present invention provides an image color matting system, which includes:
the space conversion unit is used for acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image;
a reference pixel point component value obtaining unit, configured to use a pixel point in the HSV image, where color subtraction is required, as a reference pixel point, and obtain an H component value, an S component value, and a V component value corresponding to the reference pixel point;
the difference ratio calculation unit is used for calculating the difference and the tolerance value of corresponding components between the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating the difference ratio according to the difference and the tolerance value;
and the target image acquisition unit is used for calculating a transparency map according to the difference value and the difference ratio, and performing fusion processing on the SRC image and the base image according to the transparency map to obtain a target image.
In a third aspect, an embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor, and the processor, when executing the computer program, implements the image color matting method according to the first aspect.
In a fourth aspect, the present invention further provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and the computer program, when executed by a processor, causes the processor to execute the image color matting method according to the first aspect.
The embodiment of the invention provides an image color matting method, a system, computer equipment and a storage medium, wherein the method comprises the following steps: acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image; taking a pixel point needing color deduction in the HSV image as a reference pixel point, and acquiring an H component value, an S component value and a V component value corresponding to the reference pixel point; calculating differences and tolerance values of corresponding components between the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating a difference ratio according to the differences and the tolerance values; and calculating a transparency map according to the difference and the ratio of the difference, and performing fusion processing on the SRC image and the basic image according to the transparency map to obtain a target image. According to the embodiment of the invention, the transparency map is obtained by calculating the difference value and the difference value ratio of each component value of each pixel point and the reference pixel point in the SRC image, and the SRC image and the basic image are fused by using the transparency map, so that the background color is more accurately scratched, and the application range is wider.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
Fig. 1 is a schematic flow chart of an image color matting method according to an embodiment of the present invention;
fig. 2 is a schematic block diagram of an image color matting system provided by an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It will be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It is also to be understood that the terminology used in the description of the invention herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in the specification of the present invention and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise.
It should be further understood that the term "and/or" as used in this specification and the appended claims refers to and includes any and all possible combinations of one or more of the associated listed items.
Referring to fig. 1, fig. 1 is a schematic flow chart of an image color matting method according to an embodiment of the present invention, where the method includes steps S101 to S104.
S101, acquiring an SRC image to be processed, converting the SRC image from a BGR color space into an HSV space to obtain an HSV image, and calculating an H component value, an S component value and a V component value corresponding to each pixel point in the HSV image;
s102, taking a pixel point needing color deduction in the HSV image as a reference pixel point, and acquiring an H component value, an S component value and a V component value corresponding to the reference pixel point;
s103, calculating difference values and tolerance values of corresponding components among the H component, the S component and the V component of each pixel point in the HSV image and the H component, the S component and the V component of the reference pixel point, and calculating difference value ratios according to the difference values and the tolerance values;
and S104, calculating a transparency map according to the difference and the ratio of the difference, and performing fusion processing on the SRC image and the base image according to the transparency map to obtain a target image.
In this embodiment, an SRC image to be processed is first acquired, color space conversion is then performed on the SRC image to obtain an HSV image, and then an H component value, an S component value, and a V component value corresponding to each pixel point in the HSV image are calculated; selecting HSV values of colors to be vacated, namely selecting a reference pixel point in the HSV image, taking the color corresponding to the reference pixel point as a deduction color, and calculating an H component value, an S component value and a V component value of the reference pixel point; calculating the difference and the tolerance value between each pixel point in the HSV image and each corresponding component of the reference pixel point, and calculating the ratio of the difference; and after the difference and the ratio of the difference are obtained, calculating a corresponding transparency map, selecting a new image as a basic image, and fusing the SRC image and the basic image according to the transparency map to obtain a target image so as to finish the color deduction processing of the image.
And performing color space conversion on the SRC image, namely converting the SRC image from an RGB color space into an HSV color space, wherein R in the RGB color space represents a red channel, G represents a green channel, B represents a blue channel, H in the HSV image represents hue, S represents saturation, and V represents brightness. Images acquired in a natural environment are easily affected by conditions such as natural illumination, foreign object occlusion and shadows, namely are sensitive to brightness. The three components of the RGB color space are strongly dependent on luminance, i.e. as soon as the luminance changes, the three components change accordingly. However, the human eye is not sensitive to the three color components, and in a monochrome, the human eye is least sensitive to red and most sensitive to blue, so the RGB color space is a color space with poor uniformity. If the similarity of colors is directly measured by Euclidean distance, the result has a large deviation from the human vision. For a certain color, it is difficult to infer more accurate values of three components for representation. Therefore, the RGB color space is suitable for the display system but not for image processing, and therefore, the SRC image is first converted from the RGB image format into the HSV image for processing the image.
In one embodiment, the calculating the H component value, the S component value and the V component value corresponding to each pixel point in the HSV image includes:
calculating the value range of the V component by using the following formula: v ═ max (R, G, B);
and calculating the value range of the S component by using the V component:
when V ≠ 0, S ═ V-min (R, G, B))/V; when V is 0, S is 0;
and calculating the value range of the H component by using the V component:
when V ═ R, H ═ 60 ═ G-B)/(V-min (R, G, B));
when V ═ G, H ═ 120+60 ═ B-R)/(V-min (R, G, B));
when V is B, H is 240+60 (R-G)/(V-min (R, G, B)).
In this embodiment, the value of each component in the HSV image is calculated by using the above formula. When RGB color space conversion is carried out, the value range of each channel of RGB should be normalized to 32-bit floating point number, namely the change range of the value of each channel is 0-1. At this time, V is max (R, G, B), then values of an S component and an H component are calculated according to the value of the V component, and values of components of the HSV image corresponding to the SRC image are obtained through calculation, wherein the calculated value range of the H component is 0 to 360, the value range of the S component is 0 to 1, and the value range of the V component is 0 to 1.
In an embodiment, the calculating differences and tolerance values of corresponding components between the H component, the S component, and the V component of each pixel point in the HSV image and the H component, the S component, and the V component of the reference pixel point, and calculating a difference ratio according to the differences and tolerance values includes:
acquiring a low value range larger than 0 and a high value range lower than 360 in the red area of the HSV image, and normalizing the difference value according to the low value range and the high value range to obtain a normalized value;
calculating a reference range of the difference value between the H component of each pixel point and the H component of the reference pixel point and a tolerance value of the reference range;
and calculating the difference ratio of the difference value in the reference range and the range of the tolerance value according to the normalized value and the tolerance value.
In this embodiment, a high value range and a low value range of a red region in the HSV image are obtained, normalization processing is performed, then a difference value of an H component between each pixel point and a reference pixel point, a reference range of the difference value, and a tolerance value are calculated, and a difference value ratio is calculated according to the normalization value and the tolerance value.
Specifically, in the HSV image, the red region spans the hue ranges of 0 and 360, and therefore, the value range of the red region is calculated first during calculation. Let control _ h _ low be the range of red color when the hue range is greater than 0 and control _ h _ high be the range of red color when the hue range is less than 360. After debugging and verification, when the control _ h _ low is 10, the red display effect is better, namely, a hue area between 0 and 10 is judged as a red area; when control _ h _ high is 330, the red display effect is better, i.e. the hue region between 330 and 360 is determined as the red region.
Setting the reference range of the difference value between the H component value of any pixel point in the HSV image and the H component value of the reference pixel point as range, and setting the tolerance value as offset. And taking the pixel points with the difference value smaller than the sum of the reference range and the tolerance value as candidate pixel points which can be scratched, and taking the pixel points with the difference value larger than the sum of the reference range and the tolerance value as foreground pixel points. The range and the offset can be 1-360 (the hue range is 0-360), but for better removing the designated color, the range and the offset need to be reduced and rationalized. According to the RGB color circle, the three primary colors of red, green and blue are respectively represented at 0(360), 120 and 240, and the positions where the color change in the middle is obvious are basically 30, 60 and 90, so that range and offset are verified within 90 and 60 respectively. The verification result shows that when the range is between 20 and 80 and the offset is any value between 20 and 40 in the visual effect of image matting, the color display effect is not obvious and the effect is good.
When hue < ═ control _ h _ low and h > -control _ h _ high:
dif_h=(360.0-h+hue)/360
value_h=(range_-abs(dif_h)+offset_)/offset_
when hue > control _ h _ high and h < ═ control _ h _ low:
dif_h=(360-hue+h)/360.0;
value_h=(range_-abs(dif_h)+offset_)/offset_
otherwise:
dif_h=abs(h-hue)/360;
value_h=(range_-abs(dif_h)+offset_)/offset_
the range _ range/360, the offset _ range/360, the range _ range is a standard range after normalization, the offset _ range is a tolerance value after normalization, the dif _ H represents a normalization value (the normalization value is a value after normalization of an absolute value of a difference value between an H component of a pixel point in an HSV image and an H component of a reference pixel point, wherein the value range of H is 0-360), and the value _ H represents a ratio of the difference value.
In an embodiment, the calculating the transparency map according to the difference and the ratio of the differences includes:
acquiring the minimum value and the maximum value of the S component and the minimum value and the maximum value of the V component of the reference pixel point;
normalizing the difference ratio to obtain a normalized difference ratio, and calculating the display ratio of each pixel point in the SRC image according to a preset calculation rule by combining the minimum value and the maximum value of the S component and the minimum value and the maximum value of the V component;
and carrying out standardization processing on the display proportion to obtain the transparency map.
In this embodiment, the display ratio of each pixel point is calculated according to the maximum value and the minimum value of the S component, the maximum value and the minimum value of the V component, and the normalized difference ratio, and the transparency map is calculated according to the display ratio. In the transparency map, if the pixel value is 0, the pixel point is represented as a background, if the pixel value is 1, the pixel point is represented as a foreground, and if the pixel value is between 0 and 1, the transition condition between the foreground and the background at the edge is represented.
The maximum value and the minimum value of the S component are denoted as control _ S _ high and control _ S _ low, respectively, and the maximum value and the minimum value of the V component are denoted as control _ V _ high and control _ V _ low, respectively. In this embodiment, control _ s _ low is set to 0.15, control _ s _ high is set to 0.3, control _ v _ low is set to 0.2, and control _ v _ high is set to 0.4. Continuously adjusting the sizes of the four values by utilizing prestored image data, wherein the image data comprises an image with less influence of the scratched color on saturation and brightness and uniform visual color (pure color) and also comprises an image with nonuniform scratched color (the visual effect obviously shows the same color but different shades); in the process of color matting, colors of different positions of the non-uniform background can be selected for matting. The larger the control _ s _ high or control _ v _ high (the larger the control _ s _ low and control _ v _ low, respectively), the more pixels in the picture can be scratched, and conversely the fewer. Since the average values of the S component and the V component are between 0 and 1, the ranges of the control _ S _ low and the control _ S _ high are both between 0 and 1, and the ranges of the control _ V _ low and the control _ V _ high are also between 0 and 1. After a plurality of parameter adjustments and verifications, in this embodiment, the difference between control _ s _ low and control _ s _ high is kept to be 0.15, the difference between control _ v _ low and control _ v _ high is kept to be 0.2, and the difference between control _ s _ low is constantly less than control _ v _ low by 0.05, so that the obtained effect is better.
For any pixel point I (I, j), I represents a row number, j represents a column number:
when value _ h < ═ 0.1: mask (i, j) ═ 1.0;
when value _ h > is 0.9:
if: dif _ s < (control _ s _ low) and dif _ v < (control _ v _ high)
Then mask (i, j) is 0.0
If: control _ s _ high or dif _ v > control _ v _ high
Then mask (i, j) is 1.0
Otherwise: a mask (i, j) — (control _ s _ high-dif _ s)/(control _ s _ high-control _ s _ low) ((control _ v _ high-dif _ v)/(control _ v _ high-control _ v _ low));
when 0.1< value _ h < 0.9:
if: dif _ s > control _ s _ high or dif _ v > control _ v _ high
Then mask (i, j) is 1.0
Otherwise: a mask (i, j) ═ value _ h (control _ s _ high-dif _ s)/(control _ s _ high-control _ s _ low) ((control _ v _ high-dif _ v)/(control _ v _ high-control _ v _ low));
the mask (I, j) is a value of a certain pixel point in the transparency map, value _ h represents a difference value ratio, Dif _ S refers to an absolute value of a difference value between an S component value of a reference pixel point and an S component value of a pixel point I, and Dif _ V is an absolute value of a difference value between a V component value of the reference pixel point and a V component value of the pixel point I.
In an embodiment, the fusing the SRC image and the base image according to the transparency map to obtain a target image includes:
performing edge smoothing operation on the transparency graph to obtain an edge transparency graph;
and carrying out fusion processing on the SRC image and the basic image according to the edge transparency graph, and carrying out expansion operation and corrosion operation on the image subjected to fusion processing to obtain a target image.
In this embodiment, an edge smoothing process is performed on the transparency map to obtain an edge transparency map, so that a target image obtained when the SRC image and the base image are fused according to the edge transparency map is smoother, and then an expansion operation and a corrosion operation are performed to obtain a final target image.
The edge smoothing process may employ the following method: and smoothing the transparency graph by adopting a 3-by-3 window, and performing window dividing treatment on each pixel point in the transparency graph by taking the pixel point as a center. For example, taking a pixel point with a pixel value of 0.3 as a center, a window covering 3 × 3 is calculated as follows: value ═ (0.3 × 1+0.4 × 1+0.15 × 1+0.01 × 1+0.3 × 1+0.58 × 1+0.77 × 1+0.14 × 1+0.35 × 1)/9, and 0.3 was replaced with the calculated Value. And then, the window is divided, a pixel point is moved rightwards, and the next value is calculated through the formula to replace the pixel point. By parity of reasoning, the window division is performed sequentially from the initial pixel point of the transparency map to the right until the edge, and then the window division is performed sequentially by taking the pixel point of the initial pixel point right below the transparency map as the center and sliding to the right until the edge.
And (3) expansion operation: and for each pixel point, solving the maximum value in the window taking the pixel point as the center, and assigning the maximum value to the center pixel of the current window. And (3) corrosion operation: and (4) contrary to the expansion operation, for each pixel point, solving the minimum value in the window taking the pixel point as the center, and assigning the minimum value to the center pixel of the current window.
In an embodiment, the fusing the SRC image and the base image according to the edge transparency map includes:
the fusion process was performed according to the following formula:
combine(i,j)=src(i,j)*mask(i,j)+b_img(i,j)*(1-mask(i,j))
wherein SRC (i, j) is a pixel point in the SRC image, i is a row number, j is a column number, mask (i, j) is a transparency map corresponding to the pixel point in the SRC image, and b _ img (i, j) is one of the pixel points in the base image.
In this embodiment, image fusion is performed according to the above formula, because the SRC image is a multi-channel image (R, G, B three color channels) at the time of input, the calculation of the above formula is performed for each channel, and the resulting combination is also an image of the same number of channels as the SRC image. The Mask image may be a single channel, i.e. one pixel has only one value, or may be an image with the same number of channels as the SRC image, and if the number of channels is greater than 1 channel, the channel value used to represent each pixel is the same.
In an embodiment, the normalizing the difference ratio includes:
normalizing the difference ratio according to the following formula:
value_h=min(value_h,1.0)
value_h=max(value_h,0.0)
wherein value _ h represents the ratio of the difference values;
the normalizing the display proportion comprises:
the display proportion is normalized according to the following formula:
mask(i,j)=min(mask(i,j),1.0)
mask(i,j)=max(mask(i,j),0.0)
wherein, mask (i, j) represents a pixel point in the transparency map.
In this embodiment, normalization processing is required after obtaining the difference ratio and the display ratio, so as to avoid data overflow. The rule of the normalization processing is as follows: when the data is more than 1, the data is regarded as 1; when the data is less than 0, the data is regarded as 0; and when the data is more than 0 and less than 1, the actual value of the data is fetched. And carrying out normalization processing on the data through the rule, so that the value range of the data is controlled to be kept between 0 and 1.
Referring to fig. 2, fig. 2 is a schematic block diagram of an image color matting system according to an embodiment of the present invention, where the image color matting system 200 includes:
a space conversion unit 201, configured to acquire an SRC image to be processed, convert the SRC image from a BGR color space into an HSV space, obtain an HSV image, and calculate an H component value, an S component value, and a V component value corresponding to each pixel point in the HSV image;
a reference pixel point component value obtaining unit 202, configured to use a pixel point in the HSV image, which needs to be color-subtracted, as a reference pixel point, and obtain an H component value, an S component value, and a V component value corresponding to the reference pixel point;
a difference ratio calculation unit 203, configured to calculate differences and tolerance values of corresponding components between the H component, the S component, and the V component in each pixel point in the HSV image and the H component, the S component, and the V component of the reference pixel point, and calculate a difference ratio according to the differences and the tolerance values;
and the target image obtaining unit 204 is configured to calculate a transparency map according to the difference and the ratio of the difference, and perform fusion processing on the SRC image and the base image according to the transparency map to obtain a target image.
In one embodiment, the spatial transform unit 201 includes:
the V component value taking unit is used for calculating the value range of the V component by using the following formula: v ═ max (R, G, B);
and the S component value taking unit is used for calculating the value range of the S component by utilizing the V component:
when V ≠ 0, S ═ V-min (R, G, B))/V; when V is 0, S is 0;
and the H component value taking unit is used for calculating the value range of the H component by utilizing the V component:
when V ═ R, H ═ 60 ═ G-B)/(V-min (R, G, B));
when V ═ G, H ═ 120+60 ═ B-R)/(V-min (R, G, B));
when V is B, H is 240+60 (R-G)/(V-min (R, G, B)).
In an embodiment, the difference ratio calculating unit 203 includes:
a normalization value acquisition unit, configured to acquire a low value range greater than 0 and a high value range less than 360 in a red color region in the HSV image, and normalize the difference value according to the low value range and the high value range to obtain a normalization value;
a tolerance value calculation unit for calculating a reference range of a difference value between the H component of each pixel point and the H component of the reference pixel point and a tolerance value of the reference range;
and the difference ratio calculation unit is used for calculating the difference ratio of the difference in the reference range and the range of the tolerance value according to the normalized value and the tolerance value.
In one embodiment, the target image acquiring unit 204 includes:
a reference pixel component value obtaining unit, configured to obtain a minimum value and a maximum value of an S component and a minimum value and a maximum value of a V component of the reference pixel;
a display ratio obtaining unit, configured to perform normalization processing on the difference ratio to obtain a normalized difference ratio, and calculate, according to a preset calculation rule, a display ratio of each pixel point in the SRC image by combining the minimum value and the maximum value of the S component and the minimum value and the maximum value of the V component;
and the transparency map acquisition unit is used for carrying out standardization processing on the display proportion to obtain the transparency map.
In an embodiment, the target image obtaining unit 204 further includes:
the edge transparency graph acquiring unit is used for carrying out edge smoothing processing operation on the transparency graph to obtain an edge transparency graph;
and the post-processing unit is used for carrying out fusion processing on the SRC image and the basic image according to the edge transparency map, and carrying out expansion operation and corrosion operation on the fused image to obtain a target image.
In an embodiment, the target image obtaining unit 204 further includes:
the formula calculation unit is used for carrying out fusion processing according to the following formula:
combine(i,j)=src(i,j)*mask(i,j)+b_img(i,j)*(1-mask(i,j))
wherein SRC (i, j) is a pixel point in the SRC image, i is a row number, j is a column number, mask (i, j) is a transparency map corresponding to the pixel point in the SRC image, and b _ img (i, j) is one of the pixel points in the base image.
In one embodiment, the difference ratio normalization processing unit includes:
the first normalization processing unit is used for normalizing the difference ratio according to the following formula:
value_h=min(value_h,1.0)
value_h=max(value_h,0.0)
wherein value _ h represents the ratio of the difference values;
the display proportion normalization processing unit comprises:
the second normalization processing unit is used for normalizing the display proportion according to the following formula:
mask(i,j)=min(mask(i,j),1.0)
mask(i,j)=max(mask(i,j),0.0)
wherein, mask (i, j) represents a pixel point in the transparency map.
The embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the computer program, the processor implements the image color matting method as described above.
An embodiment of the present invention further provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the method for image color matting is implemented as described above.
The embodiments are described in a progressive manner in the specification, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments are referred to each other. For the system disclosed by the embodiment, the description is relatively simple because the system corresponds to the method disclosed by the embodiment, and the relevant points can be referred to the method part for description. It should be noted that, for those skilled in the art, it is possible to make various improvements and modifications to the present invention without departing from the principle of the present invention, and those improvements and modifications also fall within the scope of the claims of the present invention.
It is further noted that, in the present specification, relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.