Probe into the watermark rendering and refraction filter of raster image science

Source: Internet
Author: User
Tags square root

Actually is the winter vacation review high number when groped out some small thing, has been wanting to tidy up the process, today is here to do some simple discussion.

We can often see some image processing programs that simulate the effects of the watermark, and must try to explain how they are implemented. I have read some related discussions or blog posts on the Internet, and I have gained a lot of inspiration from them. Today, in this essay concise introduction of my on the watermark rendering algorithm thinking and implementation of it.

In the image to simulate the refraction produced by the wave, at first glance it really makes people have a sense of awe, after all, the wave of this thing seems to be complex and messy, let alone to analyze the light refraction of each place? Of course, in order for this seemingly complex problem to be solved, we must first of all have a certain understanding of the physics laws contained in it. When it comes to the watermark, we have to mention mechanical waves. The watermark is a mechanical wave of the embodiment of, of course, obey the mechanical wave nature and law. Secondly, when it comes to mechanical waves, we need to talk about simple harmonic motion, simple harmonic is a common mechanical wave, both in the application and scientific research field has a very important position. There is the optical refraction law that involves the core of the whole problem, and our ultimate goal is to establish a formal process to simulate the natural phenomenon of the refraction of the watermark.

The nature of the watermark is really all kinds, let people messy to start. Fortunately, physics tells us that complex waves obey the Whigens principle in the propagation process, which allows us to define the problem first to the shock generated by the simplest single harmonic source. Of course, the more physical characteristics involved in our discussion, the more complex the problem will be. Here I will explain in advance that the reflection and diffraction characteristics of mechanical waves are completely ignored in the discussion below (which is of little significance in the issues we are discussing).

The refraction of light is caused by the change of the transmitted medium. In the water wave refraction, the light is transmitted from the bottom and passed vertically upward until the display area (which can be understood as a fluorescent screen that only accepts parallel light), it should be emphasized that we are simulating parallel light imaging, and the non-human eye is directly observed as a perspective projection. Although it is inconsistent with intuition, it can be approximated as equivalent. When light is refracted above the water from the bottom, the relationship between the refractive angle (in the light-trapping medium) and the incident angle (the optical dense medium) satisfies the sine ratio equal to the constant (greater than), and the two important angles are closely related to the normal at the three-dimensional waveform tangent plane. Refraction causes the position of the point we observe is not its actual position at the bottom, but the relative offset from the one that conforms to a certain law. Our core problem is finding a way to calculate relative offsets. Before discussing the three-dimensional waveform, we discuss the relatively simple two-dimensional waveform, which is the analysis of the refraction and relative offset of the sin simple harmonics:

The apparent geometric relationship makes it easy to deduce the relationship between Δx and x:

A represents the amplitude of the wave at x, the Omega parameter is used to adjust the wavelength, T determines the phase of a moment, deep actually represents the depth of the water, and ultimately reflects the intensity of the refraction. The functional relationship between the derivative and the refractive offset is easily obtained by the relation of relations. It is worth noting that the inverse tangent function (overhead) is required to calculate the alpha angle, and an approximate algorithm is used here:

It is not difficult to see that when the epsilon is very small x with a consistent sign and monotonicity. If you specify a distance of two pixels to be 1, then there are:

Here, the parameter D1 directly affects the intensity of the refraction.

The above is the discussion of the refraction and relative offset of the two-dimensional waveform, we have come to the conclusion that the relative offset in the x direction and the derivative in this direction has a functional relationship. In fact, in the three-dimensional waveform, the relative offset is divided into the X-direction and the Y-direction, and the relative offset of the component as the relationship between the two-dimensional waveform, and the partial derivative of the waveform function is a functional relationship, here no longer prove. The following is the promotion process:

Similarly, the approximate calculation method is used:

Here, we can completely simulate the three-dimensional waveform and calculate the relative offset. I do not know about Windows programming, for the convenience of using EASYX (or OpenGL) Drawing Library Registration window and drawing the image, the algorithm is written entirely in C + +. is to simulate a single waves generation of three-dimensional simple harmonics, with a grayscale value of 255 blue and red, respectively, 1 and 1:

When rendering a watermark, it is only necessary to store the data of the waveform in memory for calculating the relative offset of each pixel point. However, the actual waveform in the transmission process will undoubtedly have energy attenuation, otherwise even if the amplitude of the wave at infinity is still consistent with waves, it is obviously unrealistic. Next, let's talk about the attenuation of waves.

The so-called attenuation can simply be considered as the amplitude of the point outside the waves (no attenuation) multiplied by a factor that decreases with the increase of the relative waves distance. It is troublesome to establish a mathematical model that conforms strictly to the laws of physics, and the attenuation itself is not the core of the watermark rendering, so it is possible to approximate the attenuation of the waveform in propagation by selecting the appropriate function. For the selection of attenuation functions, it is obvious that the following important constraints apply:

1. Continuous and monotonically decreasing

2. The definition field contains all positive real numbers

3. The function range is (0,1], and the limit of the function at infinity is 0

Through many trials and comparisons, the attenuation model I finally chose is:

In which, the constant a,c is used to limit the function transform range to [0,1]; The parameter omega is less than 0, which makes the function into a monotonically decreasing function, its absolute size determines the attenuation rate, the parameter Phi determines the position of the function at the upper inflection point, and its domain is the most obvious range of waveform attenuation. Due to the nature of the function, the resulting waveform decay process appears relatively natural within the desired interval, not too sharp or too slow.

Here, you might think that using the inverse tangent function undoubtedly increases the computational overhead, so why not use a simpler function? My answer is: first, in order to render the beautiful. Second, the decay process can generate a hash table of discrete values in advance, and query with the parameter rho when calling.

Of course, you can also introduce two or more waves, at the same time you can see the interference of the waveform:

With this discussion, you can simply create a renderer model:

The waves queue is used to store the parameters of different waves, the waveform generator calculates the waveform at any point in the display area by acquiring the waves parameter and the attenuation factor, and puts the background image in the static cache (the underwater scene). Each time a frame of waveform data is processed, the render module calculates the relative offset of each pixel and generates an image to be stored in the dynamic cache, and the resulting frame is pushed by the display module to the display device.

Here's how the actual rendering of the final implementation compares to the original:

A single waves in the middle

Waves visible attenuation outside the display area

Comprehensive testing of multiple waves

Recalling the waveform of the watermark rendering, the waveform data of the display area needs to be updated each time a frame is generated. The amplitude of the wave on each pixel point is determined by the function of its distance from the waves. Assuming that there is only one waves, the amplitude of all pixels on the same arc is the same for the center of the same waves.

Rho, as an independent variable of the waveform function and the geometrical meaning represented by R, makes it easy to understand that the oscillation generated by point waves is formed by a series of concentric circles with equal spacing in a homogeneous two-dimensional medium. In a circular equation, different R determines the different point sets that satisfy the equation. When r increases continuously from 0 to infinity, then the trajectory of the arc has and only once covered all the points on the plane. It is possible to consider the incompatible points set on different R and S surfaces as a one-to mapping. What's the use of it? Previously said, the waveform is transmitted in concentric circles, if I want the waveform to be transmitted in concentric ellipse? First look at the elliptic equation:

If you can look like a circle, find a parameter R that can be used as Rho. Since the shape and size of the ellipse are determined by the parameter, a, the following changes may be made:

As a result, the eccentricity of the ellipse does not change, so that the shape does not change, but as the R value differs, a unique ellipse of different sizes is obtained (similar). When the parameter r is taken as the whole positive real number, the solution of the equation can have and cover the entire plane only once.

Similarly, the hyperbolic can also take the same approach to the introduction of parameter R, of course, can also do the appropriate treatment so that the centrifugal rate changes with the R, but it is important to note that the hyperbolic does not cover the entire plane, that is, the existence of complex solutions, the need to pay attention to the decision.

But if it's just enough to transform the waveform into an ellipse or a hyperbola, it sucks! Imagine the lens effect of the water droplets on the photo, can it be achieved? The answer, of course, is yes! But what we need to change this time is the wave function.

The water droplets in the photo can be approximated as half ellipsoid, so the z-variable can not be considered as a wave function in the equation of the half ellipsoid?

Note that the square root must be greater than or equal to 0. The following is the realization of the water droplet lens:

Summary: A seemingly complex problem, as long as the reasonable to define and solve the total harvest.

Probe into the watermark rendering and refraction filter of raster image science

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.