C # example of generating an image pyramid

Source: Internet
Author: User

First, let's talk about the principle of the pyramid:

The image pyramid is to process an original image into a tower-like image structure (please do not tangle with this concept)

The resolution of the finest layer in the image pyramid is 16*16, and the next layer is 8*8, which is 4*4, 2*2, 1*1 in sequence.

As shown in:

In the figure, the mesh vertices represent the original image 16*16, the red difference represents 8*8, and the black circle represents 4*4.

According to the above principle, the programming implementation is just the interval sampling, so the gray values of the adjacent two images on the corresponding points should be equal, of course, I think so.

C # The bitmap class in the drawing namespace has a function for processing the image pyramid,

CopyCode The Code is as follows: public void mipmaping ()

{

Bitmap jpgsource = new Bitmap (@ "E: \ height512.jpg ");

Bitmap jpgtarget = new Bitmap (jpgsource, jpgsource. width> 1, jpgsource. Height> 1 );

Jpgtarget. Save (@ "E: \ height256.jpg ");

}

However, after pixel-by-pixel comparison, I found that the pixels of the corresponding points on the two images are not equal, which indicates a problem: the method in C # is certainly not based on point-by-point sampling, instead, the image is stretched in some way. This does not seem to conform to the pyramid principle, so I re-wrote a pyramid function:

Copy code The Code is as follows: public void rescale ()

{

Bitmap myimage1024 = new Bitmap (@ "E: \ height.jpg ");

Int width = myimage1024.height ;;

Int Height = myimage1024.width ;;

Console. writeline (datetime. Now. tostring ());

For (INT power = 1; power <3; power ++)

{

Bitmap myimage = new Bitmap (width> power, height> power );

Int ROW = myimage. height;

Int column = myimage. width;

For (INT I = 0; I <row; I ++)

For (Int J = 0; j <column; j ++)

{// Here is the essence. In fact, it is the step by sampling interval.

Int i1024 = getcoor (I, power); int j1024 = getcoor (J, power );

Color color1024 = myimage1024.getpixel (i1024, j1024 );

Myimage. setpixel (I, j, color1024 );

}

Myimage. Save (string. Format (@ "E: \ myjpg \ height1_02.16.jpg", width> power ));

}

Console. writeline (datetime. Now. tostring ());

}

Public int getcoor (int I, int power)

{

If (power> 1)

Return 2 * getcoor (I, power-1) + 1;

Else

Return 2 * I + 1;

}

Follow the function I wrote to create a pyramid. At this time, the corresponding point pixel values between adjacent layers are equal.

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.