Is there a simple way to set all values in a cv::Mat to a given value if they fulfill some condition. For instance, I have CV_32FC1, and I want set all values which are 0 to 20. In MATLAB I would have simply done this:
M(M == 0) = 20;
Examples of OpenCV Mat Given below are the examples of OpenCV Mat: Example #1 OpenCV program in C++ to create a matrix using Mat function and display the matrix as the output on the screen.
The scalar parameter specifies the data to be stored in the matrix. Given below are the examples of OpenCV Mat: OpenCV program in C++ to create a matrix using Mat function and display the matrix as the output on the screen. In the above program, we are including the necessary headers.
OpenCV program in C++ to create a matrix using Mat function and display the matrix as the output on the screen. In the above program, we are including the necessary headers. Then we are defining the namespaces std and cv.
M.step [M.dims-1] is minimal and always equal to the element size M.elemSize () . So, the data layout in Mat is fully compatible with CvMat, IplImage, and CvMatND types from OpenCV 1.x.
You can use
cv::Mat mask = M == 0;
M.setTo(0.5, mask);
However, it includes using additional memory for creating mask, but is a solution using opencv API therefore can be applied to all matrix types. If you consider performance issues, you can always refer directly to Mat::data to optimize this solution for concrete matrix type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With