一、图像阈值化简介
二、固定阈值
三、自适应阈值
#include<opencv2/opencv.hpp>
using namespace cv;
void main(){
Mat src=imread("E://1.jpg",);//以灰度模式读入
Mat dst;
//threshold(src,dst,100,255,CV_THRESH_BINARY);
//adaptiveThreshold(src,dst,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,11,5);
adaptiveThreshold(src,dst,,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,,);
imshow("src",src);
imshow("dst",dst);
waitKey();
}
四、滚动条调整参数
#include<opencv2/opencv.hpp>
using namespace cv;
Mat src,dst,dst2;
int thres_value=,block_size=,c=;
void onThreshold(int ,void*){
threshold(src,dst,thres_value,,CV_THRESH_BINARY);
imshow("固定阈值",dst);
}
void onAdaptiveThreshold(int ,void *){
if(block_size%==) block_size++;//如果block_size是偶数
adaptiveThreshold(src,dst2,,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,block_size,c);
imshow("自适应阈值",dst2);
}
void main(){
src=imread("E://1.jpg",);//以灰度模式读入
namedWindow("固定阈值",CV_WINDOW_AUTOSIZE);
namedWindow("自适应阈值",CV_WINDOW_AUTOSIZE);
createTrackbar("Threshold", "固定阈值",&thres_value,,onThreshold,);
createTrackbar("Block_size", "自适应阈值",&block_size,,onAdaptiveThreshold,);
createTrackbar("C", "自适应阈值",&c,,onAdaptiveThreshold,);
onThreshold(thres_value,); //回调函数初始化
onAdaptiveThreshold(block_size,);
onAdaptiveThreshold(c,);
imshow("src",src);
waitKey();
}