Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to Multiply cv::Mat with mask

I'd like multiply two arrays(cv::Mat) with mask for speed up application.

In add and subtraction exists

mask - optional operation mask - 8-bit single channel array, that specifies elements of the output array to be changed

Provide OpenCV similar functionality for multiplication and divide?

I can fill multiplier by mask with neutral element(1). But this is slow down.

I can multiply with operation per pixel(two cycles through columns and rows), but in this case I need to know data type(uchar, double, int, ...) for template function Mat::at<datatype>

How to Multiply cv::Mat with mask

I'd like multiply two arrays(cv::Mat) with mask for speed up application.

In add and subtraction exists

mask - optional operation mask - 8-bit single channel array, that specifies elements of the output array to be changed

Provide OpenCV similar functionality for multiplication and divide?

I can fill multiplier by mask with neutral element(1). solve it for full image and then copy data to output. But this is slow down.

cv::Mat input1(size, CV_8U|CV_16S|CV_32F);
cv::Mat input2(size, CV_8U|CV_16S|CV_32F); // or cv::Scalar
cv::Mat mask(size, CV_8U);
cv::Mat output(size, CV_8U|CV_16S|CV_32F); 

// per pixel multiplication input1 and input2 
cv::multiply(input1, input2, output);
//cv::multiply(input1, input2, output, mask);

I can multiply with operation per pixel(two cycles through columns and rows), but way around for same result:

cv::Mat multiplyFull;
cv::multiply(input1, input2, multiplyFull);

// clear data in this case I need to know data type(uchar, double, int, ...) for template function Mat::at<datatype>

output output.setTo(cv::Scalar::all(0), mask); // set in output only multiplication given by mask cv::add(output, multiplyFull, output, mask);

How to Multiply cv::Mat with mask

I'd like multiply two arrays(cv::Mat) with mask for speed up application.

In add and subtraction exists

mask - optional operation mask - 8-bit single channel array, that specifies elements of the output array to be changed

Provide OpenCV similar functionality for multiplication and divide?

I can solve it for full image and then copy data to output. But this is slow down.

cv::Mat input1(size, CV_8U|CV_16S|CV_32F);
cv::Mat input2(size, CV_8U|CV_16S|CV_32F); // or cv::Scalar
cv::Mat mask(size, CV_8U);
cv::Mat output(size, CV_8U|CV_16S|CV_32F); 

// per pixel multiplication input1 and input2 
cv::multiply(input1, input2, output);
//cv::multiply(input1, input2, output, mask);

way around for same result:

cv::Mat multiplyFull;
cv::multiply(input1, input2, multiplyFull);

// clear data in output
output.setTo(cv::Scalar::all(0), mask);

// set in output only multiplication given by mask
cv::add(output, multiplyFull, output, mask);