::返回OpenCV算子速查表
1. 函数定义
double threshold(
InputArray src,
OutputArray dst,
double thresh,
double maxval,
int type
);
阈值类型 | 操作 |
---|
cv::THRESH_BINARY | DST=(SRC>thresh)?MAXVALUE:0 |
cv::THRESH_BINARY_INV | DST=(SRC>thresh)?0:MAXVALUE |
cv::THRESH_TRUNC | DST=(SRC>thresh)?THRESH:SRC |
cv::THRESH_TOZERO | DST=(SRC>thresh)?SRC:0 |
cv::THRESH_TOZERO_INV | DST=(SRC>thresh)?0:SRC |
2. 例程
#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
namedWindow("电芯", WINDOW_NORMAL);
namedWindow("THRESH_BINARY", WINDOW_NORMAL);
namedWindow("THRESH_BINARY_INV", WINDOW_NORMAL);
namedWindow("THRESH_TRUNC", WINDOW_NORMAL);
namedWindow("THRESH_TOZERO", WINDOW_NORMAL);
namedWindow("THRESH_TOZERO_INV", WINDOW_NORMAL);
namedWindow("THRESH_OTSU", WINDOW_NORMAL);
namedWindow("THRESH_TRIANGLE", WINDOW_NORMAL);
Mat m_ImgCell = imread("./1.bmp", IMREAD_GRAYSCALE);
Mat m_ImgThreshBinary;
Mat m_ImgThreshBinaryInv;
Mat m_ImgThreshTrunc;
Mat m_ImgThreshToZero;
Mat m_ImgThreshToZeroInv;
Mat m_ImgThreshOtsu;
Mat m_ImgThreshTriangle;
double m_dbThresh = 150;
double m_dbMaxValue = 255;
threshold(m_ImgCell, m_ImgThreshBinary, m_dbThresh, m_dbMaxValue, THRESH_BINARY);
threshold(m_ImgCell, m_ImgThreshBinaryInv, m_dbThresh, m_dbMaxValue, THRESH_BINARY_INV);
threshold(m_ImgCell, m_ImgThreshTrunc, m_dbThresh, m_dbMaxValue, THRESH_TRUNC);
threshold(m_ImgCell, m_ImgThreshToZero, m_dbThresh - 70, m_dbMaxValue, THRESH_TOZERO);
threshold(m_ImgCell, m_ImgThreshToZeroInv, m_dbThresh - 70, m_dbMaxValue, THRESH_TOZERO_INV);
double m_dbOtsuThresh = threshold(m_ImgCell, m_ImgThreshOtsu, 0, m_dbMaxValue, THRESH_BINARY | THRESH_OTSU);
double m_dbTriangleThresh = threshold(m_ImgCell, m_ImgThreshTriangle, 0, m_dbMaxValue, THRESH_BINARY | THRESH_TRIANGLE);
imshow("电芯", m_ImgCell);
imshow("THRESH_BINARY", m_ImgThreshBinary);
imshow("THRESH_BINARY_INV", m_ImgThreshBinaryInv);
imshow("THRESH_TRUNC", m_ImgThreshTrunc);
imshow("THRESH_TOZERO", m_ImgThreshToZero);
imshow("THRESH_TOZERO_INV", m_ImgThreshToZeroInv);
imshow("THRESH_OTSU", m_ImgThreshOtsu);
imshow("THRESH_TRIANGLE", m_ImgThreshTriangle);
cout << m_dbOtsuThresh << endl;
cout << m_dbTriangleThresh << endl;
waitKey(0);
return 0;
}