c++ 绘制函数图像_图像轮廓和分水岭算法
生活随笔
收集整理的這篇文章主要介紹了
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++ 绘制函数图像_图像轮廓和分水岭算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银行卡号码的校验规则
- 下一篇: 研究内容、研究方法、研究方案的区别与联系