user5650610
user5650610

Reputation: 33

C++ opencv custom threshold

I need a custom threshold to the image, where is the value of the pixel is less than thr I need to leave the original value, but if the pixel is bigger than the thr then it should be the same value of the thr.

I check the threshold method in the opencv, but it give me back and white, I do not want this, I need the same what I explain above.

Thanks in advance.!

Upvotes: 2

Views: 1406

Answers (1)

Hazem Abdullah
Hazem Abdullah

Reputation: 1893

Opencv offer you some basic thresholding operations, We can effectuate 5 types of Thresholding operations:

Threshold Binary:

enter image description here

enter image description here

if the intensity of the pixel src(x,y) is higher than thresh, then the new pixel intensity is set to a MaxVal. Otherwise, the pixels are set to 0.

Threshold Binary, Inverted:

enter image description here

enter image description here

If the intensity of the pixel src(x,y) is higher than thresh, then the new pixel intensity is set to a 0. Otherwise, it is set to MaxVal.

Truncate:

enter image description here

enter image description here

The maximum intensity value for the pixels is thresh, if src(x,y) is greater, then its value is truncated.

Threshold to Zero:

enter image description here

enter image description here

If src(x,y) is lower than thresh, the new pixel value will be set to 0.

Threshold to Zero, Inverted:

enter image description here

enter image description here

If src(x,y) is greater than thresh, the new pixel value will be set to 0.

So you can do that using Truncated type, check this:

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

src – input array (single-channel, 8-bit or 32-bit floating point).
dst – output array of the same size and type as src.
thresh – threshold value.
maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
type – thresholding type (see the details below).

Example:

/*  threshold_type
     0: Binary
     1: Binary Inverted
     2: Threshold Truncated
     3: Threshold to Zero
     4: Threshold to Zero Inverted
   */
  threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );
//In your case threshold_type = 2

ref: 1 2

Upvotes: 5

Related Questions