阈值操作是最简单的图像分割的方法。应用举例:从一副图像中利用阈值分割出我们需要的物体部分(当然这里的物体可以是一部分或者整体)。这样的图像分割方法是基于图像中物体与背景之间的灰度差异,而且此分割属于像素级的分割。
为了从一副图像中提取出我们需要的部分,应该用图像中的每一个像素点的灰度值与选取的阈值进行比较,并作出相应的判断。(注意:阈值的选取依赖于具体的问题。即:物体在不同的图像中有可能会有不同的灰度值。)一旦找到了需要分割的物体的像素点,我们可以对这些像素点设定一些特定的值来表示。(例如:可以将该物体的像素点的灰度值设定为:‘0’(黑色),其他的像素点的灰度值为:‘255’(白色);当然像素点的灰度值可以任意,但最好设定的两种颜色对比度较强,方便观察结果)。
前言
越来越多的开发人员选择基于开源的Qt框架与OpenCV来实现界面和算法,其原因不单单是无版权问题,更多是两个社区的发展蓬勃,可用来学习的资料与例程特别丰富。以下是关于利用Qt构建GUI并使用OpenCV中的threshold函数进行图像阈值处理。
软件版本:Qt-5.12.0/OpenCV-4.5.3
平台:Windows10/11–64
一、函数介绍
函数原型:
cv::threshold(InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type);
参数解释:
**src **: 表示输入图像;
**dst **: 表示输出图像(尺寸和类型和输入图像一样);
thresh : 表示阈值;
**maxval **: 表示预设最大值;
**type **: 表示阈值化处理的类型设置;
阈值类型枚举:
1 THRESH_BINARY
2 THRESH_BINARY_INV
3 THRESH_TRUNC
4 THRESH_TOZERO
5 THRESH_TOZERO_INV
6 THRESH_MASK–不支持32位
7 THRESH_OTSU–不支持32位
8 THRESH_TRIANGLE --不支持32位
enum ThresholdTypes {
THRESH_BINARY = 0,
THRESH_BINARY_INV = 1,
THRESH_TRUNC = 2,
THRESH_TOZERO = 3,
THRESH_TOZERO_INV = 4,
THRESH_MASK = 7,
THRESH_OTSU = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
THRESH_TRIANGLE = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
};
二、演示
1、GUI
如上图创建type的QComboBox和Threshold功能按钮QPushButton,对当前窗口的图像进行阈值操作,并输出状态信息。
2、实现代码
thresholdBtn的**clicked()**槽函数实现代码:
void MainWindow::on_thresholdBtn_clicked() { std::size_t numView = ui->tabWidget->currentIndex() % 3; if (dispMat[numView]->empty()) { outputInfo(2, tr("Please make sure the Mat exist!")); return; } if (dispMat[numView]->channels() == 3) { cv::cvtColor(*dispMat[numView], *dispMat[numView], cv::COLOR_RGB2GRAY); } int threshValue = ui->thresholdSlider->value(); // 获取阈值threshValue int threshType = ui->thresholdCombo->currentIndex(); // 获取类型 tmpMat->zeros(dispMat[numView]->rows, \ dispMat[numView]->cols, \ dispMat[numView]->type()); cv::threshold(*dispMat[numView], *tmpMat, threshValue, 255, threshType); if (ui->thresholdChkBox->isChecked()) { *dispMat[numView] = tmpMat->clone(); cvtMatPixmap(dispMat, dispPixmap, numView); } else { QImage tmpImage = QImage(tmpMat->data, tmpMat->cols,tmpMat->rows, \ static_cast<int>(tmpMat->step), \ QImage::Format_Grayscale8); dispPixmap[numView]->setPixmap(QPixmap::fromImage(tmpImage.rgbSwapped())); } outputInfo(1, tr("Threshold done.")); }
总结
以上是关于利用Qt进行GUI构建并使用OpenCV中的threshold函数进行图像阈值处理。