opencv固定化阈值

来源:互联网 发布:什么叫网络编程 编辑:程序博客网 时间:2024/06/06 23:54
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. }
原创粉丝点击