Guest
Public paste!

Untitled

By: a guest | Mar 16th, 2010 | Syntax: C | Size: 4.02 KB | Hits: 32 | Expires: Never
Copy text to clipboard
  1. #include "stdafx.h"
  2. #include <cv.h>
  3. #include <cxcore.h>
  4. #include <highgui.h>
  5.  
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <cstdlib>
  13. using namespace std;
  14.  
  15. #define NUM_N 262144
  16. #define NUM_T 49152
  17. #define PATCH_SIZE 8
  18.  
  19. IplImage* open_image (int image_id, int flg){
  20.         char fname[256];
  21.  
  22.         if(flg == 1) {
  23.                 sprintf (fname, "C:/Users/Panna/Documents/MATLAB/cs186/gauss/gaussT/imT%d.bmp", image_id);
  24.         }
  25.         if(flg == 2) {
  26.                 sprintf (fname, "C:/Users/Panna/Documents/MATLAB/cs186/gauss/gaussN/imN%d.bmp", image_id);
  27.         }
  28.  
  29.         return cvLoadImage(fname);
  30. }
  31.  
  32. /*IplImage* get_patch(IplImage* I,int p_num) {
  33.         IplImage* mypatch = cvCreateImage(cvSize(PATCH_SIZE,PATCH_SIZE),IPL_DEPTH_8U,1);//0;
  34.         CvScalar s;
  35.         int row = (int)(p_num / 128);
  36.         int col = (int)(p_num % 128);
  37.         for(int i=0;i<PATCH_SIZE;i++) {
  38.                 for(int j=0;j<PATCH_SIZE;j++) {
  39.                         s = cvGet2D(I,PATCH_SIZE*row+i,PATCH_SIZE*col+j);
  40.                         cvSet2D(mypatch,i,j,s);
  41.                 }
  42.         }
  43.         return mypatch;
  44. }
  45.  
  46. int get_cumsum(IplImage* Tpatch,IplImage* Npatch) {
  47.         int cumsum = 0;
  48.         CvScalar Tpix; CvScalar Npix;
  49.         for(int i=0; i<8; i++){
  50.                 for(int j=0; j<8; j++){
  51.                         Tpix = cvGet2D(Tpatch,i,j);
  52.                         Npix = cvGet2D(Npatch,i,j);
  53.                         cumsum += (Tpix.val[0]-Npix.val[0])*(Tpix.val[0]-Npix.val[0]);
  54.                 }
  55.         }
  56.         return cumsum;
  57. }
  58. */
  59. int main(void) {
  60.  
  61.         //cvNamedWindow("t1",CV_WINDOW_AUTOSIZE);
  62.  
  63.         int num_patches = (int)(1024*1024/(PATCH_SIZE*PATCH_SIZE));
  64.         IplImage* T;
  65.         IplImage* N;
  66.         IplImage* curTpatch = cvCreateImage(cvSize(PATCH_SIZE,PATCH_SIZE),IPL_DEPTH_8U,1);
  67.         IplImage* curNpatch = cvCreateImage(cvSize(PATCH_SIZE,PATCH_SIZE),IPL_DEPTH_8U,1);
  68.  
  69.         CvScalar s; CvScalar Tpix; CvScalar Npix;
  70.  
  71.         int p_num; int row; int col; int cumsum;
  72.         FILE *myfile = fopen("test.txt","w");
  73.  
  74.         for(int tcnt=1;tcnt<2;tcnt++){
  75.                 T = open_image((int)(tcnt / num_patches)+1,1);
  76.                 p_num = (int)(tcnt % num_patches);
  77.                 row = (int)(p_num / 128);
  78.             col = (int)(p_num % 128);
  79.                 for(int i=0;i<PATCH_SIZE;i++) {
  80.                 for(int j=0;j<PATCH_SIZE;j++) {
  81.                         s = cvGet2D(T,PATCH_SIZE*row+i,PATCH_SIZE*col+j);
  82.                         cvSet2D(curTpatch,i,j,s);
  83.                         }
  84.                 }
  85.                 //curTpatch = get_patch(T,(int)(tcnt % num_patches));
  86.                 for(int ncnt=1;ncnt<2;ncnt++){
  87.                         N = open_image((int)(ncnt / num_patches)+1,2);
  88.                         p_num = (int)(ncnt % num_patches);
  89.                         row = (int)(p_num / 128);
  90.                         col = (int)(p_num % 128);
  91.                         for(int i=0;i<PATCH_SIZE;i++) {
  92.                         for(int j=0;j<PATCH_SIZE;j++) {
  93.                                 s = cvGet2D(N,PATCH_SIZE*row+i,PATCH_SIZE*col+j);
  94.                                 cvSet2D(curNpatch,i,j,s);
  95.                                 }
  96.                         }
  97.                         //curNpatch = get_patch(N,(int)ncnt % num_patches);
  98.                         cumsum = 0;
  99.                         for(int i=0; i<8; i++){
  100.                                 for(int j=0; j<8; j++){
  101.                                         Tpix = cvGet2D(curTpatch,i,j);
  102.                                         Npix = cvGet2D(curNpatch,i,j);
  103.                                         cumsum += (Tpix.val[0]-Npix.val[0])*(Tpix.val[0]-Npix.val[0]);
  104.                                 }
  105.                         }
  106.                         //cum_sum = get_cumsum(curTpatch,curNpatch);
  107.                         //printf("the cum_sum is: %d",cum_sum);
  108.                         fprintf(myfile, "%d ", cumsum);
  109.                         if(ncnt % 50 == 0)
  110.                                 printf("ncnt: %d\n",ncnt);
  111.                 }
  112.                 printf("tcnt: %d\n",tcnt);
  113.                 fprintf(myfile,"\n"); //newline.
  114.         }
  115.  
  116.         //write cumsum to a file..
  117.         /*FILE *myfile = fopen("test.txt", "w");
  118.         for(int row=0;row<NUM_T;row++)
  119.         {
  120.                 for(int col=0;col<NUM_N;col++)
  121.                 {
  122.                         fprintf (myfile, "%d ", cum_sum[row][col]);
  123.                 }
  124.                 fprintf (myfile, "\n"); // newline
  125.         }*/
  126.  
  127.         //cvShowImage("t1",T);
  128.  
  129.         cvReleaseImage(&T); cvReleaseImage(&N); cvReleaseImage(&curTpatch); cvReleaseImage(&curNpatch);
  130.         system("PAUSE");
  131.        
  132.         /*while(1)
  133.         {
  134.                 int key_pressed;
  135.                 key_pressed = cvWaitKey(0);
  136.  
  137.                 if(key_pressed == 27) {
  138.                         // release the image
  139.                         cvReleaseImage(&T);
  140.                         return 0;
  141.                 }
  142.         }*/
  143. }
  144.  
  145.  
  146.  
  147. /*      for(int ncnt=1;ncnt<NUM_N+1;ncnt++) {
  148.                 cumsum[tcnt-1][ncnt-1]=0;
  149.                 printf("cumsum:%d\n",cumsum[tcnt-1][ncnt-1]);
  150.                 N = open_image(ncnt,2);
  151.  
  152.                 for(int i=0; i<PATCH_SIZE; i++){
  153.                         for(int j=0; j<PATCH_SIZE; j++){
  154.                                 Tpix = cvGet2D(T,i,j);
  155.                                 Npix = cvGet2D(N,i,j);
  156.                                 cumsum[tcnt-1][ncnt-1] += (Tpix.val[0]-Npix.val[0])*(Tpix.val[0]-Npix.val[0]);
  157.                         }
  158.                 }
  159.                 printf("cumsum:%d\n",cumsum[tcnt-1][ncnt-1]);
  160.         }
  161. }*/