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

4

1 回答 1

2

请注意,当您对像素求和时,您添加了unsigned chars 并且您可能会溢出。

通过将这些像素值转换为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;
  }
}
于 2013-03-01T15:59:44.277 回答