C ++,OpenCv:不更新成员变量的成员函数

时间:2017-08-20 04:05:55

标签: c++ opencv pointers reference mat

我有一个MainWindow类,它包含saveImageFile()函数。这会读取一个图像,将其放入Canvas对象,一个存储Layer对象列表的类,每个Layer包含两个OpenCv Mats,一个预效果Mat和一个后效果垫。

在saveImageFile()中,我在画布的图层[1]上应用了图像效果lumaToAlpha()。 lumaToAlpha()获取图层的预效果Mat,对其执行操作,并将其存储在图层的后效果垫中。该功能已经过测试。它有效。

然而,当我试图获得后效Mat时,我得到了原始的Mat。这就好像lumaToAlpha()根本没有做任何事情,或者是在完全不同的图层对象上执行的。

我相信这可能与指针有关,但我并不自信。出了什么问题?

MainWindow班......

//member variables of MainWindow...
Canvas mCanvas;    

void MainWindow::saveImageFile()
{

    Mat mat = imread("C://picture.jpg), CV_LOAD_IMAGE_GRAYSCALE);

    mCanvas.startCanvas(mat, Canvas::BGRA);

    mCanvas.getLayer(1).lumaToAlpha();
    Mat canvasMat = mCanvas.getLayer(1).getPostEffectMat();
    imwrite("C://picture-result.png", canvasMat);
    //getPostEffectMat is returning the wrong mat!

}

Canvas类......

//member variables of Canvas...
enum CanvasColorMode { GREY, BGRA };
CanvasColorMode mCanvasColorMode;
QList<Layer> mLayerSet;

Canvas::Canvas()
{
    //empty constructor
}

Layer Canvas::getLayer(int index)
{
    if(0 <= index && index < mLayerSet.size()){
        return mLayerSet.at(index);
    }
    throw std::invalid_argument("layer index not in range");
}

void Canvas::startCanvas(Mat base, CanvasColorMode mode)
{
    mLayerSet.clear();

    if (base.channels() == 1 && mode == Canvas::BGRA){
        cvtColor(base, base, CV_GRAY2BGRA);
    } else if (base.channels() > 1 && mode == Canvas::GREY){
        cvtColor(base, base, CV_BGRA2GRAY);
    }

    Layer layer00(makeCheckMat(base.rows, base.cols, mode));
    Layer layer01(base);

    mLayerSet.append(layer00);
    mLayerSet.append(layer01);
}

图层类......

//layer member variables
Mat mPreEffectMat;
Mat mPostEffectMat; 

Layer::Layer(Mat matPrePost)
{
    mPreEffectMat = matPrePost;
    mPostEffectMat = matPrePost;
}

Mat Layer::getPreEffectMat()
{
    return mPreEffectMat;
}

Mat Layer::getPostEffectMat()
{
    return mPostEffectMat;
}

void Layer::setPreEffectMat(Mat mat)
{
    mPreEffectMat = mat;

}

void Layer::setPostEffectMat(Mat mat)
{
    mPostEffectMat = mat;
    qDebug() << "setPostEffectMat";
}

void Layer::lumaToAlpha()
{
    Mat pre = getPreEffectMat();
    Mat post= Mat(pre.rows, pre.cols, CV_8UC4, Scalar::all(0) );

    //SNIP: converts luminence channel to alpha, function tested
    //this works exactly how I expect it to. The question is why
    //is the change not reflected in post mat

    setPostEffectMat(post);
}

1 个答案:

答案 0 :(得分:0)

我猜你有一个名为Private Sub Worksheet_Calculate() Call MoveRectangle End Sub 的成员变量。你希望在函数中设置它的值。但是,您在函数中所做的是定义函数局部变量。同名的成员变量不受您在函数中执行的操作的影响。

更改行

mat

// This defines a function local variable.
Mat mat = imread("C://picture.jpg), CV_LOAD_IMAGE_GRAYSCALE);