opencv固定化阈值

opencv提供了固定化函数threshold,该函数有5中阈值化类型参数可以选择。
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
函数解析:
实现图像阈值化操作。参数src表示源图像数组(单通道,8为或者32位浮点数);参数dst表示输出图像组(与输入图像相同的尺寸和类型);thresh表示阈值设置;maxval表示预示最大值,使用THRESH_BINARY或者 THRESH_BINARY_INV类型;type表示阈值化处理的类型设置。
Threshold函数应用在单通道图像中固定阈值处理,通常是为了得到二值化灰度图像(只包含0或者1灰度值)或者为了去除噪声。
代码如下:
   
   
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include "opencv2/opencv.hpp"
  4. #include "opencv2/core/core.hpp"
  5. #include <stdio.h>
  6. #include <string>
  7. using namespace std;
  8. using namespace cv;
  9. int main()
  10. {
  11. cv::Mat srcImage = cv::imread("C:\\Users\\LP\\Desktop\\C++\\ConsoleApplication4\\ConsoleApplication4\\RGBFlower4.jpg");
  12. if (srcImage.empty())
  13. {
  14. return -1;
  15. }
  16. cv::imshow("原图像", srcImage);
  17. //灰度转换
  18. cv::Mat srcGray;
  19. cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY);
  20. cv::imshow("srcGray", srcGray);
  21. cv::Mat dstImage;
  22. //初始化阈值
  23. int thresh = 130;
  24. //0:二进制阈值,1:反二进制阈值,2:截断阈值,3:0阈值,4:反0阈值
  25. int threshType = 0;
  26. //预设最大值
  27. const int maxVal = 255;
  28. //固定阈值化操作
  29. cv::threshold(srcImage, dstImage, thresh, maxVal, threshType);
  30. cv::imshow("dstImage", dstImage);
  31. cv::waitKey(0);
  32. return 0;
  33. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值