search
HomeWeb Front-endPS TutorialPhotoShop algorithm principle analysis series - pixelation - fragmentation

Continuing with the popularity of the previous article, let’s continue talking about some slightly simpler algorithms.

This article will talk about the fragmentation algorithm. Let’s post a few renderings first:

PhotoShop算法原理解析系列 - 像素化-碎片 PhotoShop算法原理解析系列 - 像素化-碎片 PhotoShop算法原理解析系列 - 像素化-碎片 PhotoShop算法原理解析系列 - 像素化-碎片

This is a destructive filter. The reason for using beautiful women is that 90% of the people who create images are men, perverted men.

Regarding the principle of the fragment filter, the information available on the Internet is: Create four copies of the image that are offset from each other, producing an effect similar to ghosting.

With the above sentence, we can start.

Analysis: Through the comparison of the above images, especially the eyes, it can be seen that the processed image should look like a single eye has become 4 eyes. Therefore, the network The statement above is reliable.

So where is the center of the offset and what is the number of offsets? In which directions are the 4 offsets offsets? These questions are also very simple. You can Then PS for verification:

The specific steps are as follows: Open an image and fill a 2*2 pixel red area in the place where the color of the image is relatively monotonous (such as the arm of the above-mentioned beauty). , then copy the layer, apply a fragment filter to the copied layer, and adjust the layer transparency to 50%. You can get the following image by partially zooming in:

PhotoShop算法原理解析系列 - 像素化-碎片## With such an effect, you can easily draw the conclusion:

The center of the offset is centered on each pixel, and the four offsets are symmetrical about the center, with a slope of 45 Degrees are evenly arranged in a circle, with horizontal and vertical offsets of 45 degrees each, and an offset of 4 pixels.

Then the question of how to superimpose can be guessed, it is to take the average of the accumulated values ​​after four offsets.

Based on this idea, I wrote the following algorithm:

private void CmdFragment_Click(object sender, EventArgs e)
{    int X, Y, Z, XX, YY;    int Width, Height, Stride;    int Speed, Index;    int SumR, SumG, SumB;
    Bitmap Bmp = (Bitmap)Pic.Image;    if (Bmp.PixelFormat != PixelFormat.Format24bppRgb) throw new Exception("不支持的图像格式.");

    Width = Bmp.Width; Height = Bmp.Height; Stride = (int)((Bmp.Width * 3 + 3) & 0XFFFFFFFC);    byte[] ImageData = new byte[Stride * Height];                                    // 用于保存图像数据,(处理前后的都为他)
    byte[] ImageDataC = new byte[Stride * Height];                                   // 用于保存克隆的图像数据
    int[] OffsetX = new int[] { 4, -4, -4, 4 };                                      // 每个点的偏移量
    int[] OffsetY = new int[] { -4, -4, 4, 4 };    fixed (byte* P = &ImageData[0], CP = &ImageDataC[0])
    {        byte* DataP = P, DataCP = CP;
        BitmapData BmpData = new BitmapData();
        BmpData.Scan0 = (IntPtr)DataP;                                              //  设置为字节数组的的第一个元素在内存中的地址
        BmpData.Stride = Stride;
        Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite | ImageLockMode.UserInputBuffer, PixelFormat.Format24bppRgb, BmpData);

        Stopwatch Sw = new Stopwatch();                                             //  只获取计算用时        Sw.Start();
        System.Buffer.BlockCopy(ImageData, 0, ImageDataC, 0, Stride * Height);     //  填充克隆数据        

        for (Y = 0; Y = Width)
                        XX = Width - 1;                    if (YY = Height)
                        YY = Height - 1;
                    Index = YY * Stride + XX * 3;
                    SumB += DataCP[Index];
                    SumG += DataCP[Index + 1];
                    SumR += DataCP[Index + 2];
                }

                DataP[Speed] = (byte)((SumB+2) >> 2);    //  求平均值(Sum+2)/4,为什么要+2,就为了四舍五入。比如如果计算结果为108.6,则取像素109更为合理     
                DataP[Speed + 1] = (byte)((SumG + 2) >> 2);
                DataP[Speed + 2] = (byte)((SumR + 2) >> 2);
                Speed += 3;                                                     //  跳往下一个像素            }
        }
        Sw.Stop();        this.Text = "计算用时: " + Sw.ElapsedMilliseconds.ToString() + " ms";
        Bmp.UnlockBits(BmpData);                         //  必须先解锁,否则Invalidate失败     }
    Pic.Invalidate();}

 

In the algorithm, OffsetX and OffsetY are the offsets of the sampling point pixels respectively. Similarly, since this filter involves field operations, pixel backup needs to be done before processing, but the backup data is not expanded here. Therefore, the coordinates of the sampling point need to be verified in the internal code to see if it exceeds its range. If it exceeds the range, it is usually within the range of the image filter algorithm. There are 3 processing methods:

(1) If it exceeds, it is considered to be its closest boundary value, that is, repeated edge pixels. This part of the code is the if...else if part posted above.

(2) Return can be described by the following code:

while (XX >= Width)
    XX = XX - Width;while (XX = Height)
    YY = YY - Height;while (YY <p class="cnblogs_code"></p><p> (3 ) Calculate only the pixels within the image range: </p><p><span style="font-size: 13px;"></span></p><pre class="brush:php;toolbar:false"> if (XX >= 0 && XX = 0 && YY <p class="cnblogs_code"></p> <p> Of course, to do this, you must use a variable to record how much has been done qualifying calculations. </p><p><span style="font-size: 13px;"></span> Friends who are interested can change the code and give it a try. </p><p></p><p><span style="font-size: 13px;"> In the above code snippet, DataP[Speed] = (byte)((SumB+2) >> 2); The reason for adding 2 to SumB is to round the result, so that it is more accurate Reasonable. </span></p><p><span style="font-size: 13px;"> After testing, the above code is 100% consistent with the effect of PS processing. It shows that our guess is completely correct. </span></p><p><span style="font-size: 13px;"> You can also further expand the algorithm: </span><span style="font-size: 13px;">Think further, why does it have to be 4 ghost images? It has to be an angle of 45 degrees. It has to be 4 The horizontal and vertical offset of pixels. I give the picture below for interested readers to develop on their own. </span></p><p><span style="font-size: 13px;"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/013/1ec01116c8ef6115593d57c273276c69-5.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="PhotoShop算法原理解析系列 - 像素化-碎片"    style="max-width:90%"  style="max-width:90%" title="PhotoShop算法原理解析系列 - 像素化-碎片"></span></p><p><span style="font-size: 13px;"> In the picture, the angle is 32 degrees, the radius is 10, and the number of fragments is 7, which can produce an effect similar to the following (You can use my Imageshop to verify): </span></p><p><span style="font-size: 13px;"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/013/1ec01116c8ef6115593d57c273276c69-6.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="PhotoShop算法原理解析系列 - 像素化-碎片"    style="max-width:90%"  style="max-width:90%" title="PhotoShop算法原理解析系列 - 像素化-碎片"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/013/1ec01116c8ef6115593d57c273276c69-7.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="PhotoShop算法原理解析系列 - 像素化-碎片"    style="max-width:90%"  style="max-width:90%" title="PhotoShop算法原理解析系列 - 像素化-碎片"></span></p>## More PhotoShop algorithm principle analysis series - pixelation-fragmentation related articles Please pay attention to PHP Chinese website! <p></p>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Photoshop's Key Features: A Deep DivePhotoshop's Key Features: A Deep DiveApr 19, 2025 am 12:08 AM

Key features of Photoshop include layers and masks, adjustment tools, filters and effects. 1. Layers and masks allow independent editing of image parts. 2. Adjust tools such as brightness/contrast can modify image tone and brightness. 3. Filters and effects can quickly add visual effects. Mastering these features can help creative professionals achieve their creative vision.

Photoshop and Digital Art: Painting, Illustration, and CompositingPhotoshop and Digital Art: Painting, Illustration, and CompositingApr 18, 2025 am 12:01 AM

Photoshop's applications in digital art include painting, illustration and image synthesis. 1) Painting: Using brushes, pencils and mixing tools, the artist can create realistic effects. 2) Illustration: With vector and shape tools, artists can accurately draw complex graphics and add effects. 3) Synthesis: Using mask and layer blending mode, artists can seamlessly blend different image elements.

Advanced Photoshop Tutorial: Master Retouching & CompositingAdvanced Photoshop Tutorial: Master Retouching & CompositingApr 17, 2025 am 12:10 AM

Photoshop's advanced photo editing and synthesis technologies include: 1. Use layers, masks and adjustment layers for basic operations; 2. Use image pixel values ​​to achieve photo editing effects; 3. Use multiple layers and masks for complex synthesis; 4. Use "liquefaction" tools to adjust facial features; 5. Use "frequency separation" technology to perform delicate photo editing, these technologies can improve image processing level and achieve professional-level effects.

Using Photoshop for Graphic Design: Branding and MoreUsing Photoshop for Graphic Design: Branding and MoreApr 16, 2025 am 12:02 AM

The steps to using Photoshop for brand design include: 1. Use the Pen tool to draw basic shapes, 2. Add shadows and highlights through layer styles, 3. Adjust colors and details, 4. Use smart objects and actions to automatically generate different versions of the design. Photoshop helps designers create and optimize brand elements with the flexibility of layers and masks, ensuring consistency and professionalism of designs, from simple logos to complex branding guides.

Photoshop's Subscription Model: What You Get for Your MoneyPhotoshop's Subscription Model: What You Get for Your MoneyApr 15, 2025 am 12:17 AM

Photoshop's subscription model is worth buying. 1) Users can access the latest version and use across devices at any time. 2) The subscription fee is low, and continuous updates and technical support are provided. 3) Advanced functions such as neural filters can be used for complex image processing. Despite the high long-term costs, its convenience and feature updates are valuable to professional users.

Photoshop: Investigating Free Trials and Discount OptionsPhotoshop: Investigating Free Trials and Discount OptionsApr 14, 2025 am 12:06 AM

You can get the access to Photoshop in the most economical way: 1. Experience the software features with a 7-day free trial; 2. Find student or teacher discounts, as well as seasonal promotions; 3. Use coupons on third-party websites; 4. Subscribe to Adobe CreativeCloud's monthly or annual plan.

Photoshop for Designers: Creating Visual ConceptsPhotoshop for Designers: Creating Visual ConceptsApr 13, 2025 am 12:09 AM

Creating visual concepts in Photoshop can be achieved through the following steps: 1. Create a new document, 2. Add a background layer, 3. Use the brush tool to draw basic shapes, 4. Adjust colors and brightness, 5. Add text and graphics, 6. Use masks for local editing, 7. Apply filter effects, these steps help designers build a complete visual work from scratch.

Is Photoshop Free? Understanding Subscription PlansIs Photoshop Free? Understanding Subscription PlansApr 12, 2025 am 12:11 AM

Photoshop is not free, but there are several ways to use it at low cost or free: 1. The free trial period is 7 days, and you can experience all functions during this period; 2. Student and teacher discounts can cut costs by half, and school proof is required; 3. The CreativeCloud package is suitable for professional users and includes a variety of Adobe tools; 4. PhotoshopElements and Lightroom are low-cost alternatives, with fewer functions but lower prices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool