Difference between revisions of "Цифрови камери"

From Ilianko
Line 114: Line 114:
 
</pre></code>
 
</pre></code>
  
3. Извличане на Bayern pattern  
+
3. Извличане на Bayer pattern  
  
 
<code><pre>
 
<code><pre>
Line 141: Line 141:
 
           g = (g == 1) ? 0 : 1;
 
           g = (g == 1) ? 0 : 1;
 
           br = (br == 1) ? 0 : 1;
 
           br = (br == 1) ? 0 : 1;
         (img_bw->imageData + img_bw->widthStep*y)[x] =
+
         (img_bw->imageData + img_bw->widthStep*y)[x] =      
       
 
 
           ((uchar)((img->imageData + img->widthStep*y)[x*3+2])*br*r)+
 
           ((uchar)((img->imageData + img->widthStep*y)[x*3+2])*br*r)+
           ((uchar)((img->imageData + img->widthStep*y)[x*3+1])*g ) +
+
           ((uchar)((img->imageData + img->widthStep*y)[x*3+1])*br*b) + ((uchar)((img->imageData + img->widthStep*y)[x*3+1])*g*r)+
      ((uchar)((img->imageData + img->widthStep*y)[x*3])*br*b);   
+
      ((uchar)((img->imageData + img->widthStep*y)[x*3])*g*b);   
 
                                                       
 
                                                       
 
}
 
}
Line 159: Line 158:
 
   cvShowImage("bw", img_bw);
 
   cvShowImage("bw", img_bw);
 
    
 
    
   cvSaveImage("out2.bmp", img_bw, 0);
+
   cvSaveImage("raw.bmp", img_bw, 0);
  
 
   //wait for key to close the window   
 
   //wait for key to close the window   
Line 188: Line 187:
 
   img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 3 );
 
   img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 3 );
 
    
 
    
   img_bw = cvLoadImage("lennaBG.bmp",0);
+
   img_bw = cvLoadImage("raw.bmp",0);
 
    
 
    
 
   int b = 0, r = 1, g = 1, br = 0;
 
   int b = 0, r = 1, g = 1, br = 0;

Revision as of 07:36, 26 May 2011

1. Разглежда не R, G, B каналите

#include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
#include <opencv/cv.h> //main OpenCV functions

int main()
{
  
  IplImage *img, *img_r, *img_g, *img_b ; // image structer
  int x,y, w = 512, h = 512; //image size
  
  
  img_r = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 1 );
  img_g = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 1 );
  img_b = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 1 );
  
  img = cvLoadImage("lena_std.tif",1);
  
  for(x=0; x<img->width; x++)
	{
		for(y=0;y<img->height; y++)
		{
			(img_r->imageData + img_r->widthStep*y)[x]= (img->imageData + img->widthStep*y)[x*3+2];
			(img_g->imageData + img_g->widthStep*y)[x]= (img->imageData + img->widthStep*y)[x*3+1];
			(img_b->imageData + img_b->widthStep*y)[x]= (img->imageData + img->widthStep*y)[x*3];
		}
	}
  
 
  cvNamedWindow("red", 1);
  //display the image in the window
  cvShowImage("red", img_r);
  
  cvNamedWindow("green", 1);
  //display the image in the window
  cvShowImage("green", img_g);
  
  cvNamedWindow("blue", 1);
  //display the image in the window
  cvShowImage("blue", img_b);
  
  cvNamedWindow("rgb", 1);
  //display the image in the window
  cvShowImage("rgb", img);
  
  //wait for key to close the window  
  cvWaitKey(0);
  cvDestroyWindow( "blue" ); //destroy the window
  cvDestroyWindow( "red" ); 
  cvDestroyWindow( "green" );
  cvDestroyWindow( "rgb" );  
  cvReleaseImage( &img ); //release the memory for the image
  cvReleaseImage( &img_r );
  cvReleaseImage( &img_g );
  cvReleaseImage( &img_b );
  
return 0;
}


2. Получаване на черно бяло изображение

include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
#include <opencv/cv.h> //main OpenCV functions


int main()
{
  
  IplImage *img, *img_bw ; // image structure
  int x,y, w = 512, h = 512; //image size
  
  
  img_bw = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 1 );
  
  img = cvLoadImage("lena_std.tif",1);
  
  for(x=0; x<img->width; x++)
	{
		for(y=0;y<img->height; y++)
		{

			(img_bw->imageData + img_bw->widthStep*y)[x] =    
			   ((uchar)((img->imageData + img->widthStep*y)[x*3+2])  >> 2 ) +  ((uchar)((img->imageData + img->widthStep*y)[x*3+2])  >> 4 ) +
			   ((uchar)((img->imageData + img->widthStep*y)[x*3+1])  >> 1 ) +  ((uchar)((img->imageData + img->widthStep*y)[x*3+1])  >> 4 ) +
			   ((uchar)((img->imageData + img->widthStep*y)[x*3])    >> 3 );
			                                                      
		}
	}
  
 
  cvNamedWindow("rgb", 1);
  //display the image in the window
  cvShowImage("rgb", img);
  
  cvNamedWindow("bw", 1);
  //display the image in the window
  cvShowImage("bw", img_bw);
  
 
  //wait for key to close the window  
  cvWaitKey(0);
  cvDestroyWindow( "bw" );
  cvDestroyWindow( "rgb" );  
  cvReleaseImage( &img ); //release the memory for the image
  cvReleaseImage( &img_bw );
  
return 0;
}

3. Извличане на Bayer pattern

#include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
#include <opencv/cv.h> //main OpenCV functions


int main()
{
  
  IplImage *img, *img_bw ; // image structure
  int x,y, w = 512, h = 512; //image size
  
  
  img_bw = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 1 );
  
  img = cvLoadImage("lena_std.tif",1);
  
  int b = 1, r = 0, g = 1, br = 0;
  for(x=0; x<img->width; x++)
	{
		b = ( b == 1 ) ? 0 : 1;
		r = ( r == 1) ?  0 : 1;
		for(y=0;y<img->height; y++)
		{
           g = (g == 1) ? 0 : 1;
           br = (br == 1) ? 0 : 1;
        (img_bw->imageData + img_bw->widthStep*y)[x] =        
          ((uchar)((img->imageData + img->widthStep*y)[x*3+2])*br*r)+
          ((uchar)((img->imageData + img->widthStep*y)[x*3+1])*br*b) + ((uchar)((img->imageData + img->widthStep*y)[x*3+1])*g*r)+
	      ((uchar)((img->imageData + img->widthStep*y)[x*3])*g*b);  
			                                                      
		}
	}
  
 
  cvNamedWindow("rgb", 1);
  //display the image in the window
  cvShowImage("rgb", img);
  
  cvNamedWindow("bw", 1);
  //display the image in the window
  cvShowImage("bw", img_bw);
  
  cvSaveImage("raw.bmp", img_bw, 0);

  //wait for key to close the window  
  cvWaitKey(0);
  cvDestroyWindow( "bw" );
  cvDestroyWindow( "rgb" );  
  cvReleaseImage( &img ); //release the memory for the image
  cvReleaseImage( &img_bw );
  
return 0;
}

4. Преобразуване от bayer pattern в RGB

#include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
#include <opencv/cv.h> //main OpenCV functions


int main()
{
  
  IplImage *img, *img_bw ; // image structure
  int x,y, w = 512, h = 512; //image size
  
  
  img = cvCreateImage( cvSize( w, h ), IPL_DEPTH_8U, 3 );
  
  img_bw = cvLoadImage("raw.bmp",0);
  
  int b = 0, r = 1, g = 1, br = 0;
  for(x=1; x<img->width-1; x++)
	{
		b = ( b == 1 ) ? 0 : 1;
		r = ( r == 1) ?  0 : 1;
		for(y=1;y<img->height-1; y++)
		{
          g = (g == 1) ? 0 : 1;
          br = (br == 1) ? 0 : 1;
          
          (img->imageData + img->widthStep*y)[x*3] = (img_bw->imageData + img_bw->widthStep*(y-1*g))[x-1*r];
          
          (img->imageData + img->widthStep*y)[x*3+1] = (img_bw->imageData + img_bw->widthStep*(y+1*br))[x]*b +
                                                       (img_bw->imageData + img_bw->widthStep*(y+1*g))[x]*r;
          
          
	   (img->imageData + img->widthStep*y)[x*3+2]  =  (img_bw->imageData + img_bw->widthStep*(y+1*br))[x+1*b] ;
          
	                                                      
		}
	}
  
 
  cvNamedWindow("rgb", 1);
  //display the image in the window
  cvShowImage("rgb", img);
  
  cvNamedWindow("bw", 1);
  //display the image in the window
  cvShowImage("bw", img_bw);
  
  cvSaveImage("out1.bmp", img, 0);

  //wait for key to close the window  
  cvWaitKey(0);
  cvDestroyWindow( "bw" );
  cvDestroyWindow( "rgb" );  
  cvReleaseImage( &img ); //release the memory for the image
  cvReleaseImage( &img_bw );
  
return 0;
}

5. Оценка на разликите