具有两个功能差异的平均图像

时间:2013-03-01 15:53:49

标签: opencv image-processing

我想要过程图像,因此每个像素值将是其值和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

1 个答案:

答案 0 :(得分:2)

请注意,当您执行像素总和时,您会添加unsigned char并且可能会溢出。

通过将这些像素值投射到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;
  }
}