博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

OpenCV (十二)阈值操作

阈值二值化:

                

 

阈值反二值化:

            

 

 

阈值截断:

            

 

 阈值取零:

           

 

 

#include<opencv2\opencv.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...\n");
		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;
}

  

 

posted @ 2019-07-24 17:28  haibochina  阅读(379)  评论(0编辑  收藏  举报