阈值二值化:
阈值反二值化:
阈值截断:
阈值取零:
#include<opencv2opencv.hpp> #include<iostream> #include<math.h> using namespace std; using namespace cv; Mat src, dst; int threshold_demo = 127; int max_threshold = 255; int threshold_type = 0;//0:阈值二值化, 1:阈值反二值化, 2:阈值阶段, 3:阈值取零, 4阈值反取零; int max_type = 4; void demo_threshold(int, void*); int main(int argc, char** argv) { src = imread("D:/OpenCVprj/image/test1.jpg"); if (!src.data) { printf("Could not load image... "); return -1; } imshow("src", src); namedWindow("output image", CV_WINDOW_AUTOSIZE); createTrackbar("threshold", "output image", &threshold_demo, max_threshold, demo_threshold); createTrackbar("threshold_type", "output image", &threshold_type, max_type, demo_threshold); demo_threshold(0, 0); waitKey(0); return 0; } void demo_threshold(int, void*) { Mat temp; if (src.channels() == 3) { cvtColor(src, temp, CV_BGR2GRAY); } else { temp = src; } threshold(temp, dst, threshold_demo, max_threshold, threshold_type); imshow("output image", dst); return; }