OpenCV-C++ 图像基本阈值操作
2021/4/11 22:25:11
本文主要是介绍OpenCV-C++ 图像基本阈值操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
阈值类型
阈值产生的算法,阈值类型
THRESH_BINARY
表示大于thresh
的取maxval
,否则取0;THRESH_BINARY_INV
表示大于thresh
的取0,否则取maxvalue
;THRESH_TRUNC
表示大于thresh
取threshold
,否则不改变灰度值;THRESH_TOZERO
表示大于thresh
的不改变灰度值,否则取0;THRESH_TOZERO_INV
表示大于thresh
取0,窦泽不改变灰度值;THRESH_OTSU
表示使用otsu
自动计算阈值;THRESH_TRIANGLE
表示使用Triangle
自动计算阈值;
代码示例如下:
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; char output_title[] = "output window"; int threshold_value = 127; const int MAX_THRESHOLD = 255; int threshold_type = 2; const int MAX_THRESHOLD_TYPE = 4; void Threshold_Demo(int, void*); Mat src, srcGray, dst; int main(){ // 读取图像 src = imread("/home/chen/dataset/lena.jpg"); if (src.empty()){ cout << "could not load image." << endl; return -1; } namedWindow("src", WINDOW_AUTOSIZE); imshow("src", src); // 创建 namedWindow(output_title, WINDOW_AUTOSIZE); createTrackbar("Threshold: ", output_title, &threshold_value, MAX_THRESHOLD, Threshold_Demo); createTrackbar("Type: ", output_title, &threshold_type, MAX_THRESHOLD_TYPE, Threshold_Demo); Threshold_Demo(0, 0); waitKey(0); return 0; } void Threshold_Demo(int, void*){ cvtColor(src, srcGray, COLOR_BGR2GRAY); // printf("%d", THRESH_BINARY); // 0 // printf("%d", THRESH_BINARY_INV); // 1 // printf("%d", THRESH_TRUNC); // 2 // printf("%d", THRESH_TOZERO); // 3 // printf("%d", THRESH_TOZERO_INV); // 4 threshold(srcGray, dst, threshold_value, MAX_THRESHOLD, threshold_type); // threshold(srcGray, dst, 0, 255, THRESH_OTSU | threshold_type); // 自动计算阈值 // threshold(srcGray, dst, 0, 255, THRESH_TRIANGLE | threshold_type); // 自动计算阈值 imshow(output_title, dst); }
这篇关于OpenCV-C++ 图像基本阈值操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-09cosmos 开发是什么-icode9专业技术文章分享
- 2024-07-09Cosmos 示例-icode9专业技术文章分享
- 2024-07-09安装Cosmos SDK-icode9专业技术文章分享
- 2024-07-09ubuntu 安装 cosmos-icode9专业技术文章分享
- 2024-07-09cosmos 开发能做到跨链吗-icode9专业技术文章分享
- 2024-07-09app 未公示个人信息的收集范围 的解决办法-icode9专业技术文章分享
- 2024-07-08测试人员在 Scrum 中的角色是什么?
- 2024-07-07Dify + TiDB Vector,快速构建你的AI Agent
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享