ubuntu22.04@laptop OpenCV Get Started: 009_image_thresholding

ubuntu22.04@laptop OpenCV Get Started: 009_image_thresholding

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

0 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.png

0 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) > thresh
  dst(x,y) = maxValue
else
  dst(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) > thresh
  dst(x,y) = 0
else
  dst(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) > thresh
  dst(x,y) = thresh
else
  dst(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) > thresh
  dst(x,y) = src(x,y)
else
  dst(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) > thresh
  dst(x,y) = 0
else
  dst(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》对图像进行卷积的计算机操作,从而对数据进行有效性过滤。

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

  • 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》开始,一个章节一个章节的了解,循序渐进。

相关推荐
零号机几秒前
使用TRAE 30分钟极速开发一款划词中英互译浏览器插件
前端·人工智能
FunTester2 分钟前
基于 Cursor 的智能测试用例生成系统 - 项目介绍与实施指南
人工智能·ai·大模型·测试用例·实践指南·curor·智能测试用例
SEO_juper8 分钟前
LLMs.txt 创建指南:为大型语言模型优化您的网站
人工智能·ai·语言模型·自然语言处理·数字营销
淮雵的Blog23 分钟前
langGraph通俗易懂的解释、langGraph和使用API直接调用LLM的区别
人工智能
Mintopia26 分钟前
🚀 共绩算力:3分钟拥有自己的文生图AI服务-容器化部署 StableDiffusion1.5-WebUI 应用
前端·人工智能·aigc
HPC_C33 分钟前
SGLang: Efficient Execution of Structured Language Model Programs
人工智能·语言模型·自然语言处理
王哈哈^_^41 分钟前
【完整源码+数据集】草莓数据集,yolov8草莓成熟度检测数据集 3207 张,草莓成熟度数据集,目标检测草莓识别算法系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
songyuc1 小时前
《A Bilateral CFAR Algorithm for Ship Detection in SAR Images》译读笔记
人工智能·笔记·计算机视觉
码界奇点1 小时前
解密AI语言模型从原理到应用的全景解析
人工智能·语言模型·自然语言处理·架构
余衫马1 小时前
你好,未来:零基础看懂大语言模型
人工智能·语言模型·自然语言处理·智能体