欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

c++ 绘制函数图像_图像轮廓和分水岭算法

发布时间:2023/12/19 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 c++ 绘制函数图像_图像轮廓和分水岭算法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 寻找轮廓

findContours() 函数用于在二值图像中寻找轮廓。常与 drawContours() 函数配合使用;findContours() 函数检测到图像的轮廓后,就可以用 drawContours() 函数将检测到的轮廓绘制出来。

2. 绘制轮廓

drawContours() 函数用于在图像中绘制外部或内部轮廓。

3. 综合例子

利用图像平滑技术(blur ()函数)和边缘检测技术(canny()函数),根据滑动条,动态地检测出图形的轮廓。

例子代码:

#include#includeusing namespace std;using namespace cv;Mat srcImg, grayImg,cannyOutImg;int ThreshValue = 80;int ThrashValueMax = 255;vector> vContours;vector vHierarchy;RNG rng(12345);void ThreshChange(int, void*);void test(){    srcImg = imread("home.jpg");    if (srcImg.empty())    {        cout <

效果图:

4. 分水岭算法

例子代码:

#include#includeusing namespace std;using namespace cv;Mat srcImg, maskImg, cannyOutImg;Point prevPt(-1, -1);//鼠标回调函数static void Mouse(int event, int x, int y, int flags, void*);static void ShowHelpText();  //提升用户操作函数void test(){    srcImg = imread("home.jpg");    if (srcImg.empty())    {        cout <> contours;            vector hierarchy;            //寻找轮廓            findContours(maskImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);            //轮廓为空时的处理            if (contours.empty())                continue;            //复制掩膜            Mat maskImg2(maskImg.size(), CV_32S);            maskImg2 = Scalar::all(0);            //循环绘制出轮廓            for (int i = 0; i >= 0; i = hierarchy[i][0], compCount++)                drawContours(maskImg2, contours, i, Scalar::all(compCount+1), -1, 8, hierarchy, INT_MAX);                //compCount 为零时的处理                if (compCount == 0)                    continue;                    //生成随机颜色                    vector colorTab;                    for (int j = 0; j (i, j);                            if (index == -1)                                watershedImg.at(i, j) = Vec3b(255, 255, 255);                            else if (index <= 0 || index > compCount)                                watershedImg.at(i, j) = Vec3b(0, 0, 0);                            else                                watershedImg.at(i, j) = colorTab[index - 1];                        }                        //混合灰度图像和分水岭效果图并显示最终的窗口                        watershedImg = watershedImg*0.5 + grayImg*0.5;                        imshow("watershed transform", watershedImg);            }    }}static void Mouse(int event, int x, int y, int flags, void*){    //处理鼠标不在窗口中的情况    if (x = srcImg.cols || y = srcImg.rows)        return;    //处理鼠标左键相关消息    if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON))        prevPt = Point(-1, -1);    //处理鼠标左键按下并移动,绘制出白色线条    else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))    {        Point pt(x, y);        if (prevPt.x 

效果图:

总结

以上是生活随笔为你收集整理的c++ 绘制函数图像_图像轮廓和分水岭算法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。