|
图像阈值
阈值 是什么?简单点说是把图像分割的标尺,这个标尺是根据什么产生的,阈值产生算法?阈值类型。(Binary segmentation)
阈值类型
     
阈值类型一阈值二值化(threshold binary)
  
阈值类型一阈值反二值化(threshold binary Inverted)
  
阈值类型一截断 (truncate)
  
阈值类型一阈值取零 (threshold to zero)
  
阈值类型一阈值反取零 (threshold to zero inverted)
  

[C++] 纯文本查看 复制代码 /*
注意:
THRESH_BINARY = 0, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{maxval}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
THRESH_BINARY_INV = 1, //!< \f[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{maxval}}{otherwise}\f]
THRESH_TRUNC = 2, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{threshold}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
THRESH_TOZERO = 3, //!< \f[\texttt{dst} (x,y) = \fork{\texttt{src}(x,y)}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
THRESH_TOZERO_INV = 4, //!< \f[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
THRESH_MASK = 7,
THRESH_OTSU = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
THRESH_TRIANGLE = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat src, gray_src, dst;
//阈值二值化的模式0-4 参考注意点,默认设为0
int threshold_mod=0;
//阈值二值化最大值,一共5种模式0-4
int threshold_mod_max=4;
//--------------------------------------
//设定阈值
int threshold_value = 127;
//设定最大阈值
int threshold_max = 255;
// 定义输出窗口标题
char output_title[] = "输出窗口";
// -----------------------------
//声明函数
void Thershold_Demo(int, void*);
int main(int argc, char** argv) {
src = imread("D:/IDE/opencv-3.1.0/demo.jpg");
if (!src.data) {
printf("加载图片异常\n");
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
imshow("input", src);
createTrackbar("拖动条:",output_title, &threshold_value, threshold_max, Thershold_Demo);
//创建效果拖动条
createTrackbar("模式:", output_title, &threshold_mod, threshold_mod_max, Thershold_Demo);
//阈值二值化的模式的变化
Thershold_Demo(threshold_value, 0);
waitKey(0);
return 0;
}
void Thershold_Demo(int val, void* ) {
switch (threshold_mod)
{
case 0: cout << "THRESH_BINARY模式(阈值二值化)" << endl; break;
case 1: cout << "THRESH_BINARY_INV模式(阈值反二值化)" << endl; break;
case 2: cout << "THRESH_TRUNC模式(阈值截断)" << endl; break;
case 3: cout << "THRESH_TOZERO模式(阈值取零)" << endl; break;
case 4: cout << "THRESH_TOZERO_INV模式(阈值反取零)" << endl; break;
default:
break;
}
printf("调整的数值为:%d\n", val);
//转灰度图
cvtColor(src, gray_src, CV_BGR2GRAY);
//阈值的处理
threshold(gray_src, dst, threshold_value, threshold_max, threshold_mod);
imshow(output_title, dst);
}

自动寻找阈值:
THRESH_OTSU
THRESH_TRIANGLE
[C++] 纯文本查看 复制代码 /*
注意:
THRESH_OTSU THRESH_TRIANGLE处理的图像必须是8位单通道(灰度图)
自动寻找阈值
THRESH_OTSU
THRESH_TRIANGLE
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//声明函数
void demo(int, void*);
//定义滑块条最小值
int val = 0;
Mat src, gray_src,dst;
int main(int argc, char** argv) {
src = imread("D:/IDE/opencv-3.1.0/demo.jpg");
if (!src.data) {
printf("加载图片异常\n");
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
namedWindow("CV_demo", CV_WINDOW_AUTOSIZE);
//创建滑块条
createTrackbar("模式调整:", "CV_demo",&val,4, demo);
demo(0, 0);
imshow("input", src);
waitKey(0);
return 0;
}
void demo(int, void*){
//转灰度图
cvtColor(src, gray_src, CV_BGR2GRAY);
//通过THRESH_OTSU 自动获取阈值,处理效果
//threshold(gray_src, dst, 0, 255, THRESH_OTSU|val);
//通过THRESH_TRIANGLE 自动获取阈值,处理效果
threshold(gray_src, dst, 0, 255, THRESH_TRIANGLE | val);
imshow("CV_demo", dst);
}
|
|