0
点赞
收藏
分享

微信扫一扫

OpenCV + CPP 系列(十六)图像阈值 threshold(二值化)


文章目录

  • ​​一、介绍​​
  • ​​二、演示效果​​


阈值:简单点说是把图像分割的标尺。

一、介绍


double threshold(


InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type     阈值类型如下
);


阈值类型:

THRESH_BINARY
THRESH_BINARY_INV



THRESH_TOZERO
THRESH_TOZERO_INV



THRESH_TRUNC
THRESH_MASK



THRESH_OTUS  大津阈值算法配合其它阈值使用,最适用于双波峰
THRESH_TRIANGLE  三角形的二值化法根据直方图自行计算阈值,最适用于单个波峰,最开始用于医学分割细胞等

OpenCV + CPP 系列(十六)图像阈值 threshold(二值化)_opencv


头文件 quick_opencv.h:声明类与公共函数

#pragma once
#include <opencv2\opencv.hpp>
using namespace cv;

class QuickDemo {
public:
...
void thresh_Demo(Mat& image1);

};

主函数调用该类的公共成员函数

#include <opencv2\opencv.hpp>
#include <quick_opencv.h>
#include <iostream>
using namespace cv;


int main(int argc, char** argv) {
Mat src = imread("D:\\Desktop\\jianbian.png");
if (src.empty()) {
printf("Could not load images...\n");
return -1;
}
QuickDemo qk;
qk.thresh_Demo(src1);
waitKey(0);
destroyAllWindows();
return 0;
}

二、演示效果

源文件 quick_demo.cpp:实现类与公共函数

void QuickDemo::thresh_Demo(Mat& image) {
int width = image.cols;
cvtColor(image, image, COLOR_BGR2GRAY);
Mat dst0, dst1, dst2;
threshold(image, dst0, 127, 255, THRESH_TRUNC);
threshold(image, dst1, 127, 255, THRESH_OTSU);
threshold(image, dst2, 127, 255, THRESH_OTSU|THRESH_BINARY_INV);


Mat horiz, show_image;
Mat horiz_line = Mat::zeros(30, width * 2, CV_8UC1);
hconcat(dst0, dst1, horiz); // 水平方向拼接
vconcat(horiz, horiz_line, show_image); // 竖直方向拼接

putText(show_image, "THRESH_TRUNC", Point(30, 212), FONT_HERSHEY_COMPLEX, 0.5, Scalar(255), 1, 8, false);
putText(show_image, "THRESH_OTSU", Point(200, 212), FONT_HERSHEY_COMPLEX, 0.5, Scalar(255), 1, 8, false);
imshow("show_image", show_image);
}

OpenCV + CPP 系列(十六)图像阈值 threshold(二值化)_scala_02


OpenCV + CPP 系列(十六)图像阈值 threshold(二值化)_#include_03


举报

相关推荐

0 条评论