2013-03-01 111 views
1

我想要过程图像,因此每个像素值将是其值和4个邻居的平均值。具有两种功能差异的平均图像

创建了两个不同的功能:

Mat meanImage(cv::Mat& inputImage) 
{ 
    Mat output; 
    Mat kernel(3,3,CV_32F,0.0); 
    kernel.at<float>(0,1) = 0.2; 
    kernel.at<float>(1,0) = 0.2; 
    kernel.at<float>(1,1) = 0.2; 
    kernel.at<float>(1,2) = 0.2; 
    kernel.at<float>(2,1) = 0.2; 
    filter2D(inputImage,output,-1,kernel); 
    return output; 
} 

和:

Mat meanImage2(Mat& inputImage) 
{ 
    Mat temp; 
    Mat output(inputImage.rows,inputImage.cols,inputImage.type()); 
    copyMakeBorder(inputImage,temp,1,1,1,1,BORDER_REPLICATE); 
    CV_Assert(output.isContinuous()); 
    CV_Assert(temp.isContinuous()); 
    const int len = output.rows * output.cols * output.channels(); 
    const int rowLenTemp = temp.cols * temp.channels(); 
    const int twoRowLenTemp = 2 * rowLenTemp; 
    const int rowLen = output.cols * output.channels(); 
    uchar* outPtr = output.ptr<uchar>(0); 
    uchar* tempPtr = temp.ptr<uchar>(0); 
    for(int i = 0; i < len; ++i) 
    { 
     const int a = 6 * (i/rowLen) + 3; 
     outPtr[i] = (tempPtr[i+rowLenTemp+a] + tempPtr[i+a] + 
        tempPtr[i+rowLenTemp+a+3] + tempPtr[i+rowLenTemp+a-3] + 
        tempPtr[i+twoRowLenTemp+a])/5; 
    } 
    return output; 
} 

我认为结果会是一样的。所以我比较了图片:

Mat diff; 
compare(meanImg1,meanImg2,diff,CMP_NE); 
printf("Difference: %d\n",countNonZero(diff)); 
imshow("diff",diff); 

并获得很多差异。 这个函数有什么区别?

编辑: 差异性的lena图像从Lena

Lena_diff

+0

什么是你的图像类型? – cedrou 2013-03-01 16:23:27

+0

类型是CV_UC3。 – krzych 2013-03-01 17:07:20

+0

你的意思是CV _ ** 8 ** UC3? – cedrou 2013-03-01 21:41:48

回答

2

采取小心,当你做像素的总和,你添加unsigned char S和你可能会溢出。

通过将这些像素值转换为int来测试您的代码。

outPtr[i] = ((int)tempPtr[i+rowLenTemp+a] + (int)tempPtr[i+a] + 
      (int)tempPtr[i+rowLenTemp+a+3] + (int)tempPtr[i+rowLenTemp+a-3] + 
      (int)tempPtr[i+twoRowLenTemp+a])/5; 

编辑:我宁愿这个代码所示(假设图像类型是UCHAR,它有3个通道)

for (int r = 0; r < output.rows; r++) 
{ 
    uchar* previousRow = temp.ptr<uchar>(r) + 3; 
    uchar* currentRow = temp.ptr<uchar>(r+1) + 3; 
    uchar* nextRow = temp.ptr<uchar>(r+2) + 3; 

    uchar* outRow = output.ptr<uchar>(r); 

    for (int c = 0; c < 3*output.cols; c++) 
    { 
    int value =    (int)previousRow[c] + 
     (int)currentRow[c-3] + (int)currentRow [c] + (int)currentRow[c+3] + 
          (int)nextRow [c]; 

    outRow[c] = value/5; 
    } 
} 
+0

感谢您的优化建议。它比我的实现更快。我也应用了对int的建议,并看到溢出的可能性。有线的事情是,它没有任何区别,我已经测试了不同的图像。这个答案没有回答我的问题。您的实现结果与我的实现相同,仍与'cv :: filter2D'的'meanImage'不同。 – krzych 2013-03-01 17:33:51

+0

它也应该是'currentRow [c-3]'和'currentRow [c + 3]'我建议编辑。 – krzych 2013-03-01 17:49:57

+0

是的,你是对的...我编辑它。 – cedrou 2013-03-01 21:34:56