Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mat element bulk modification : negative to 0, positive to 1

Tags:

c++

opencv

matrix

I have a matrix of negative and positive integers. I want to set negative elements to 0 and positive elements to 1. I do not want to set each element individually.

Is there any function/combination of functions in OpenCv that can perform this?

like image 437
Barshan Das Avatar asked Mar 18 '13 13:03

Barshan Das


2 Answers

Look at the function threshhold. Also, this tutorial explains how to get a binary image by applying a fixed-level threshold to each array element.

cv::Mat source_array, binary_output;
cv::threshold(source_array, binary_output, 0, 1, cv::THRESH_BINARY); 
like image 135
Alexey Avatar answered Oct 20 '22 10:10

Alexey


What you're doing is called thresholding. The answer depends on what language you're using. Below are a few examples.

C++

cv::threshold(m, m, 0, 1, cv::THRESH_BINARY);

C

cvThreshold(m, m, 0, 1, THRESH_BINARY);

Python (numpy, cv2)

m = m > 0

Python (cv)

cv.Threshold(m, m, 0, 1, cv.CV_THRESH_BINARY)
like image 37
Geoff Avatar answered Oct 20 '22 10:10

Geoff