我需要为图像设置一个自定义阈值,其中像素的值小于 thr ,我需要保留原始值,但是如果像素大于 thr ,则它应该与和。
我在opencv中检查了阈值方法,但是它使我反感,我不想要这个,我需要与上面解释的相同。
提前致谢。!
最佳答案
Opencv为您提供一些基本的阈值操作,我们可以完成5种类型的阈值操作:
阈值二进制文件:
如果像素src(x,y)的强度高于阈值,则将新像素强度设置为MaxVal。否则,像素设置为0。
二进制阈值,取反:
如果像素src(x,y)的强度高于阈值,则将新像素强度设置为0。否则,将其设置为MaxVal。
截断:
像素的最大强度值是阈值,如果src(x,y)较大,则其值将被截断。
阈值零:
如果src(x,y)小于thresh,则新像素值将设置为0。
阈值为零,取反:
如果src(x,y)大于thresh,则新像素值将设置为0。
因此,您可以使用Truncated
类型来执行,请检查以下内容:
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).
例:
/* 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
引用:1 2
关于c++ - C++ OpenCV自定义阈值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34150568/