Advertisement
Kojima0502

display_標準偏差

Feb 23rd, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GLUT/glut.h>
  4. #include <time.h>
  5. #include <math.h>
  6. /* keep the picture data*/
  7. #define  IMGFILE  "depth_combine48_cunbus_conv.bmp"
  8. #define  CHANNEL  3  // color(R, G, B)
  9. FILE *fp1,*fp2,*fp3,*fp4,*fp5;
  10. int i,j;
  11. float min;//miminum
  12. float max;//max
  13. int width;           // width of picture
  14. int height;          // height of picture
  15. float *file1,*file2,*file3,*file4,*file5;
  16.  
  17. GLuint texture;      // texture number
  18. GLubyte* image;      // keep the BMP data
  19.  
  20. void loadImage(const char* filepath);  // read BMP
  21.  
  22. float shiftX,shiftY;//for motion
  23. float mouseX,mouseY;//mouse
  24. float x=0.1;
  25. float y=0.1;
  26. float g_pressed = 0;//for mouse
  27. float r;
  28. float sum,sum2,sum3;
  29. float sum4;
  30. int i_first,j_first;
  31. int WIDTH,HEIGHT;
  32. int WIDTH1,HEIGHT1;
  33. float dn1,dn2,dn3,dn4,dn5;
  34. int b;
  35. float total1,total2,total3,total4,total5;//total
  36. float  average1,average2,average3,average4,average5;//average
  37. float start, end;//for calculating processing time
  38.  
  39. void display(void)
  40. {
  41.     /*diaplay the picture */
  42.     glEnable(GL_TEXTURE_2D);// for making texture
  43.     glClear(GL_COLOR_BUFFER_BIT);
  44.     glBindTexture(GL_TEXTURE_2D, texture);
  45.    
  46.     glTranslated(-0.5,-0.5,0);
  47.     glBegin(GL_QUADS);
  48.    
  49.     glTexCoord2f(1.0, 0.0);  glVertex2f(1.0, 0.0);//position of texuture
  50.     glTexCoord2f(1.0, 1.0);  glVertex2f(1.0, 1.0);
  51.     glTexCoord2f(0.0, 1.0);  glVertex2f(0.0, 1.0);
  52.     glTexCoord2f(0.0, 0.0);  glVertex2f(0.0, 0.0);
  53.    
  54.     glDisable(GL_TEXTURE_2D);
  55.    
  56.     glEnd();
  57.    
  58.    
  59.    
  60.     glTranslated(0.5,0.5,0);
  61.     glBegin(GL_LINE_LOOP);//tie each poosition by the line
  62.    
  63.     glVertex2d(-x+shiftX, -y+shiftY);//at the lower left
  64.     glVertex2d(x+shiftX, -y+shiftY);//at the lower right
  65.     glVertex2d(x+shiftX, y+shiftY);//at the upper rught
  66.     glVertex2d(-x+shiftX, y+shiftY);//at the upper left
  67.     //printf("ShiftX1 = %f : ShiftY1 = %f\n" , -x+shiftX , -y+shiftY);//at the lower left of square
  68.     //printf("ShiftX2 = %f : ShiftY2 = %f\n" , x+shiftX , -y+shiftY);//at the lower right of square
  69.     //printf("ShiftX3 = %f : ShiftY3 = %f\n" , x+shiftX , y+shiftY);//at the upper right of square
  70.     //printf("ShiftX4 = %f : ShiftY4 = %f\n" , -x+shiftX , y+shiftY);//at the upper left of square
  71.     //printf("b=%d\n",b);
  72.  
  73.     printf("HEIGHT = %d : WIDTH = %d : HEIGHT1 = %d : WIDTH1 = %d : y+shiftY = %f : -x+shiftX = %f : b=%d  \n" , HEIGHT,WIDTH,HEIGHT1,WIDTH1,y+shiftY,-x+shiftX,b);//display HEIGHT,WIDTH,HEIGHT1,WIDTH1,y+shiftY,-x+shiftX,b
  74.    
  75.     total1=0;
  76.     /*total2=0;
  77.     total3=0;
  78.     total4=0;
  79.     total5=0;
  80.     */
  81.    
  82.     i_first=-(int)((float)HEIGHT*(y+shiftY))+b;
  83.     j_first=(int)((float)WIDTH*(-x+shiftX))+b;
  84.     min=file1[(i_first-(WIDTH-HEIGHT)/2)*WIDTH+j_first];
  85.     max=file1[(i_first-(WIDTH-HEIGHT)/2)*WIDTH+j_first];
  86.  
  87.     //start=clock();
  88.    
  89.     for(i= -(int)((float)HEIGHT*(y+shiftY))+b ;i<(int)(-(float)HEIGHT*(y+shiftY))+b+HEIGHT1;i++)
  90.     {
  91.         //printf("i=%d\n",i);
  92.         //printf("i2=%d\n",-HEIGHT*(int)(y+shiftY)+b);
  93.        
  94.         for(j=(int)((float)WIDTH*(-x+shiftX))+b;j<(int)((float)WIDTH*(-x+shiftX))+b+WIDTH1;j++)
  95.         {
  96.             //   printf("j=%d\n",j);
  97.             //  printf("check-7\n");
  98.             // printf("position=%d\n",(i-(WIDTH-HEIGHT)/2)*WIDTH+j);
  99.             //printf("position=%d¥n",(i-WIDTH1)*HEIGHT1+(j-HEIGHT1+((WIDTH-HEIGHT)/2)));
  100.            
  101.            
  102.             dn1=file1[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  103.             total1=total1+dn1;
  104.            
  105.             if(min>dn1)
  106.             {
  107.                 min=dn1;//calculate miminum
  108.             }
  109.            
  110.             if(max<dn1)
  111.             {
  112.                 max=dn1;//calculate maximun
  113.             }
  114.            // sum +=dn1-average1;
  115.            // sum2 +=sum*sum;
  116.            // sum3 +=sum2/(WIDTH*HEIGHT);
  117.                 /*
  118.             dn2=file2[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  119.             total2=total2+dn2;
  120.             dn3=file3[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  121.             total3=total3+dn3;
  122.             dn4=file4[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  123.             total4=total4+dn4;
  124.             dn5=file5[(i-(WIDTH-HEIGHT)/2)*WIDTH+j];
  125.             total5=total5+dn5;
  126.            
  127.              */
  128.         }
  129.     }
  130.     //printf("total1=%f\n",total1);
  131.     //printf("total2=%f\n",total2);
  132.    //printf("total3=%f\n",total3);
  133.     // printf("total4=%f\n",total4);
  134.     // printf("total5=%f\n",total5);
  135.    
  136.    
  137.     average1=total1/(float)(WIDTH1*HEIGHT1);
  138.     //average2=total2/(float)(WIDTH1*HEIGHT1);
  139.     //average3=total3/(float)(WIDTH1*HEIGHT1);
  140.     //average4=total4/(float)(WIDTH1*HEIGHT1);
  141.     //average5=total5/(float)(WIDTH1*HEIGHT1);
  142.  
  143.     sum2=0;
  144.     for(i= -(int)((float)HEIGHT*(y+shiftY))+b ;i<(int)(-(float)HEIGHT*(y+shiftY))+b+HEIGHT1;i++)
  145.     {
  146.         for(j=(int)((float)WIDTH*(-x+shiftX))+b;j<(int)((float)WIDTH*(-x+shiftX))+b+WIDTH1;j++)
  147.         {
  148.                     sum =dn1-average1;
  149.            
  150.                     sum2 +=sum*sum;
  151.            
  152.            
  153.            // printf( "b=%f\n", sum4 );
  154.         }
  155.     }
  156.              sum3 =sum2/(WIDTH*HEIGHT);
  157.             double sum4 = sqrt( sum3 );
  158.    
  159.    
  160.    
  161.     printf("The average1 water depth is %f m\n " ,average1 );//DOCUMENTATION
  162.     printf("The minimum water depth is %f m\n " ,min );
  163.     printf("The maximum water depth is %f m\n " ,max );
  164.     printf("sum4 is %f m\n " ,sum4 );
  165.     //printf("Standard deviation of the maximum water depth is %f m\n " ,sum3);
  166.  
  167.     if(average1>0 && average1<20.0)//document
  168.     {
  169.         printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  170.     }
  171.     else if (average1>20 && average1<400.0)
  172.     {
  173.         printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  174.     }
  175.  
  176.     else if(average1>=400.0 && average1<1000.0)
  177.     {
  178.         printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  179.     }
  180.     else if(average1>1000.0)
  181.     {
  182.         printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  183.     }
  184.     else if(average1<0)
  185.     {
  186.         printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
  187.     }
  188.     /*
  189.     printf("The average2 water depth is %f m\n " ,average2 );//average2
  190.    
  191.     if(average2>0 && average2<400.0)
  192.     {
  193.         printf("The average2 water depth is so deep\n");
  194.     }
  195.     else if(average2>=400.0 && average2<1000.0)
  196.     {
  197.         printf("The average2 water depth is too deep\n");
  198.     }
  199.     else if(average2>1000.0)
  200.     {
  201.         printf("The average2 water depth is fucking deep \n");
  202.     }
  203.     else if(average2<0)
  204.     {
  205.         printf("The average2 water depth is under 0m this means there are alomost land\n");
  206.     }
  207.    
  208.     printf("The average3 water depth is %f m\n " ,average3 );//average3
  209.    
  210.     if(average3>0 && average3<400.0)
  211.     {
  212.         printf("The average3 water depth is so deep\n");
  213.     }
  214.     else if(average3>=400.0 && average3<1000.0)
  215.     {
  216.         printf("The average3 water depth is too deep\n");
  217.     }
  218.     else if(average3>1000.0)
  219.     {
  220.         printf("The average3 water depth is fucking deep \n");
  221.     }
  222.      else if(average3<0)
  223.      {
  224.      printf("The average3 water depth is under 0m this means there are alomost land\n");
  225.      }
  226.    
  227.      printf("The average4 water depth is %f m\n " ,average4 );//averave4
  228.    
  229.     if(average4>0 && average4<400.0)
  230.     {
  231.         printf("The average4 water depth is so deep\n");
  232.     }
  233.     else if(average4>=400.0 && average4<1000.0)
  234.     {
  235.         printf("The average4 water depth is too deep\n");
  236.     }
  237.     else if(average4>1000.0)
  238.     {
  239.         printf("The average4 water depth is fucking deep \n");
  240.     }
  241.     else if(average4<0)
  242.     {
  243.      printf("The average4 water depth is under 0m this means there are alomost land\n");
  244.     }
  245.    
  246.      printf("The average5 water depth is %f m\n " ,average5 );//averave5
  247.    
  248.     if(average5>0 && average5<400.0)
  249.     {
  250.         printf("The average5 water depth is so deep\n");
  251.     }
  252.     else if(average5>=400.0 && average5<1000.0)
  253.     {
  254.         printf("The average5 water depth is too deep\n");
  255.     }
  256.     else if(average5>1000.0)
  257.     {
  258.         printf("The average5 water depth is fucking deep \n");
  259.     }
  260.      else if(average5<0)
  261.      {
  262.      printf("The average5 water depth is under 0m this means there are alomost land\n");
  263.      }
  264.      
  265.    
  266. */
  267.     end=clock();
  268.    
  269.     printf("Start  %f /sec \n " ,start/1000000);
  270.     printf("End  %f /sec \n " ,end/1000000);
  271.     printf("Star to end  %f /sec \n " ,(end-start)/1000000);
  272.     glEnd();
  273.    
  274.    
  275.     glutSwapBuffers();
  276. }
  277.  
  278. void myInit(void)
  279. {
  280.    
  281.     /* necessary setting for before create window*/
  282.     //window1//
  283.     glutInitWindowSize(600, 600);                  // setting for window size
  284.     glutInitWindowPosition(0, 0);              // window position
  285.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);  // setting display mode
  286.     glutCreateWindow("OpenGL");// create window
  287.    
  288.     glClearColor (0.0, 0.0, 0.0, 1.0);             // window back color
  289.    
  290.     glutDisplayFunc(display);
  291.     //create texture
  292.     loadImage(IMGFILE);
  293.     glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  294.     glGenTextures(1, &texture);
  295.     glBindTexture(GL_TEXTURE_2D, texture);
  296.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  297.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  298.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  299.                  width, height, 0, GL_RGB,
  300.                  GL_UNSIGNED_BYTE, image);
  301.    
  302.     free(image);
  303. }
  304.  
  305. void keyboard(unsigned char key, int x, int y)  //keyboard setting
  306. {
  307.     switch (key)
  308.     {
  309.         case 'q':
  310.         case 'Q':
  311.         case '\033':
  312.             exit(0);
  313.         default:
  314.             break;
  315.     }
  316. }
  317.  
  318.  
  319. void mouse(int btn, int state, int x, int y)  //mouze setting
  320. {
  321.    
  322.    
  323.     if(btn == GLUT_LEFT_BUTTON == state == GLUT_DOWN)  //when press the left button
  324.     {
  325.         mouseX = x;
  326.         mouseY = y;
  327.         g_pressed = 1;
  328.        
  329.     }
  330.     else if(btn == GLUT_LEFT_BUTTON == state ==GLUT_UP)  //when take off the left button
  331.         g_pressed = 0;
  332.    
  333. }
  334.  
  335.  
  336. void motion(int x, int y)  //controll the motion by the mouse
  337. {
  338.     {
  339.        
  340.         shiftX = (float)((x - mouseX)/320);
  341.         shiftY = (float)(-(y - mouseY)/320);
  342.         start =clock();
  343.         glutPostRedisplay();
  344.        
  345.     }
  346.    
  347.     /* printf("X = %d : Y = %d\n" , x , y);  //diplay the position of coordinate
  348.      printf("ShiftX = %f : ShiftY = %f\n" , shiftX , shiftY);  //display the amount of the shift
  349.      printf("mouseX = %f : mouseY = %f \n" , -x+shiftX , mouseY);
  350.      //printf("mouseX = %f : mouseY = %f \n" , mouseX , mouseY);  //display the mouseX and mouseY
  351.      printf("wx = %f : wy = %f \n" , get_wx , get_wy);
  352.      // printf("windowX = %f : windowY = %f \n" , get_wx , get_wy);
  353.      printf("windowW = %f : windowH = %f \n" , get_ww , get_wh);
  354.      */
  355. }
  356.  
  357. int main(int argc, char* argv[])
  358. {
  359.    
  360.    // printf("check-1\n");
  361.    
  362.    
  363.     if(argc!=4)
  364.     {
  365.         fprintf(stderr,"Usage: %s\n(1)input file name\n(2)input file name\n(3)input file name\n(4)WIDTH\n(5)HEIGHT\n",argv[0]);
  366.         exit(1);
  367.     }
  368.    
  369.     r=1/(2*x);
  370.     //printf("r = %f \n",r);
  371.     WIDTH = atoi(argv[2]); //printf("W = %d,", WIDTH);
  372.     HEIGHT = atoi(argv[3]); //printf("H = %d\n", HEIGHT);
  373.     WIDTH1=(int)((float)WIDTH/r);
  374.     HEIGHT1=(int)((float)WIDTH/r);
  375.     b=WIDTH/2;
  376.    
  377.     file1= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  378.     //file2= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  379.     //file3= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  380.     //file4= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  381.     //file5= (float *)malloc(WIDTH*HEIGHT*sizeof(float));
  382.    
  383.  
  384.     if((fp1=fopen(argv[1],"rb"))==NULL)
  385.     {
  386.         fprintf(stderr,"input file open error\n");
  387.         exit(1);
  388.     }
  389.     /*
  390.     if((fp2=fopen(argv[2],"rb"))==NULL)
  391.     {
  392.         fprintf(stderr,"input file open error\n");
  393.         exit(1);
  394.     }
  395.    
  396.     if((fp3=fopen(argv[3],"rb"))==NULL)
  397.     {
  398.         fprintf(stderr,"input file open error\n");
  399.         exit(1);
  400.     }
  401.    
  402.      if((fp4=fopen(argv[4],"rb"))==NULL)
  403.      {
  404.      fprintf(stderr,"input file open error\n");
  405.      exit(1);
  406.      }
  407.    
  408.      if((fp5=fopen(argv[5],"rb"))==NULL)
  409.      {
  410.      fprintf(stderr,"input file open error\n");
  411.      exit(1);
  412.      }
  413.    
  414.      
  415.     */
  416.     fread((float *)file1,sizeof(float),WIDTH*HEIGHT,fp1);
  417.     //fread((float *)file2,sizeof(float),WIDTH*HEIGHT,fp2);
  418.     //fread((float *)file3,sizeof(float),WIDTH*HEIGHT,fp3);
  419.     //fread((float *)file4,sizeof(float),WIDTH*HEIGHT,fp4);
  420.     //fread((float *)file5,sizeof(float),WIDTH*HEIGHT,fp5);
  421.  
  422.     //printf("check-10\n");
  423.    
  424.     glutInit(&argc, argv);
  425.    
  426.     myInit();
  427.    
  428.     glutKeyboardFunc(keyboard);  //when need keyboard function
  429.     glutMouseFunc(mouse);  //when need mouse function
  430.     glutMotionFunc(motion);  //when need mouse motion function
  431.     glutDisplayFunc(display);  //when need to set about display function
  432.     //glutIdleFunc(idle_func);
  433.     glutMainLoop();
  434.    
  435.     return 0;
  436.    
  437.     free(file1);
  438.     //free(file2);
  439.     //free(file3);
  440.     //free(file4);
  441.     //free(file5);
  442.  
  443.    
  444.     fclose(fp1);
  445.     //fclose(fp2);
  446.     //fclose(fp3);
  447.     //fclose(fp4);
  448.     //fclose(fp5);
  449. }
  450.  
  451.  
  452. void loadImage(const char* filepath)
  453. {
  454.     int i;
  455.     int size;  // size of picture (width , height, color)
  456.     FILE* fp;
  457.    
  458.     /* read file */
  459.     fp = fopen(filepath, "rb");  //read binary
  460.     if (fp == NULL)
  461.     {
  462.         fprintf(stderr, "%sfail loading\n", filepath);
  463.         exit(EXIT_FAILURE);
  464.     }
  465.    
  466.     /* get width and height  */
  467.     fseek(fp, 18, SEEK_SET);  //move fp to place where has been put width of picture
  468.     fread(&width, 4, 1, fp);
  469.     fread(&height, 4, 1, fp);
  470.    
  471.     /* read data of picture */
  472.     fseek(fp, 54, SEEK_SET);  //move fp to place where has been put dataof the picture
  473.     size = width * height * CHANNEL;
  474.     image = (GLubyte* ) malloc(size);
  475.     if (image == NULL)
  476.     {
  477.         fprintf(stderr, "fail to keep the memory\n");
  478.         exit(EXIT_FAILURE);
  479.     }
  480.    
  481.     for (i = 0; i < size; ++i)
  482.         fread(&image[i], sizeof(GLubyte), 1, fp);
  483.    
  484.     /* exit */
  485.     fclose(fp);
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement