how to modify values of Mat according to some condition in opencv?

sam ran picture sam ran · Jan 15, 2015 · Viewed 8k times · Source

In Matlab a(a>50)=0 can replace all elements of a that are greater than 50 to 0. I want to do same thing with Mat in OpenCV. How to do it?

Answer

Ashutosh Gupta picture Ashutosh Gupta · May 10, 2017

Naah. to do that, just one line:

cv::Mat img = imread('your image path');
img.setTo(0,img>50);

as simple as that.