Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 414
sam ran Avatar asked Jan 15 '15 14:01

sam ran


2 Answers

Naah. to do that, just one line:

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

as simple as that.

like image 119
Ashutosh Gupta Avatar answered Nov 07 '22 10:11

Ashutosh Gupta


What you want is to truncate the image with cv::threshold.

The following should do what you require:

cv::threshold(dst, dst, 50, 0, CV_THRESH_TOZERO_INV);

this is the function definition

double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)

http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold

like image 40
Gilad Avatar answered Nov 07 '22 12:11

Gilad