Direct3d improvement: HLSL programming achieves Photoshop filter effect-stroke

Source: Internet
Author: User

For the relief effect, stroke (edge detection)CodeIt's not complicated. It's just a little more complicated in theory, and the effect looks more attractive.

If the gray value at the edge of the image passes through a skip, we can calculate the Skip and perform some processing on the value to obtain the stroke effect of the Black edge.

First, consider the difference between the two pixels of this pixel to get a difference. The larger the difference, the more the image is at the edge, and the edge should be in the left and right directions. We can also get the image edges in the upper and lower directions and on two diagonal lines. In this way, we need to construct a filter.

After this filter, the difference value of the image changes in this pixel is obtained, which is converted into a gray value and the absolute value (the difference value may be negative ), then we define that the greater the absolute value of the difference, the blacker the place (the edge is obviously Black). Otherwise, the more white the difference, the following results can be obtained:

The code for this effect is as follows (the Code of the dip_filter function is the same as above ):

The template used in the above demonstration is an edge detector, which is a gradient-based filter in signal processing, also known as edge operators. The gradient is oriented and always orthogonal to the edge (vertical). In the above Code, we use a 45-degree gradient template, it can detect an edge of 135 degrees.

These are simple edge detection operators. More strictly, We can sample the Sobel operator. There are two Sobel operators: one is to detect the horizontal edge, and the other is to detect the same vertical flat edge. The other is the Sobel operator, which also has two, one is to detect the horizontal edge, and the other is to detect the vertical edge. Compared with the common Sobel operator, the location weighting coefficient of the same-sex Sobel operator is more accurate, and the gradient amplitude is consistent when detecting the edge of different directions. You can try the effect of the Sobel operator by yourself. You only need to modify the value of pencil_filter.

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.