Set each Mata pixel to a specific value, if it is less than the value?

I am trying to do the following in OpenCV. How can I set each Mat pixel to a specific value if it is less than the value?

So, I want to do something like threshold , but not quite like that, since I don't want to touch pixels above a given threshold.

For example: set each pixel to 50, which is less than 50.

Any ideas?

+4
source share
2 answers

Regarding your specific request:

set 50all pixels< 50

using matrix expressions and setTois simple:

Mat m = ...
m.setTo(50, m < 50);

OpenCV , cv:: threshold .

, , , 255 > th :

double th = 100.0;
Mat m = ...
Mat thresh;
threshold(m, thresh, th, 255, THRESH_BINARY);

double th = 100.0;
Mat m = ...
Mat thresh = m > th;

, 255 < th, :

double th = 100.0;
Mat m = ...
Mat thresh;
threshold(m, thresh, th, 255, THRESH_BINARY_INV); // <-- using INVerted threshold

:

double th = 100.0;
Mat m = ...
Mat thresh = m <= th; // <-- using less-or-equal comparison 

//Mat thresh = m < th; // Will also work, but result will be different from the "threshold" version

, threshold , CV_8U.

+5

LUT OpenCV, , . .

, CV_8U OpenCV ++- ( Python).

lookUpTable :

cv::Mat lookUpTable(256,1,CV_8U);
for(int i = 0; i < 256; i++) {
    if(i < 50)
        lookUpTable.at<uchar>(i,0) = 50;
    else
        lookUpTable.at<uchar>(i,0) = i;
}

, 50.

:

cv::Mat image;  # your original image
cv::Mat thresholdedImage;
cv::LUT(image, lookUpTable, thresholdedImage);

.

, , , - , .

0

All Articles