我需要图像的自定义阈值,其中像素的值小于thr我需要保留原始值,但如果像素大于 thr那么它应该与thr的值相同。
我检查了opencv中的阈值方法,但它给了我返回和白色,我不想要这个,我需要与上面解释的相同的内容。
提前致谢。!
Opencv为您提供一些基本的阈值操作,我们可以实现5种类型的阈值操作:
\n\n阈值二进制:
\n\n\n\n\n\n如果像素 src(x,y) 的强度高于 thresh,则将新像素强度设置为 MaxVal。否则,像素设置为 0。
\n\n阈值二进制,反转:
\n\n\n\n\n\n如果像素 src(x,y) 的强度高于 thresh,则新像素强度设置为 0。否则,设置为 MaxVal。
\n\n截短:
\n\n\n\n\n\n像素的最大强度值为 thresh,如果 src(x,y) 较大,则其值被截断。
\n\n阈值为零:
\n\n\n\n\n\n如果 src(x,y) 低于 thresh,则新像素值将设置为 0。
\n\n阈值为零,反转:
\n\n\n\n\n\n如果 src(x,y) 大于 thresh,则新像素值将设置为 0。
\n\n所以你可以使用Truncatedtype 来做到这一点,检查一下:
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)\n\nsrc \xe2\x80\x93 input array (single-channel, 8-bit or 32-bit floating point).\ndst \xe2\x80\x93 output array of the same size and type as src.\nthresh \xe2\x80\x93 threshold value.\nmaxval \xe2\x80\x93 maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.\ntype \xe2\x80\x93 thresholding type (see the details below).\nRun Code Online (Sandbox Code Playgroud)\n\n例子:
\n\n/* threshold_type\n 0: Binary\n 1: Binary Inverted\n 2: Threshold Truncated\n 3: Threshold to Zero\n 4: Threshold to Zero Inverted\n */\n threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );\n//In your case threshold_type = 2\nRun Code Online (Sandbox Code Playgroud)\n\n\n