图像阈值(threshold):图像分割的标尺

阈值类型

阈值二值化(threshold binary):
设定一个临界值,将像素值大于临界值的像素设置成255,低于临界值设置成0 
dst(x,y)={maxVal ifsrc(x,y)>thresh 
dst(x,y)={0 otherwise

阈值反二值化: 
设定一个临界值,将像素值大于临界值的像素设置成0,低于临界值设置成255。 
dst(x,y)={0 ifsrc(x,y)>thresh 
dst(x,y)={maxVal otherwise

截断: 
设定一个临界值,将像素值大于临界值的像素设置成临界值,低于临界值不变。 
dst(x,y)={threshhold ifsrc(x,y)>thresh 
dst(x,y)={src(x,y) otherwise

阈值取0: 
设定一个临界值,将像素值大于临界值的像素不变,低于临界值设置成0。 
dst(x,y)={src(x,y) ifsrc(x,y)>thresh 
dst(x,y)={0 otherwise

阈值反取0: 
设定一个临界值,将像素值大于临界值的像素设置成0,低于临界值不变。 
dst(x,y)={0 ifsrc(x,y)>thresh 
dst(x,y)={src(x,y) otherwise

代码:
 

#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <string> 
#include<fstream> 
using namespace cv;
using namespace std;

Mat src, gray_src, dst, dst1, dst2, dst3, dst4, dst5;
int threshold_value = 127;
int type_value = 1;
void getThresholdChange(int, void*) 
{
	cvtColor(src, gray_src, CV_RGB2GRAY);
	imshow("gray", gray_src);

	threshold(gray_src, dst1, threshold_value, 255, THRESH_BINARY);//阈值二值化
	imshow("THRESH_BINARY", dst1);

	threshold(gray_src, dst2, threshold_value, 255, THRESH_BINARY_INV);//阈值反二值化
	imshow("THRESH_BINARY_INV", dst2);

	threshold(gray_src, dst3, threshold_value, 255, THRESH_TRUNC);//截断
	imshow("THRESH_TRUNC", dst3);

	threshold(gray_src, dst4, threshold_value, 255, THRESH_TOZERO);//阈值取0
	imshow("THRESH_TOZERO", dst4);

	threshold(gray_src, dst5, threshold_value, 255, THRESH_TOZERO_INV);//阈值反取0
	imshow("THRESH_TOZERO_INV", dst5);

	threshold(gray_src, dst, threshold_value, 255, type_value);
	imshow("outputthreshold", dst);
}

int main()
{
	src = imread("Texture.jpg");
	imshow("input", src);
	getThresholdChange(0, 0);
	createTrackbar("阈值大小:", "outputthreshold", &threshold_value, 255, getThresholdChange);
	createTrackbar("type Value", "outputthreshold", &type_value, 5, getThresholdChange);
	waitKey(0);
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值