日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2023/12/19 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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++ 绘制函数图像_图像轮廓和分水岭算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。