当前位置:网站首页>【OpenCV】—阈值化
【OpenCV】—阈值化
2022-07-24 18:08:00 【我菜就爱学】
什么是阈值化?
答:在对各种图形进行处理操作的过程中,需要对图像中的像素做出取舍与决策,直接剔除一些低于或者高于一定值的像素。阈值可以被视作最简单的图像分割方法。如:从一副图像中利用阈值分割出我们需要的物体部分。图像分割方法基于图像中物体与背景之间的灰度差异,其中分割属于像素级的分割。
用法:阈值的选取依赖于具体的问题。即物体在不同的图像中可能会有不同的灰度值。一旦找到需要分割的物体的像素点,可以对这些像素点设定一些特定的值来表示。
1、固定阈值操作:Threshold()函数
说明:函数Threshold()对单通道数组应用固定阈值操作。该函数的典型应用是对灰度值图像进行阈值操作得到二值图像,(compare()函数也可以达到此目的)或者是去掉噪声,例如过滤很小或很大像素值的图像点。
double threshold(InputArray src,OutputArray dst,double thresh,double maxval,int type)
第一个参数:输入数组,填单通道,8或者32位浮点类型的Mat
第二个参数:函数调用后运算结果存在这里,即这个参数用于存放输出结果,且和第一个参数中的Mat变量有一样的尺寸和类型
第三个参数:阈值的具体值
第四个参数:当第五个参数阈值类型type取CV_THRESH或CV_THRESH_BINARY_INV时阈值类型的最大值
第五个参数:阈值类型。
下面左边标识符依次取值0,1,2,3,4——一一对应右边的图形化的阈值描述

2、自适应阈值操作:adaptiveThreshold()函数
说明:自适应阈值操作是对矩阵采用自适应阈值操作,支持就地操作
void adaptiveThreshold(InputArray src,OutputArray dst,double maxValue,int adaptiveMethod,int thresholdType,int blockSize,double C)
- 第一个参数:输入图形,即源图像,填Mat类对象的即可,且需为8位单通道浮点型图形
- 第二个参数:函数调用后的运算结果存在这里,需要和源图像有一样的尺寸和类型
- 第三个参数:给像素赋的满足条件的非零值。
- 第四个参数:用于指定要使用的自适应阈值算法,可取值为ADAPTIVE_THRESH_MEAN_C或者ADAPTIVE_THRESH_GAUSSIAN_C
- 第五个参数:阈值类型。取值必须为THRESH_BINARY、THRESH_BINARY_INV其中之一
- 第六个参数:用于计算阈值大小的一个像素的邻域尺寸,取值3、5、7等
- 第七个参数:减去平均或加权平均值后的常数值。通常其为正数,但少数情况下也可以为0或者负数。
adaptiveThreshold()函数有如下将一幅灰度图变换为一幅二值图公式:
(1)当第五个参数“阈值类型”thresholdType取值为THRESH_BINARY时:

(2)当第五个参数取值为THRESH_BINARY_INV时:

(3)其中的T(x,y)为分别计算每个单独像素的阈值,取值如下:
- 对于ADAPTIVE_THRESH_MEAN_C方法,阈值T(x,y)为blockSize X blockSize邻域内(x,y)减去第七个参数C的平均值。
- 对于ADAPTIVE_THRESH_GAUSSIAN_C方法,阈值T(x,y)为blockSize X blockSize邻域内(x,y)减去第七个参数C与高斯窗交叉相关的加权总和。
3、示例程序
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#define WINDOW_NAME "【程序窗口】"
//--------------------------------
// 全局变量声明
//--------------------------------
int g_nThresholdValue = 100;
int g_nThresholdType = 3;
Mat g_srcImage, g_grayImage, g_dstImage;
//--------------------------------
// 全局函数声明
//--------------------------------
static void ShowHelpText();//输出帮助文字
void on_Threshold(int, void *);//回调函数
int main()
{
system("color 5E");
//读入源图片
g_srcImage = imread("E:\\Pec\\黑寡妇.jpg");
//存留一部分原图的灰度图
cvtColor(g_srcImage, g_grayImage, COLOR_RGB2GRAY);
//创建窗口并显示原始图
namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);
imshow("【原始图】", g_srcImage);
//创建滑动条来控制阈值
createTrackbar("模式", WINDOW_NAME, &g_nThresholdType, 4, on_Threshold);
createTrackbar("参数值", WINDOW_NAME, &g_nThresholdValue, 255, on_Threshold);
//初始化自定义的阈值回调函数
on_Threshold(0, 0);
//轮询等待用户按键,如果ESC按键按下则退出程序
while (1)
{
int key;
key = waitKey(20);
if ((char)key == 27)
break;
}
}
void on_Threshold(int, void*)
{
//调用阈值函数
threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType);
imshow(WINDOW_NAME, g_dstImage);
}
(1)原始图
(2)模式0:二进制阈值
(3)模式1:反二进制阈值
(4)模式2:截断阈值
(5)模式3:反阈值化为0
(6)模式4:阈值化为0
边栏推荐
- Just one dependency to give swagger a new skin, which is simple and cool!
- Dry goods | three sub domain name collection tools worth collecting
- Common questions of testers during interview
- Use of jumpserver
- C language programming training topics: K characters in left-handed string, little Lele and Euclidean, printing arrow pattern, civil servant interview, poplar matrix
- What are the pitfalls from single architecture to distributed architecture?
- Review and analysis of noodle dishes
- 颜色的13 个必备方法!
- 0612~quartz定时器框架
- 《STL源码剖析》应该怎样读?
猜你喜欢

面会菜评论分析

生信常用分析图形绘制02 -- 解锁火山图真谛!

Get the data of Tongcheng (elong) Hotel

Detailed explanation of ansible automatic operation and maintenance (V) the setting and use of variables in ansible, the use of jinja2 template and the encryption control of ansible

Shardingsphere database read / write separation

es(1)
![[leetcode] 30. Concatenate substrings of all words](/img/21/3965532a31553cfe6edf64ca5de3f4.png)
[leetcode] 30. Concatenate substrings of all words
去不图床容量兑换

手写博客平台~第二天

What are the pitfalls from single architecture to distributed architecture?
随机推荐
手写博客平台~第二天
[OBS] dependency Library: x264 vs Build
Go language file operation
(mandatory) override equals must override hashcode (principle analysis)
Inheritance and Derive
Simple test JS code
New can also create objects. Why do you need factory mode?
odoo中的bom理解
0612~quartz定时器框架
邻接表的定义和存储以及有向图无向图的邻接存储
0623~ holiday self study
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷!
T245982 「KDOI-01」醉花阴
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)
pinia 入门及使用
Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t
Model saving and loading of sklearn
SV强制类型转换和常数
700. 二叉搜索树中的搜索-dfs法