阈值分割
C++閾值分割
1、閾值分割
- 5種類型的閾值分割如下:
第一種:閾值二值化
藍色線表示閾值
原理:像素值大于閾值時,為最大值,小于閾值則為0。
第二種:閾值反二值化
原理:像素值大于閾值時,為0,否則為設(shè)定的最大值。
第三種:截斷
原理:像素值大于閾值時,為設(shè)定的閾值,否則為像素值本身。
第四種:閾值取零
原理:像素值大于閾值時,為像素值,否則為0.
第五種:閾值反取零
原理:像素值大于閾值時,為0,否則為像素值本身。
2、代碼實現(xiàn)
#include <opencv2/opencv.hpp> #include <iostream>using namespace std; using namespace cv;void CallbackFunc(int, void*);Mat img, gray, dst; int threshold_num = 2; int element = 127; int max_size = 255; int threshlod_classes = 4; void CallbackFunc(int, void*);int main(int argc, char** argv) {img = imread("G:/testpic/img.png");if (!img.data){printf("cannot load the image");return -1;}namedWindow("input", WINDOW_AUTOSIZE);imshow("input", img);namedWindow("output", WINDOW_AUTOSIZE);createTrackbar("thresh type:", "output", &threshold_num, threshold_classes, CallbackFunc);createTrackbar("num:", "output", &element, max_size, CallbackFunc);waitKey(0);return 0; }void CallbackFunc(int, void*) {cvtColor(img, gray, COLOR_BGR2GRAY);//閾值分割threshold(gray, dst, element, max_size, threshold_num);imshow("output", dst);return; }
總結(jié)
- 上一篇: 迭代阈值分割
- 下一篇: 【信号检测】基于隐马尔可夫模型HMM算法