Kuwahara过滤器的奇怪结果

时间:2013-03-25 05:54:09

标签: c++ opencv image-processing computer-vision

我正在用C ++实现Kuwahara过滤器,使用OpenCV来帮助打开和显示图像。这个想法非常简单,但不知怎的,我得到了奇怪的结果。这是'cose:

#include "opencv2/opencv.hpp"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
using namespace cv;

//This class is essentially a struct of 4 Kuwahara regions surrounding a pixel, along with each one's mean, sum and variance.
class Regions{
    int* Area[4];
    int Size[4];
    unsigned long long Sum[4];
    double Var[4];
    int kernel;
public:
    Regions(int _kernel) : kernel(_kernel) {
        for (int i = 0; i<4; i++) {
            Area[i] = new int[kernel*kernel];
            Size[i] = 0;
            Sum[i] = 0;
            Var[i] = 0.0;
        }
    }

    //Update data, increase the size of the area, update the sum
    void sendData(int area, int data){
        Area[area][Size[area]] = data;
        Sum[area] += data;
        Size[area]++;
    }
    //Calculate the variance of each area
    double var(int area) {
        int __mean = Sum[area]/Size[area];
        double temp = 0;
        for (int i = 0; i<Size[area]; i++) {
            temp+= (Area[area][i] - __mean) * (Area[area][i] - __mean);
        }
        if (Size[area]==1) return 1.7e38; //If there is only one pixel inside the region then return the maximum of double
                                           //So that with this big number, the region will never be considered in the below minVar()
        return sqrt(temp/(Size[area]-1));
    }
    //Call the above function to calc the variances of all 4 areas
    void calcVar() {
        for (int i = 0; i<4; i++) {
            Var[i] = var(i);
        }
    }
    //Find out which regions has the least variance
    int minVar() {
        calcVar();
        int i = 0;
        double __var = Var[0];
        if (__var > Var[1]) {__var = Var[1]; i = 1;}
        if (__var > Var[2]) {__var = Var[2]; i = 2;}
        if (__var > Var[3]) {__var = Var[3]; i = 3;}
        return i;
    }

    //Return the mean of that regions
    uchar result(){
        int i = minVar();
        return saturate_cast<uchar> ((double) (Sum[i] *1.0 / Size[i]));
    }
};

class Kuwahara{
private:
    int wid, hei, pad, kernel;
    Mat image;
public:
    Regions getRegions(int x, int y){
        Regions regions(kernel);

        uchar *data = image.data;

        //Update data for each region, pixels that are outside the image's boundary will be ignored.

        //Area 1 (upper left)
        for (int j = (y-pad >=0)? y-pad : 0; j>= 0 && j<=y && j<hei; j++)
            for (int i = ((x-pad >=0) ? x-pad : 0); i>= 0 && i<=x && i<wid; i++) {
                regions.sendData(1,data[(j*wid)+i]);
            }
        //Area 2 (upper right)
        for (int j = (y-pad >=0)? y-pad : 0; j<=y && j<hei; j++)
            for (int i = x; i<=x+pad && i<wid; i++) {
                regions.sendData(2,data[(j*wid)+i]);
            }
        //Area 3 (bottom left)
        for (int j = y; j<=y+pad && j<hei; j++)
            for (int i = ((x-pad >=0) ? x-pad : 0); i<=x && i<wid; i++) {
                regions.sendData(3,data[(j*wid)+i]);
            }
        //Area 0 (bottom right)
        for (int j = y; j<=y+pad && j<hei; j++)
            for (int i = x; i<=x+pad && i<wid; i++) {
                regions.sendData(0,data[(j*wid)+i]);
            }
        return regions;
    }

    //Constructor
    Kuwahara(const Mat& _image, int _kernel) : kernel(_kernel) {
        image = _image.clone();
        wid = image.cols; hei = image.rows;
        pad = kernel-1;
    }

    //Create new image and replace its pixels by the results of Kuwahara filter on the original pixels
    Mat apply(){
        Mat temp;
        temp.create(image.size(), CV_8U);
        uchar* data = temp.data;

        for (int j= 0; j<hei; j++) {
            for (int i = 0; i<wid; i++)
                data[j*wid+i] = getRegions(i,j).result();
        }
        return temp;
    }
};

int main() {
    Mat img = imread("limes.tif", 1);
    Mat gray, dest;
    int kernel = 15;
    gray.create(img.size(), CV_8U);
    cvtColor(img, gray, CV_BGR2GRAY);

    Kuwahara filter(gray, kernel);

    dest = filter.apply();

    imshow("Result", dest);
    imwrite("result.jpg", dest);
    waitKey();
}

这是结果: enter image description here

正如您所看到的,它与正确的结果不同,这些石灰的边界似乎是重复的并向上移动。如果我使用15x15过滤器,它会给我一个完整的混乱:

enter image description here

我花了一整天的时间来调试,但到目前为止还没有找到。我甚至手工对小图像进行了计算,并与结果进行了比较,没有看到任何差异。 任何人都可以帮我找出我做错了什么? 非常感谢。

1 个答案:

答案 0 :(得分:6)

事实证明我的代码没有任何问题,但我定义内核的方式是问题的根源。我的内核实际上是四个小kuwahara部分之一,而内核的正确定义是为每个像素计算数据的整个区域,因此包含所有四个部分的区域实际上是内核。所以当谈到一个7x7“内核”时,我实际应用了一个15x15的内核,并且可怕的结果不是来自我认为的15x15内核,而是来自31x31。在这样的规模下,Kuwahara过滤器根本没有意义,奇怪的结果是不可避免的。