图像阈值
阈值 是什么?简单点说是把图像分割的标尺,这个标尺是根据什么产生的,阈值产生算法?阈值类型。(Binary segmentation)
阈值类型
阈值二值化
左下方的图表示图像像素点Src(x,y)值分布情况,蓝色水平线表示阈值
阈值反二值化
左下方的图表示图像像素点Src(x,y)值分布情况,蓝色水平线表示阈值
截断
左下方的图表示图像像素点Src(x,y)值分布情况,蓝色水平线表示阈值
阈值取零
左下方的图表示图像像素点Src(x,y)值分布情况,蓝色水平线表示阈值
阈值反取零
左下方的图表示图像像素点Src(x,y)值分布情况,蓝色水平线表示阈值
代码演示
//****************Cuda
#include "opencv2/cudaarithm.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/cudafeatures2d.hpp"
#include "opencv2/cudaobjdetect.hpp"
#include "opencv2/cudafilters.hpp"
//***************OpenCV
#include <fstream>
#include <string>
#include<iostream>
#include "opencv2/opencv_modules.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/timelapsers.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
// #include <opencv2/nofree/nofree.hpp>
#include<opencv2/xfeatures2d.hpp>
#include "ctime"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::detail;
bool readCamera(const string& filename, Mat& cameraMatrix, Mat& distCoeffs, float& ratio);
Mat src, dst, gray_src;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char* output_title = "binary image";
void Threshold_Demo(int, void*);
int main(int argc, char** argv) { //argc 表示命令行输入参数的个数(以空白符分隔),argv中存储了所有的命令行参数
src = imread("G:\\data1\\pic.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
imshow("input image", src);
createTrackbar("Threshold value", output_title, &threshold_value, threshold_max, Threshold_Demo); //创建跟踪条,命名,窗口名,调整的值,最大值,控制的功能函数
createTrackbar("Type Value", output_title, &type_value, type_max, Threshold_Demo); //跟踪条调整阈值类型
Threshold_Demo(0, 0);
/*
printf("%d", THRESH_BINARY); //0,二值化
printf("%d", THRESH_BINARY_INV); //1,反二值化
printf("%d", THRESH_TRUNC); //2,截断
printf("%d", THRESH_TOZERO); //3,取零
printf("%d\n", THRESH_TOZERO_INV); //4,反取零
*/
waitKey(0);
return 0;
}
/*阈值操作*/
void Threshold_Demo(int, void*) {
cvtColor(src, gray_src, CV_BGR2GRAY);
// threshold(gray_src, dst, threshold_value, threshold_max, THRESH_BINARY); //阈值二值化
// threshold(gray_src, dst, threshold_value, threshold_max, type_value); //原图,目标图,已知阈值,阈值最大值,阈值类型
threshold(gray_src, dst, 0, 255, THRESH_OTSU | type_value); //自动计算阈值,忽略输入的阈值
// threshold(gray_src, dst, 0, 255, THRESH_TRIANGLE | type_value); //自动计算三角阈值,忽略输入的阈值
imshow(output_title, dst);
}