Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV cv::Mat set if

Tags:

opencv

mat

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;
like image 896
ahmadh Avatar asked May 09 '14 20:05

ahmadh


People also ask

What are the examples of OpenCV mat?

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.

What is the scalar parameter in OpenCV mat?

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.

How to create a matrix in OpenCV using C++?

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.

Is the data layout in mat compatible with cvmat?

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.


Video Answer


1 Answers

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.

like image 93
marol Avatar answered Sep 23 '22 03:09

marol