In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use OpenCV in C++ to achieve linear mixing operation, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article hope you can get something.
Linear mixed operation
That is, the operation of mixing two images into one image.
Mathematical principle of Linear mixing of Images
G (x) = (1mura) F (x) + aQ (x)
Note:
1the value of "a" ranges from 0 to 1.
2Jing F (x) and Q (x) are the two images that participate in the mixing, and G (x) represents the output image.
3. The final output image is obtained by linearly weighting each pixel value of the two images.
4, the size and type of the two images must be exactly the same, if the image is regarded as a matrix
Then the premise of the addition of the two matrices is that the dimensions must be the same, otherwise there is no sense of addition.
AddWeighted function
The API for linear mixing operations in OpenCV is the addWeighted function.
Void addWeighted (InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
The first parameter, src1 of type InputArray, represents the first array to be weighted, often filled with a Mat.
The second parameter, alpha, represents the weight of the first array
The third parameter, src2, represents the second array, which needs to have the same size and number of channels as the first array.
The fourth parameter, beta, represents the weight value of the second array.
The fifth parameter, dst, is the output array, which has the same size and number of channels as the two input arrays.
The sixth parameter, gamma, is a scalar value added to the sum of weights. If you look at the following formula, you will understand.
The seventh parameter, dtype, outputs the optional depth of the array, with a default value of-1. When two input arrays have the same depth, this parameter is set to-1 (the default), which is equivalent to src1.depth ().
Code demonstration
New project
Create a new project opencv-0006, configure properties (VS2017 configure OpenCV common properties), and then write # include and main methods in the source file.
Load Picture
Then we loaded the image for display, and we needed two data sources this time, so we loaded two images.
And then we'll show it and see how it works.
OK, there is no problem with displaying the picture.
AddWeightedlinear mixing operation
When we operate addWeighted, the two data sources must be images of the same size, so I output the next one before you operate to see if the height and width of the two images are the same.
After running, it is found that there is a difference in the width of the two images, which must have made an error when executing addweighted. So we need to add a line of adjustment code, if it is judged that there is a discrepancy between width and height, then the second image is scaled to the size of the first image.
The API of image scaling is resize
CV_EXPORTS_W void resize (InputArray src, OutputArray dst
Size dsize, double fx=0, double fy=0
Int interpolation=INTER_LINEAR)
Next, we add the judgment of scaling to convert the image of src2 to the size of src1.
So we can do the linear mixing operation.
The defined alpha is the weight of two images. We use 0.5 specific gravity, and the second image will use 1-0.5 effect, that is, the two specific gravity should add up to 1. 5%.
Let's take a look at the display.
The middle image is the image we generated through linear blending, and then we change the weight to see the effect of the display.
Change the weight of the first image to 0.2, and the effect displayed is
It is obvious that the second picture is relatively clear.
Let's change the weight of the first graph to 0.7.
The above is how to use OpenCV to achieve linear mixing operation in C++. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.