2013-09-30 111 views
0

下面是一個代碼:錯誤:返回值類型不匹配的功能類型

#include<stdio.h> 
#include<math.h> 

float conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float imageConvolved[][11]); //Function Prototype definition 

float conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float imageConvolved[][11]) 
{ 
    int kernelSize;  //This variable represents the size of the Gaussian kernel 
    int i;   //variable which controls the rows of an image 
    int j;   //variable which controls the columns of an image 
    int ii;   //variable which controls the rows of the kernel 
    int jj;   //variable which controls the columns of the kernel 
    float sum;   //variable that holds the result of convolution for a particular pixel of an image 
//float imageConvolved;//Result of an image convolved by a Gaussian kernel 
    int imagePixel; 
    float kernelPixel; 
    kernelSize = colsKernel; /*Since we consider a square kernel, then rowsKernel=colsKernel, which implies that the size of the     kernel (kernelSize) equals either of these two variables (that is, kernelSize=colsKernel=rowsKernel) */ 
    for (i = kernelSize/2; i < rowsImage - kernelSize/2; i++) // perform iteration through the rows of an image 
    { 
    for (j = kernelSize/2; j < colsImage - kernelSize/2; j++) // perform iteration through the columns of an image 
    { 
     sum = 0;  /*Initializing the accumulator. This variable will finally contain the convolution result for a particular pixel */ 
     for (ii = -kernelSize/2; ii <= kernelSize/2; ii++) // perform iteration through the rows of a kernel 
     { 
     for (jj = -kernelSize/2; jj <= kernelSize/2; jj++) //perform iteration through the columns of a kernel 
     { 
      imagePixel = image[i + ii][j + jj]; 
      kernelPixel = kernel[ii + kernelSize/2][jj + kernelSize/2]; 

      sum += imagePixel * kernelPixel; 
     } 
     } 
     imageConvolved[i - kernelSize/2][j - kernelSize/2] = sum; 
    } 
    } 
    return (imageConvolved); // convolved image 
} 

int main() 
{ 
    float gauss[][5] = { 
    {1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1} 
    }; 
    int img[][15] = { 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 
    }; 
    int rowsK = 5, colsK = 5, rowsI = 15, colsI = 15; 
    float result[11][11]; 
    float Iconv[11][11]; 
    Iconv = conv2D(rowsK, colsK, rowsI, colsI, gauss, img, result); 
    return 0; 
} 

我修改了代碼作爲一種嘗試,試圖解決這個問題。但是,我得到兩個錯誤(使用的CCStudio V3.3):

"convolution.c", line 39: error: return value type does not match the function type "convolution.c", line 71: error: expression must be a modifiable lvalue

+1

請粘貼**真實代碼**,包括每個參數及其類型的聲明,**作爲代碼**;沒有被埋入一段。你的描述,「這個函數中的參數被聲明爲浮點結果,int rowsK,int colsK,int rowsI和int colsI」,它很容易丟失最後兩個參數類型,如果它不明顯,它們是至關重要的找到你的問題。因此諷刺的是,他們不包括在內。總之,不要只告訴我們「關於」你的代碼; **展示下**。 – WhozCraig

+2

什麼是'imageConvolved'?您可以將它作爲二維數組訪問並將其作爲「float」返回。你要哪個?另外,我同意上述評論。你的問題在眼睛上很難。包含代碼作爲代碼塊,而不是作爲段落內的粗體塊。 – paddy

+0

你說imageConvolved被定義爲float,但是你表現得像2D數組。並且你從返回float的函數返回它,所以它看起來是一個純粹的float值,你試圖用作二維數組..如果你只是複製和粘貼你的代碼可能會更好.. – phoad

回答

0

您正在返回什麼似乎是從根據其聲明返回float功能的二維陣列(imageConvolved)。我不知道你的意思是從函數返回什麼,但它似乎錯誤是在這條線:

return(imageConvolved); 
0

的功能conv2D原型是浮動。但是,您在定義中返回了imageConvolvedimageConvolved是你的ormal parameter中的數組,你不需要返回它,所以你會返回float類型insead。

+0

問題已經解決(當我使用GCC編譯器進行編譯時)。已經使得函數conv2D不會在調用點返回任何東西,並且在main()函數中,我將另一個參數收集到函數conv2D中。然後,我可以輕鬆地從main()函數讀取結果。 – user2829606