Image:Zernike polynomials.png

From Wikipedia, the free encyclopedia

Wikimedia Commons logo This is a file from the Wikimedia Commons. The description on its description page there is shown below.
Commons is a freely licensed media file repository. You can help.

[edit] Dettagli

Description

Image of the Zernike_polynomials values

Source

self-made

Date

2008-05-07

Author

Rocchini

Permission
(Reusing this image)

CC-BY 3.0


[edit] Source Code

Ther dirty C++ code:

#include <stdio.h>
#include <stdlib.h>

static double fact( int n ) {  // The factorial function!
        double f = 1;
        while(n>1)
                f *= n--;
        return f;
}

static inline unsigned char f2b( double f ) {  // float to byte
        if(f<0) f = 0; if(f>1) f = 1;
        int i = f*256; if(i>255) i = 255;
        return i;
}

void HSV2RGB(double h, double s, double v, unsigned char rgb[3] ) {  // the classic hue scale
        if (s == 0) {
                rgb[0] = rgb[1] = rgb[2] = f2b(v);
        } else {
                double v_h = h * 6;
                double v_i = floor(v_h);
                double v_1 = v * (1 - s);
                double v_2 = v * (1 - s * (v_h - v_i));
                double v_3 = v * (1 - s * (1 - (v_h - v_i)));
                double v_r,v_g,v_b;
                     if (v_i == 0) {v_r = v;   v_g = v_3; v_b = v_1;}
                else if (v_i == 1) {v_r = v_2; v_g = v;   v_b = v_1;}
                else if (v_i == 2) {v_r = v_1; v_g = v;   v_b = v_3;}
                else if (v_i == 3) {v_r = v_1; v_g = v_2; v_b = v  ;}
                else if (v_i == 4) {v_r = v_3; v_g = v_1; v_b = v  ;}
                else               {v_r = v;   v_g = v_1; v_b = v_2;};
                
                rgb[0] = f2b(v_r);
                rgb[1] = f2b(v_g);
                rgb[2] = f2b(v_b);
        }
}

double zernike_polynomials( int m, int n, double ro, double th ) {  // the polynomial hitself
        double Rmnro = 0;
        bool even = m>=0;
        if(m<0) m = -m;
        if( (n-m)%2 ) return 0;

        for(int k=0;k<=(n-m)/2;++k) {
                Rmnro += pow(ro,n-2*k)*
                             ( pow(-1,k) * fact(n-k) ) /
                             ( fact(        k) *
                               fact((n+m)/2-k) * 
                               fact((n-m)/2-k)
                             );
        }

        if(even) return Rmnro * cos(m*th);
        else     return Rmnro * sin(m*th);
}

void main() {
        const double NO_VALUE = 42; const int B  = 64;
        const int SX = 1024;
        const int SY = 1024;
        
        const int LX = 3; const int LY = 7;
        int pairs[21][2] =
        {
                { 0,0},
                {-1,1},{ 1,1},
                {-2,2},{ 0,2},{ 2,2},
                {-3,3},{-1,3},{ 1,3},{ 3,3},
                {-4,4},{-2,4},{ 0,4},{ 2,4},{ 4,4},
                {-5,5},{-3,5},{-1,5},{ 1,5},{ 3,5},{ 5,5},
        };

        unsigned char * img = new unsigned char[LX*SX*LY*SY*3];

        int ix,iy,q = 0;
        for(iy=0;iy<LY;++iy) for(ix=0;ix<LX;++ix) {
                int m = pairs[q][0];
                int n = pairs[q][1];
                for(int j=0;j<SY;++j) {
                        double y = double(SY/2-j)/(SY/2-B);
                        for(int i=0;i<SX;++i) {
                           double x = double(SX/2-i)/(SX/2-B);
                           double ro = sqrt(x*x+y*y);
                           double th = atan2(y,x);      
                           double z = NO_VALUE;
                           if(ro<=1)
                                        z = zernike_polynomials(m,n,ro,th);
                           unsigned char * rgb = img+3*( i+SX*(ix+LX*(j+SY*iy)) );
                                if(z == NO_VALUE )
                                        rgb[0] = rgb[1] = rgb[2] = 255;
                                else {
                                        if(z>=0) z =  pow( z,0.8);   // little enance
                                        else     z = -pow(-z,0.8);
                                        z = (z+1)/2;                    // normalize
                                        if(z<0) z = 0;
                                        if(z>1) z = 1;
                                        
                                        HSV2RGB( 2.0*z/3.0, 0.8, 0.9, rgb );
                                }
                        }
                }
                printf("%03d%%\n",q*100/(LX*LY)); ++q;
        }

        FILE * fp = fopen("c:\\temp\\zernike.ppm","wb");
        fprintf(fp,"P6\n%d %d\n255\n",SX*LX,SY*LY);
        fwrite(img,1,LX*SX*LY*SY*3,fp);
        fclose(fp);
        delete[] img;
}

[edit] Licensing

I, the copyright holder of this work, hereby publish it under the following licenses:
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation license, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation license".

Aragonés | العربية | Asturianu | Български | বাংলা | ইমার ঠার/বিষ্ণুপ্রিয়া মণিপুরী | Brezhoneg | Bosanski | Català | Cebuano | Česky | Dansk | Deutsch | Ελληνικά | English | Esperanto | Español | Eesti | Euskara | فارسی | Suomi | Français | Gaeilge | Galego | עברית | Hrvatski | Magyar | Bahasa Indonesia | Ido | Íslenska | Italiano | 日本語 | ქართული | ភាសាខ្មែរ | 한국어 | Kurdî / كوردی | Latina | Lëtzebuergesch | Lietuvių | Bahasa Melayu | Nnapulitano | Nederlands | ‪Norsk (nynorsk)‬ | ‪Norsk (bokmål)‬ | Occitan | Polski | Português | Română | Русский | Slovenčina | Slovenščina | Shqip | Српски / Srpski | Svenska | తెలుగు | ไทย | Türkçe | Українська | اردو | Tiếng Việt | Volapük | Yorùbá | ‪中文(中国大陆)‬ | ‪中文(台灣)‬ | +/-

Creative Commons License
Creative Commons Attribution icon
This file is licensed under the Creative Commons Attribution 3.0 Unported License. In short: you are free to distribute and modify the file as long as you attribute its author(s) or licensor(s). Official license

Català | Česky | Deutsch | English | Ελληνικά | Español | Français | 한국어 | Italiano | עברית | Lietuvių | Magyar | Nederlands | Polski | Português | Русский | Türkçe | ‪中文(繁體)‬ | +/-

You may select the license of your choice.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current07:31, 7 May 2008793×1,850 (476 KB)Rocchini ({{Information |Description=Image of the Zernike_polynomials values |Source=self-made |Date=2008-05-07 |Author= Rocchini |Permission=CC-BY 3.0 }} )
The following pages on the English Wikipedia link to this file (pages on other projects are not listed):