当前位置: 首页 > news >正文

ubuntu22.04@laptop OpenCV Get Started: 009_image_thresholding

ubuntu22.04@laptop OpenCV Get Started: 009_image_thresholding

  • 1. 源由
  • 2. image_thresholding应用Demo
    • 2.1 C++应用Demo
    • 2.2 Python应用Demo
  • 3. 重点分析
    • 3.1 Binary Thresholding ( THRESH_BINARY )
    • 3.2 Inverse-Binary Thresholding ( THRESH_BINARY_INV )
    • 3.3 Truncate Thresholding ( THRESH_TRUNC )
    • 3.4 Threshold to Zero ( THRESH_TOZERO )
    • 3.5 Inverted Threshold to Zero ( THRESH_TOZERO_INV )
  • 4. 总结
  • 5. 参考资料
  • 6. 补充

1. 源由

阈值过滤也是OpenCV图像最基本的操作之一。

其主要方法就是:

  1. 通过一个阈值(阈值)来判断数据的有效性
  2. 通过加强对比度来让肉眼更易识别图像

比如:一张灰度图上,当灰度相近似的时候,肉眼其实很难判断出来。但是通过阈值判断和加强,就可以非常容易的让肉眼轻易识别图形。

2. image_thresholding应用Demo

009_image_thresholding是OpenCV通过阈值对图像过滤的示例程序。

2.1 C++应用Demo

C++应用Demo工程结构:

009_image_thresholding/CPP$ tree .
.
├── CMakeLists.txt
├── image_threshold.cpp
└── threshold.png0 directories, 3 files

确认OpenCV安装路径:

$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

C++应用Demo工程编译执行:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_threshold

2.2 Python应用Demo

Python应用Demo工程结构:

009_image_thresholding/Python$ tree .
.
├── image_threshold.py
├── requirements.txt
└── threshold.png0 directories, 3 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python image_threshold.py

3. 重点分析

在这里插入图片描述

3.1 Binary Thresholding ( THRESH_BINARY )

过滤规则:阈值两端极化操作

# Binary Threshold
if src(x,y) > threshdst(x,y) = maxValue
elsedst(x,y) = 0

在这里插入图片描述

C++:

// Thresholding with threshold value set 127 
threshold(src,dst,127,255, THRESH_BINARY); 

Python:

# Thresholding with threshold value set 127 
th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY) 

3.2 Inverse-Binary Thresholding ( THRESH_BINARY_INV )

过滤规则:阈值两端反向极化操作

# Inverse Binary Threshold
if src(x,y) > threshdst(x,y) = 0
elsedst(x,y) = maxValue

在这里插入图片描述

C++:

// Thresholding using THRESH_BINARY_INV 
threshold(src,dst,127,255, THRESH_BINARY_INV); 

Python:

# Thresholding using THRESH_BINARY_INV 
th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY_INV) 

3.3 Truncate Thresholding ( THRESH_TRUNC )

过滤规则:超过阈值截断操作

# Truncate Threshold
if src(x,y) > threshdst(x,y) = thresh
elsedst(x,y) = src(x,y)

在这里插入图片描述

C++:

// Thresholding using THRESH_TRUNC 
threshold(src,dst,127,255, THRESH_TRUNC); 

Python:

# Thresholding using THRESH_TRUNC 
th, dst = cv2.threshold(src,127,255, cv2.THRESH_TRUNC) 

3.4 Threshold to Zero ( THRESH_TOZERO )

过滤规则:低于阈值归零

# Threshold to Zero
if src(x,y) > threshdst(x,y) = src(x,y)
elsedst(x,y) = 0

在这里插入图片描述

C++:

// Thresholding using THRESH_TOZERO 
threshold(src,dst,127,255, THRESH_TOZERO); 

Python:

# Thresholding using THRESH_TOZERO 
th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO) 

3.5 Inverted Threshold to Zero ( THRESH_TOZERO_INV )

过滤规则:超过阈值归零

# Inverted Threshold to Zero
if src(x,y) > threshdst(x,y) = 0
elsedst(x,y) = src(x,y)

在这里插入图片描述

C++:

// Thresholding using THRESH_TOZERO_INV 
threshold(src,dst,127,255, THRESH_TOZERO_INV); 

Python:

# Thresholding using THRESH_TOZERO_INV 
th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO_INV) 

4. 总结

前面《ubuntu22.04@laptop OpenCV Get Started: 008_image_filtering_using_convolution》对图像进行卷积的计算机操作,从而对数据进行有效性过滤。

本文通过对图像进行阈值的计算机操作,从而对数据进行有效性过滤,在特定的场景下,依然能够实现很好的图像数据分析作用。

  • threshold(src,dst,thresh,maxval, type))
  • src Source array (single-channel).
  • dst Destination array with the same size and type as src .
  • thresh Threshold value.
  • maxval Maximum value to use with THRESH_BINARY and THRESH_BINARY_INV threshold types.
  • type Threshold type. For details, see threshold . The THRESH_MASK, THRESH_OTSU and THRESH_TRIANGLE threshold types are not supported.

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

6. 补充

学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。

有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。

http://www.lryc.cn/news/300241.html

相关文章:

  • Zeek实战—快速构建流量安全能力
  • vim命令编辑完文件后,按ESC键退出编辑模式,无法进入命令模式解决方案
  • 【生产实测有效】Linux磁盘清理常用命令
  • 练习:鼠标类设计之1_类内容解析
  • 消息队列RabbitMQ-使用过程中面临的问题与解决思路
  • 搜索Agent方案
  • 排序算法---计数排序
  • STM32——LCD(1)认识
  • iTop-4412 裸机程序(二十二)- RTC时钟
  • Kafka 之 AdminClient API
  • Flutter run 一直 Running Gradle task ‘assembleDebug’…
  • kali无线渗透之用wps加密模式破解出wpa模式的密码12
  • 【Python】高级数据类型
  • 挑战杯 python区块链实现 - proof of work工作量证明共识算法
  • 如何给最小化安装的CentOS主机装个远程桌面?
  • 知识图谱:py2neo将csv文件导入neo4j
  • 备战蓝桥杯---图论之最短路Bellman-Ford算法及优化
  • C++ //练习 5.19 编写一段程序,使用do while循环重复地执行下述任务:首先提示用户输入两个string对象,然后挑出较短的那个并输出它。
  • 算法刷题:有效三角形个数
  • python---变量
  • 数据库第二次实验
  • 容器高级知识:Kubernetes Pod 适配器模式详解
  • 云原生容器化-5 Docker常见操作命令
  • 几道简单的题目练一下手感
  • 2023年哪个前端框架用的最多?
  • 基于BitVM的乐观 BTC bridge
  • 谷歌浏览器安装扩展程序axure-chrome-extension
  • C++学习:大小写转换
  • 【王道数据结构】【chapter5树与二叉树】【P159t16】
  • 代码随想录算法训练营第51天 | 139.单词拆分 多重背包理论基础