当前位置:网站首页>【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
边栏推荐
- 继承与派生
- Has polardb for PostgreSQL entered the list of Xinchuang database?
- Section 10 cache breakdown follow Daewoo redis ------- directory post
- SV强制类型转换和常数
- Goodbye Navicat! This open source database management tool has a cooler interface!
- Wu Enda writes: how to establish projects to adapt to AI career
- 1688/阿里巴巴按关键字搜索新品数据 API 使用说明
- 再见收费的Navicat!这款开源的数据库管理工具界面更炫酷!
- How to follow the "low coupling" design principle?
- Install jumpserver
猜你喜欢
![[network security] analysis vulnerability of website Middleware](/img/3a/9c034c17d65348aa7c35a3dac2039c.png)
[network security] analysis vulnerability of website Middleware

PXE高效批量网络装机

PXE efficient batch network installation

The use and Simulation of character and string library functions in C language

Common methods of number and math classes

Inherit, override, overload

Install jumpserver

分家后印象笔记过日子依然不好过,骚操作却不少

Win10 super good-looking mouse theme, you also try it

Inheritance and Derive
随机推荐
ShardingSphere数据库读写分离
0621~ES&Lucene
0614~放假自习
Common questions of testers during interview
Laravel notes - RSA encryption of user login password (improve system security)
Review and analysis of noodle dishes
0630~ professional quality course
1688/阿里巴巴按关键字搜索新品数据 API 使用说明
new也可以创建对象,为什么需要工厂模式?
C language custom type explanation - Consortium
ROC and AUC details of the recommended system
猜JWT关键字
Alibaba /1688 API instructions for searching products by map (pailitao)
2.3.1 view drawing process
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷!
C language custom type explanation - structure
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
Use of jumpserver
Laravel笔记-用户登录时密码进行RSA加密(提高系统安全性)
Interview assault 66: what is the difference between request forwarding and request redirection?