OpenCV金字塔图像混合,c ++

时间:2016-08-03 07:49:00

标签: c++ image opencv colors

我正在尝试使用蒙版和金字塔方法混合到图像。我试图这样做,但我有一个问题,我可以混合图片,但颜色是错误的。 这是代码:

现在我要混合的图片是:

void main()
{
    int level = 4;
    Mat src1, src2, mask, nMask, result, one, two, temp, back1, back2;
    src2 = imread("bee.jpg");                   // openning the first picture
    src1 = imread("roundflower.jpg");           // second
    mask = imread("beeMask.jpg");               // mask
    resize(src2, src2, Size(src1.cols, src1.rows));     // resizing to the same size
    resize(mask, mask, Size(src1.cols, src1.rows));     // resizing to the same size 
    src2.convertTo(src2, CV_32F, 1.0 / 255.0);          // converting to 1.0 colors mode 
    src1.convertTo(src1, CV_32F, 1.0 / 255.0);
    mask.convertTo(mask, CV_32F, 1.0 / 255.0);
    nMask.convertTo(nMask, CV_32F, 1.0 / 255.0);
    subtract(1, mask, nMask);                           // creating the oposite mask 
    back1 = mask.mul(src1);                             // creating the background
    back2 = mask.mul(src2);                             // creating the object 
    vector<Mat> LA,LB,GR;                               // the pyarmids     
    buildPyramid(src1, LA, level);                      // building gaussian pyramid for pic1
    buildPyramid(src2, LB, level);                      
    buildPyramid(mask, GR, level);                      // gaussian pyramid of the mask 
    for (int i = 0; i < level; i++)
    {
        // creating laplacian pyramids for the pictures 
        temp = LA[i + 1];
        pyrUp(temp, temp,Size(LA[i].cols, LA[i].rows));
        LA[i] = LA[i] - temp;
        temp = LB[i + 1];
        pyrUp(temp, temp, Size(LB[i].cols, LB[i].rows));
        LB[i] = LB[i] - temp;
    }

    vector<Mat> LS;
    // function
    for (int i = 0; i < level; i++)
    {
        // using the function : LS(i) = GR(i)*LA(i) + (1-GR(i))*LB(i)
        GR[i].copyTo(temp);                     // copying the mask         
        one = temp.mul(LA[i]);                  // the first part 
        subtract(1, temp, nMask);               // oposite mask         
        two = nMask.mul(LB[i]);                 // the second part
        temp = one + two;
        LS.push_back(temp);                     // pushing to the new piramid 
    }

    LA.clear();
    result = LS[0];                 
    Size size = Size(LS[0].cols, LS[0].rows);
    while (LA.size() != 1)
    {
        // colapsing the pyramid 
        LA.clear();
        for (int i = LS.size()-1; i > 0; i--)
        {
            temp = LS[i];
            pyrUp(temp, temp, Size(LS[i-1].cols, LS[i-1].rows));
            LA.push_back(temp + LS[i - 1]);

        }
        reverse(LA.begin(), LA.end());
        LS = LA;

    }
    result = LS[0]; // showing the result 
    imwrite("result.jpg", result);
    imshow("Result", result);
    waitKey(0);




}

The Mask, The Object, The background

0 个答案:

没有答案