注:使用彩色图测试结果失败了,灰度图就可以
double threshold( InputArray src, OutputArray dst,double thresh, double maxval, int type );
src:8位原始图像
dst:输出图像
thresh:阀值
maxval:灰度图的最大值,输出图像的最大像素值
type: 检测方式
THRESH_BINARY = 0,
THRESH_BINARY_INV = 1,
THRESH_TRUNC = 2,
THRESH_TOZERO = 3,
THRESH_TOZERO_INV = 4,
THRESH_MASK = 7,
THRESH_OTSU = 8,
THRESH_TRIANGLE = 16
//m_Mat原始图像转灰度图
m_MatGray = Mat::zeros(m_Mat.size(),m_Mat.type()) ;
cvtColor(m_Mat, m_MatGray,COLOR_BGR2GRAY);
//边缘检测:m_Mat_OutPut接收轮廓点的图
threshold(m_MatGray,m_Mat_OutPut,150,200,THRESH_BINARY);
//轮廓发现
vector<vector<Point>>contours;
vector<Vec4i>hierachy;
findContours(m_Mat_OutPut,contours,hierachy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));
//将符合条件的轮廓画在原图上
for (int i = 0; i < contours.size(); ++i) {
double area = contourArea(contours[i]);//轮廓面积
double length = arcLength(contours[i],false);//轮廓长度
if(area<1000&&length<1000){continue;}
drawContours(m_Mat,contours,i,Scalar(rand()%255,rand()%255,rand()%255));//
}
//显示
QImage img = cvMat_To_Qimage(m_Mat);
img = img.scaled(ui->label_4->geometry().width(),ui->label_4->geometry().height(),
Qt::KeepAspectRatio,Qt::SmoothTransformation);
ui->label_4->setPixmap(QPixmap::fromImage(img));