Ask Your Question

Revision history [back]

You can try :

    multiply(x, y, z);
    bitwise_not(mask,maskneg);
    x.copyTo(z,maskneg);

You can try :

    multiply(x, y, z);
    bitwise_not(mask,maskneg);
    x.copyTo(z,maskneg);

or you can try this :

template<typename Type>
class ParallelMultiplyWithMask : public ParallelLoopBody
{
private:
    Mat &imgSrc1;
    Mat &imgSrc2;
    Mat &mask;
    Mat &dst;

public:
    ParallelMultiplyWithMask<Type>(Mat& img1,Mat &img2, Mat &d, Mat &m) :
        imgSrc1(img1),
        imgSrc2(img2),
        dst(d),
        mask(m)
    {}
    virtual void operator()(const Range& range) const CV_OVERRIDE
    {

        for (int y = range.start; y < range.end; y++)
        {
            uchar *m = mask.ptr<uchar>(y);
            Type *vDst = dst.ptr<Type>(y);
            Type *vSrc1 = imgSrc1.ptr<Type>(y);
            Type *vSrc2 = imgSrc2.ptr<Type>(y);
            for (int x = 0; x < imgSrc1.cols; x++, vDst++, vSrc1++, vSrc2++, m++)
            {
                if (*m)
                    *vDst = *vSrc1 * *vSrc2;
                else
                    *vDst = *vSrc1;
            }

        }
    }
};


void multiplywithMask(InputArray _src1, InputArray _src2,  OutputArray _dst, InputArray _mask)
{
    CV_Assert(_src1.sameSize(_src2) && _src1.type() == _src2.type());
    CV_Assert(_src1.sameSize(_mask) );
    Mat mask = _mask.getMat();
    CV_Assert(mask.type() == CV_8U);
    Mat dst(_src1.size(), _src1.type());
    Mat src1 = _src1.getMat(), src2 = _src2.getMat();
    if (_src1.type() == CV_8UC1)
    {
        ParallelMultiplyWithMask<uchar> product(src1,src2, dst, mask);
        parallel_for_(Range(0, src1.rows), product);
    }
    else if (_src1.type() == CV_32FC1)

    {
        ParallelMultiplyWithMask<float> product(src1, src2, dst, mask);
        parallel_for_(Range(0, src1.rows), product);
    }
    dst.copyTo(_dst);

}

multiplywithMask(x, y, z, mask);