阈值类型
阈值二值化(threshold binary):
设定一个临界值,将像素值大于临界值的像素设置成255,低于临界值设置成0
dst(x,y)={maxVal ifsrc(x,y)>thresh
dst(x,y)={0 otherwise
阈值反二值化:
设定一个临界值,将像素值大于临界值的像素设置成0,低于临界值设置成255。
dst(x,y)={0 ifsrc(x,y)>thresh
dst(x,y)={maxVal otherwise
截断:
设定一个临界值,将像素值大于临界值的像素设置成临界值,低于临界值不变。
dst(x,y)={threshhold ifsrc(x,y)>thresh
dst(x,y)={src(x,y) otherwise
阈值取0:
设定一个临界值,将像素值大于临界值的像素不变,低于临界值设置成0。
dst(x,y)={src(x,y) ifsrc(x,y)>thresh
dst(x,y)={0 otherwise
阈值反取0:
设定一个临界值,将像素值大于临界值的像素设置成0,低于临界值不变。
dst(x,y)={0 ifsrc(x,y)>thresh
dst(x,y)={src(x,y) otherwise
代码:
#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <string>
#include<fstream>
using namespace cv;
using namespace std;
Mat src, gray_src, dst, dst1, dst2, dst3, dst4, dst5;
int threshold_value = 127;
int type_value = 1;
void getThresholdChange(int, void*)
{
cvtColor(src, gray_src, CV_RGB2GRAY);
imshow("gray", gray_src);
threshold(gray_src, dst1, threshold_value, 255, THRESH_BINARY);//阈值二值化
imshow("THRESH_BINARY", dst1);
threshold(gray_src, dst2, threshold_value, 255, THRESH_BINARY_INV);//阈值反二值化
imshow("THRESH_BINARY_INV", dst2);
threshold(gray_src, dst3, threshold_value, 255, THRESH_TRUNC);//截断
imshow("THRESH_TRUNC", dst3);
threshold(gray_src, dst4, threshold_value, 255, THRESH_TOZERO);//阈值取0
imshow("THRESH_TOZERO", dst4);
threshold(gray_src, dst5, threshold_value, 255, THRESH_TOZERO_INV);//阈值反取0
imshow("THRESH_TOZERO_INV", dst5);
threshold(gray_src, dst, threshold_value, 255, type_value);
imshow("outputthreshold", dst);
}
int main()
{
src = imread("Texture.jpg");
imshow("input", src);
getThresholdChange(0, 0);
createTrackbar("阈值大小:", "outputthreshold", &threshold_value, 255, getThresholdChange);
createTrackbar("type Value", "outputthreshold", &type_value, 5, getThresholdChange);
waitKey(0);
}