2011-08-09 68 views
3

大家好我有以下處理圖像的代碼。我是新來的C++和可視工作室。該程序使用opencv,所以我下載了它,並使我的項目指向構建配置中的必要庫。當我嘗試構建代碼時,它似乎無法識別opencv變量。任何想法,爲什麼當我已經將opencv庫添加到構建?C++不能識別opencv變量

#include <cv.h> 
    #include <highgui.h> 
    #include <math.h> 
    #include <iostream> 
    #include "StdAfx.h" 

    void sampleImage(const IplImage* arr, float idx0, float idx1, CvScalar& res) 
    { 
     if(idx0<0 || idx1<0 || idx0>(cvGetSize(arr).height-1) || idx1>(cvGetSize(arr).width-1)){ 
     res.val[0]=0; 
     res.val[1]=0; 
     res.val[2]=0; 
     res.val[3]=0; 
     return; 
     } 
     float idx0_fl=floor(idx0); 
     float idx0_cl=ceil(idx0); 
     float idx1_fl=floor(idx1); 
     float idx1_cl=ceil(idx1); 

     CvScalar s1=cvGet2D(arr,(int)idx0_fl,(int)idx1_fl); 
     CvScalar s2=cvGet2D(arr,(int)idx0_fl,(int)idx1_cl); 
     CvScalar s3=cvGet2D(arr,(int)idx0_cl,(int)idx1_cl); 
     CvScalar s4=cvGet2D(arr,(int)idx0_cl,(int)idx1_fl); 
     float x = idx0 - idx0_fl; 
     float y = idx1 - idx1_fl; 
     res.val[0]= s1.val[0]*(1-x)*(1-y) + s2.val[0]*(1-x)*y + s3.val[0]*x*y + s4.val[0]*x*(1-y); 
     res.val[1]= s1.val[1]*(1-x)*(1-y) + s2.val[1]*(1-x)*y + s3.val[1]*x*y + s4.val[1]*x*(1-y); 
     res.val[2]= s1.val[2]*(1-x)*(1-y) + s2.val[2]*(1-x)*y + s3.val[2]*x*y + s4.val[2]*x*(1-y); 
     res.val[3]= s1.val[3]*(1-x)*(1-y) + s2.val[3]*(1-x)*y + s3.val[3]*x*y + s4.val[3]*x*(1-y); 
    } 

    float xscale; 
    float yscale; 
    float xshift; 
    float yshift; 

    float getRadialX(float x,float y,float cx,float cy,float k){ 
     x = (x*xscale+xshift); 
     y = (y*yscale+yshift); 
     float res = x+((x-cx)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy))); 
     return res; 
    } 

    float getRadialY(float x,float y,float cx,float cy,float k){ 
     x = (x*xscale+xshift); 
     y = (y*yscale+yshift); 
     float res = y+((y-cy)*k*((x-cx)*(x-cx)+(y-cy)*(y-cy))); 
     return res; 
    } 

    float thresh = 1; 
    float calc_shift(float x1,float x2,float cx,float k){ 
     float x3 = x1+(x2-x1)*0.5; 
     float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx))); 
     float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx))); 

     // std::cerr<<"x1: "<<x1<<" - "<<res1<<" x3: "<<x3<<" - "<<res3<<std::endl; 

     if(res1>-thresh && res1 < thresh) 
     return x1; 
     if(res3<0){ 
     return calc_shift(x3,x2,cx,k); 
     } 
     else{ 
     return calc_shift(x1,x3,cx,k); 
     } 
    } 

    int main(int argc, char** argv) 
    { 
     IplImage* src = cvLoadImage(argv[1], 1); 
     IplImage* dst = cvCreateImage(cvGetSize(src),src->depth,src->nChannels); 
     IplImage* dst2 = cvCreateImage(cvGetSize(src),src->depth,src->nChannels); 
     float K=atof(argv[3]); 
     float centerX=atoi(argv[4]); 
     float centerY=atoi(argv[5]); 
     int width = cvGetSize(src).width; 
     int height = cvGetSize(src).height; 

     xshift = calc_shift(0,centerX-1,centerX,K); 
     float newcenterX = width-centerX; 
     float xshift_2 = calc_shift(0,newcenterX-1,newcenterX,K); 

     yshift = calc_shift(0,centerY-1,centerY,K); 
     float newcenterY = height-centerY; 
     float yshift_2 = calc_shift(0,newcenterY-1,newcenterY,K); 
     // scale = (centerX-xshift)/centerX; 
     xscale = (width-xshift-xshift_2)/width; 
     yscale = (height-yshift-yshift_2)/height; 

     std::cerr<<xshift<<" "<<yshift<<" "<<xscale<<" "<<yscale<<std::endl; 
     std::cerr<<cvGetSize(src).height<<std::endl; 
     std::cerr<<cvGetSize(src).width<<std::endl; 

     for(int j=0;j<cvGetSize(dst).height;j++){ 
     for(int i=0;i<cvGetSize(dst).width;i++){ 
      CvScalar s; 
      float x = getRadialX((float)i,(float)j,centerX,centerY,K); 
      float y = getRadialY((float)i,(float)j,centerX,centerY,K); 
      sampleImage(src,y,x,s); 
      cvSet2D(dst,j,i,s); 

     } 
     } 
    #if 0 
     cvNamedWindow("Source1", 1); 
     cvShowImage("Source1", dst); 
     cvWaitKey(0); 
    #endif 

     cvSaveImage(argv[2],dst,0); 

    #if 0 
     for(int j=0;j<cvGetSize(src).height;j++){ 
     for(int i=0;i<cvGetSize(src).width;i++){ 
      CvScalar s; 
      sampleImage(src,j+0.25,i+0.25,s); 
      cvSet2D(dst,j,i,s); 
     } 
     } 

     cvNamedWindow("Source1", 1); 
     cvShowImage("Source1", src); 
     cvWaitKey(0); 

    #endif 

} 

1>------ Build started: Project: sherize, Configuration: Debug Win32 ------ 
1> sherize.cpp 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(1): warning C4627: '#include <cv.h>': skipped when looking for precompiled header use 
1>   Add directive to 'StdAfx.h' or rebuild precompiled header 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(2): warning C4627: '#include <highgui.h>': skipped when looking for precompiled header use 
1>   Add directive to 'StdAfx.h' or rebuild precompiled header 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(3): warning C4627: '#include <math.h>': skipped when looking for precompiled header use 
1>   Add directive to 'StdAfx.h' or rebuild precompiled header 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(4): warning C4627: '#include <iostream>': skipped when looking for precompiled header use 
1>   Add directive to 'StdAfx.h' or rebuild precompiled header 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(7): error C2143: syntax error : missing ',' before '*' 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'idx0' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'idx1' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'idx0' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'arr' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2228: left of '.height' must have class/struct/union 
1>   type is ''unknown-type'' 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'idx1' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2065: 'arr' : undeclared identifier 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C2228: left of '.width' must have class/struct/union 
1>   type is ''unknown-type'' 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C3861: 'cvGetSize': identifier not found 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(9): error C3861: 'cvGetSize': identifier not found 
1>c:\users\mat\documents\visual studio 2010\projects\sherize\sherize\sherize.cpp(10): error C2065: 'res' : undeclared identifier 
1>c:\ 

回答

5

這是因爲您已將#include "stdafx.h"放在其他標頭之後。這是VC++的(默認)預編譯頭標記 - 在包含它之前丟棄所有內容並加載其預編譯頭狀態,即忽略cv.h include這裏。

這意味着您應該將stdafx.h include移動到文件頂部或將#include <cv.h>等移動到stdafx.h中。如果你想將它們包含在項目中的所有文件中,並且它們不可能在構建之間進行更改,那麼將它們添加到stdafx.h會將它們包括在預編譯頭文件中,並且它們將自動(並且有效地)用於項目中的所有來源。

+0

非常感謝,現在正在工作 – turtleboy

1

當您使用預編譯頭文件時,#include "stdafx.h"指令需要成爲包含文件列表中的第一個。所以改變包括的順序爲

#include "StdAfx.h" 
#include <cv.h> 
#include <highgui.h> 
#include <math.h> 
#include <iostream> 
+0

那麼,應該先包含預編譯頭文件。它可以用不同的名稱命名。像'gcc'這樣的好的編譯器甚至可以隱式地自動注入預編譯的頭文件,這樣可以很容易地在預編譯和常規模式之間切換,而不需要在定期編譯時從預編譯頭文件中處理大量的東西。 – 2011-08-09 17:47:34

+0

@praetorian謝謝 – turtleboy