ubuntu22.04@laptop OpenCV Get Started: 009_image_thresholding

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

相关推荐

  1. Ubuntu2204配置samba

    2024-02-16 08:18:01       75 阅读
  2. 《青少年成长管理2024009 “成长需要成本”

    2024-02-16 08:18:01       37 阅读
  3. ubuntu22.04@laptop OpenCV Get Started: 003_image_resizing

    2024-02-16 08:18:01       60 阅读
  4. ubuntu22.04@laptop OpenCV Get Started: 004_cropping_image

    2024-02-16 08:18:01       56 阅读
  5. ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images

    2024-02-16 08:18:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 08:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 08:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 08:18:01       82 阅读
  4. Python语言-面向对象

    2024-02-16 08:18:01       91 阅读

热门阅读

  1. Go语言开发小技巧&易错点100例(十二)

    2024-02-16 08:18:01       58 阅读
  2. B3638 T1 三角形面积

    2024-02-16 08:18:01       51 阅读
  3. 蓝桥杯(Web大学组)2022省赛真题:展开你的扇子

    2024-02-16 08:18:01       53 阅读
  4. C语言系列6——指针:C语言的精髓之一

    2024-02-16 08:18:01       51 阅读
  5. C++ STL:list和vector的比较

    2024-02-16 08:18:01       59 阅读
  6. 【动态规划初识】整数划分

    2024-02-16 08:18:01       53 阅读
  7. 【数据统计】A股分红率排行榜2023

    2024-02-16 08:18:01       159 阅读
  8. 企业微信自动推送机器人的应用与价值

    2024-02-16 08:18:01       61 阅读