先看一下canny线。
https://blog.csdn.net/islinyoubiao/article/details/113786741
修改一下代码:
void MainWindow::cannyProc(cv::Mat in)
{
if(in.empty())
return;
if(in.channels()>1)
return;
// lowThreshold = low;
qDebug() << "start blur";
cv::blur(in, detected_edges, cv::Size(3,3));
qDebug() << "start canny";
cv::Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
dst= cv::Scalar::all(0);
qDebug() << "start copy";
src.copyTo(dst, detected_edges);
// out = dst;
//查找轮廓
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(detected_edges,contours,hierarchy,cv::RETR_TREE,cv::CHAIN_APPROX_SIMPLE,cv::Point());
cv::Mat imageContours=cv::Mat::zeros(src.size(),CV_8UC1); //轮廓
cv::Mat marks(src.size(),CV_32S); //Opencv分水岭第二个矩阵参数
marks=cv::Scalar::all(0);
int index = 0;
int compCount = 0;
for( ; index >= 0; index = hierarchy[index][0], compCount++ )
{
//对marks进行标记,对不同区域的轮廓进行编号,相当于设置注水点,有多少轮廓,就有多少注水点
cv::drawContours(marks, contours, index, cv::Scalar::all(compCount+1), 1, 8, hierarchy);
cv::drawContours(imageContours,contours,index,cv::Scalar(255),1,8,hierarchy);
cv::drawContours(dst, contours, index, cv::Scalar::all(compCount+1), 1, 8, hierarchy);
}
}
添加Contours,
效果:
多谢,亲爱的美美。