C # graphic processing series (iv) -- Mosaic Processing

Source: Internet
Author: User

Speaking of mosaic, some moles know that there are horses and no horses. This is short for Mosaic. You can think about the evil side and the mosaic has a harmonious side, for example, for some news, the key is not to be presented to the audience for harmony. This is the mosaic application.

First look at a mosaic with extremely low resolution:

 

You may be able to guess the algorithm when you see the face color block of the cube.

The mosaic algorithm is very simple. To put it bluntly, we divide an image into several cell blocks with Val * val pixels (there may be sporadic small blocks at the edge, but it does not affect the overall algorithm. The larger the Val, the more obvious the mosaic effect), the color of each cell is the same. For convenience, we may wish to make the color use the color of the point in the top left corner of the area. Of course, there can also be other methods, such as taking the color of the center point of the block, or representing the color of random points in the block.

The following is the result of val = 2.

Source image pixel
Abcdefg
Hijklmn
Opqrstu
Vwxyz01
2345678

After mosaic Processing
Aacceeg
Aacceeg
Ooqqssu
Ooqqssu
2244668

The principle is that simple. The specific implementation depends on the habits of each person. My idea is:

When Y (current height) is an integer multiple of Val:
Scan each point of X in the current row. If X is an integer multiple of Val, record the color values of the current X and Y. If X is not an integer multiple of Val, the last recorded color value is used.
If y is not an integer multiple of Val:
It's easy. Copy the previous row directly.

Therefore, the larger the block, the more obvious the processing effect. We can also conclude that the source image (r) maps the processed image (s) to multiple to one, that is: the image after Mosaic processing is irreversible. Do not try to use reversible algorithms to restore the image.

/// <Summary>
/// Mosaic
/// Divide an image into several N * n pixel cell blocks (there may be sporadic small blocks on the edge, but this does not affect the overall algorithm)
///, The color of each cell block is the same.
/// </Summary>
Public class mosaicimage: iimageprocessable
{
# Region iimageprocessable Member

Public void processbitmap (bitmap BMP)
{
Int width = BMP. width;
Int Height = BMP. height;
Const int n = 5; // effect granularity. The larger the value, the more severe the value is.
Int r = 0, G = 0, B = 0;
Color C;
For (INT y = 0; y {
For (INT x = 0; x <width; X ++)
{

If (Y % N = 0)
{
If (X % N = 0) // if it is an integer multiple, assign a value to the pixel.
{
C = BMP. getpixel (x, y );
R = C. R;
G = c. g;
B = C. B;
}
Else
{
BMP. setpixel (X, Y, color. fromargb (R, G, B ));
}
}
Else // copy the previous row
{
Color colorpreline = BMP. getpixel (X, Y-1 );
BMP. setpixel (X, Y, colorpreline );

}
}
}
}

Public unsafe void unsafeprocessbitmap (bitmap BMP)
{
Int width = BMP. width;
Int Height = BMP. height;
Const int n = 5; // effect granularity. The larger the value, the more severe the value is.
Int r = 0, G = 0, B = 0;
Rectangle rect = new rectangle (0, 0, width, height );
Bitmapdata bmp data = BMP. lockbits (rect, imagelockmode. readwrite, pixelformat. format32bppargb );
Byte * PTR = (byte *) (BMP data. scan0 );
For (INT y = 0; y {
For (INT x = 0; x <width; X ++)
{
If (Y % N = 0)
{
If (X % N = 0)
{
R = PTR [2];
G = PTR [1];
B = PTR [0];
}
Else
{
PTR [2] = (byte) R;
PTR [1] = (byte) g;
PTR [0] = (byte) B;
}
}
Else // copy the previous row
{
PTR [0] = PTR [0-BMP data. stride]; // B;
PTR [1] = PTR [1-BMP data. stride]; // G;
PTR [2] = PTR [2-BMP data. stride]; // R
}
PTR + = 4;
}
PTR + = BMP data. stride-width * 4;
}
BMP. unlockbits (BMP data );

}

# Endregion
}

The following are the processing results of the block size of 5 and 9 respectively:

 

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.