2013年4月29日 星期一

00360124_吳鍾昊_相框


今天的作業我整個DE了一整節課的BUG!!!

我的程式一直溢位....

最後才發現一個小小的關鍵 導致我程式的問題!!!


找到後真的超有成就感!!!
不過中間真的想到快發瘋了!!!!!!



-----------------------
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "stdio.h"

#define alpha 0.3
#define alpha2 0.9
#define x1 130
#define y1 170
#define x2 0
#define y2 0

CvCapture *capture;
IplImage *frame;





int main(int argc, char** argv )
{
   
IplImage* pImg = NULL;
IplImage* bImg = NULL;
IplImage* gImg = NULL;
IplImage* rImg = NULL;
IplImage* logoImg = NULL;
IplImage* logoImg2 = NULL;
IplImage* mergeImg = NULL;
   

//聲明IplImage指針

//創建視窗
capture=cvCreateCameraCapture(0);
cvNamedWindow("Original Image", 1);
cvNamedWindow("Blue Image", 1);
cvNamedWindow("Green Image", 1);
cvNamedWindow("Red Image", 1);
cvNamedWindow("Image with Logo", 1);
logoImg=cvLoadImage("logo.jpg");





while(1)
{
logoImg2=cvLoadImage("123.jpg");
pImg=cvQueryFrame(capture);
bImg = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U, 3);
gImg = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U, 3);
rImg = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U, 3);
mergeImg = cvCreateImage(cvSize(pImg->width,pImg->height), IPL_DEPTH_8U,3);


//為圖像申請空間



// Get blue pixels
for (int y=0; y<pImg->height; y++) {
  uchar* ptr1=(uchar*) (pImg->imageData +y*pImg->widthStep);
  uchar* ptr2=(uchar*) (bImg->imageData +y*bImg->widthStep);
  for (int x=0; x<pImg->width; x++) {    
     ptr2[3*x]=ptr1[3*x];
     ptr2[3*x+1]=0;
     ptr2[3*x+2]=0;  
     }
}

// Get green pixels
for (int y=0; y<pImg->height; y++) {
  uchar* ptr1=(uchar*) (pImg->imageData +y*pImg->widthStep);
  uchar* ptr2=(uchar*) (gImg->imageData +y*gImg->widthStep);
  for (int x=0; x<pImg->width; x++) {    
     ptr2[3*x]=0;
     ptr2[3*x+1]=ptr1[3*x+1];
     ptr2[3*x+2]=0;  
     }
}

// Get red pixels
for (int y=0; y<pImg->height; y++) {
  uchar* ptr1=(uchar*) (pImg->imageData +y*pImg->widthStep);
  uchar* ptr2=(uchar*) (rImg->imageData +y*rImg->widthStep);
  for (int x=0; x<pImg->width; x++) {    
     ptr2[3*x]=0;
     ptr2[3*x+1]=0;
     ptr2[3*x+2]=ptr1[3*x+2];  
     }
}


  mergeImg=cvCloneImage(pImg);
  for (int y=0; y<logoImg2->height; y++) {
    uchar* ptr1=(uchar*) (logoImg2->imageData +y*logoImg2->widthStep);
    uchar* ptr2=(uchar*) (mergeImg->imageData +(y+y2)*mergeImg->widthStep);
    for (int x=0; x<logoImg2->width; x++) {    
       if(ptr1[3*(x)] >230 && ptr1[3*(x)+1] >230 && ptr1[3*(x)+2] >230 ){
       ptr1[3*(x)]=ptr2[3*(x+x2)] ;
       ptr1[3*(x)+1]=ptr2[3*(x+x2)+1] ;
       ptr1[3*(x)+2]=ptr2[3*(x+x2)+2] ;
               }
       }
    }  




if( (logoImg->height + y1) < pImg->height) {
  //mergeImg=cvCloneImage(pImg);
  for (int y=0; y<logoImg->height; y++) {
    uchar* ptr1=(uchar*) (logoImg->imageData +y*logoImg->widthStep);
    uchar* ptr2=(uchar*) (pImg->imageData +(y+y1)*pImg->widthStep);
    for (int x=0; x<logoImg->width; x++) {    
       ptr2[3*(x+x1)]=alpha*ptr1[3*(x)] + (1-alpha)*ptr2[3*(x+x1)];
       ptr2[3*(x+x1)+1]=alpha*ptr1[3*(x)+1] + (1-alpha)*ptr2[3*(x+x1)+1];
       ptr2[3*(x+x1)+2]=alpha*ptr1[3*(x)+2] + (1-alpha)*ptr2[3*(x+x1)+2];  
       }
    }  
}








//顯示圖像
cvShowImage( "Original Image", pImg );
cvShowImage( "Blue Image", bImg );
cvShowImage( "Green Image", gImg );
cvShowImage( "Red Image", rImg );
cvShowImage( "Image with Logo", logoImg2 );





cvWaitKey(10); //等待按鍵

//銷毀視窗


}
cvReleaseImage( &pImg );
cvReleaseImage( &bImg );
cvReleaseImage( &gImg );
cvReleaseImage( &rImg );
cvReleaseImage( &mergeImg );

cvDestroyWindow( "Original Image" );
cvDestroyWindow( "Blue Image" );
cvDestroyWindow( "Green Image" );
cvDestroyWindow( "Red Image" );
cvDestroyWindow( "Image with Logo" );






}

沒有留言:

張貼留言