概述
图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。
函数
double cv::cuda::threshold
(
InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type,
Stream & stream = Stream::Null()
)
src | 源图像 |
dst | 输出图像 |
thresh | 阈值 |
maxval | 最大值 |
type | 阈值类型 |
stream |
阈值类型:
测试代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <vector>
using namespace cv;
using namespace std;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//载入原始图像
Mat mat0 = imread("c:/opencv/333.jpg");
//显示
imshow("mat",mat0);
//将图像转为灰度图
Mat im_gray;
cv::cvtColor(mat0,im_gray,COLOR_BGR2GRAY);
imshow("im_gray",im_gray);
//图像二值化处理
Mat mat_binary;
cv::threshold(im_gray,mat_binary,245,255,THRESH_BINARY);
//显示
imshow("mat_binary",mat_binary);
}
Widget::~Widget()
{
delete ui;
}
测试结果
将处理后的图像放大可见,只有 0 和 255 两种数值。