《OpenCV3编程入门》学习笔记8 图像轮廓与图像分割修复(五)分水岭算法(watershed algorithm)
生活随笔
收集整理的這篇文章主要介紹了
《OpenCV3编程入门》学习笔记8 图像轮廓与图像分割修复(五)分水岭算法(watershed algorithm)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
8.5 分水嶺算法(watershed algorithm)
1.基于拓撲理論的數學形態學的分割方法。
2.基本思想:把圖像看作測地學上的拓撲地貌,圖像中每一點像素的灰度值表示該點的海拔高度,每一個局部極小值及其影響區域稱為集水盆,分水嶺變換得到的是輸入圖像的集水盆圖像,集水盆邊界形成分水嶺(輸入圖像的極大值點)。
3.計算過程:一個迭代標注過程,包括排序過程和淹沒過程,先對每個像素的灰度級進行從低到高的排序,然后在從低到高實現淹沒的過程中,對每一個局部最小值在h階高度的影響域采用先進先出(FIFO)結構進行判斷及標注。
8.5.1 實現分水嶺算法:watershed()函數
1.基本操作:
??基于標記的分割算法,在把圖像傳給函數之前,需要大致標記出圖像中期望進行分割的區域,每個區域被標記為像素值1、2、3等,表示成為一個或多個連接組件,標記的值可使用findContours()函數和drawContours()函數由二進制的掩碼檢索出來,函數輸出中,每一個標記中的像素被設置為標記的值,區域間的值被設置為-1。
2.函數原型:
void watershed(InputArray image,InputOutputArray markers)
3.參數說明:
??(1)輸入圖像,8位三通道彩色圖像
??(2)函數調用后運算結果,輸入/輸出32位單通道圖像的標記結果
8.5.2 綜合示例
/*程序說明:鼠標大致標記出圖像中期望進行分割的區域鍵盤按鍵【1】啟動分水嶺算法按鍵【2】恢復原始圖重新標記
*/
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
//定義輔助宏
#define WINDOW_NAME1 "【原始圖窗口】"
#define WINDOW_NAME2 "【恢復原始圖窗口】"
#define WINDOW_NAME3 "【分水嶺變換窗口】"
//全局變量
Mat g_srcImage, g_maskImage;
Point prevPt(-1, -1);
//全局函數
static void on_Mouse(int event, int x, int y, int flags, void*);
static void ShowHelpText();int main()
{//【0】顯示幫助信息ShowHelpText();//【1】載入原圖并顯示g_srcImage = imread("night.jpg");if (!g_srcImage.data){ printf("載入原圖像失敗~!\n");return false;}imshow(WINDOW_NAME1, g_srcImage);//【2】初始化掩模和灰度圖Mat srcImage, grayImage;g_srcImage.copyTo(srcImage);cvtColor(srcImage, g_maskImage, COLOR_BGR2GRAY);cvtColor(g_maskImage, grayImage, COLOR_GRAY2BGR);g_maskImage = Scalar::all(0);//【3】設置鼠標回調函數setMouseCallback(WINDOW_NAME1, on_Mouse, 0);//【4】輪詢按鍵,進行處理while (1){//獲取鍵值int c = waitKey(0);//按鍵值為ESC時,退出程序if ((char)c == 27)break;//按鍵為2時,恢復原圖,使g_maskImage和g_srcImage可重新標記if ((char)c == '2'){g_maskImage = Scalar::all(0);srcImage.copyTo(g_srcImage);imshow(WINDOW_NAME2, g_srcImage);}//按鍵值為1時,進行處理if ((char)c == '1'){//定義一些參數int i, j;int compCount = 0; //記錄輪廓數vector<vector<Point>>contours;vector<Vec4i> hierarchy;//尋找輪廓findContours(g_maskImage, contours, hierarchy, CV_RETR_CCOMP, CHAIN_APPROX_SIMPLE);//輪廓為空時的處理if (contours.empty())continue;//復制掩膜Mat maskImage(g_maskImage.size(), CV_32S);maskImage = Scalar::all(0);//循環繪制輪廓for (int index = 0; index >= 0; index = hierarchy[index][0], compCount++){drawContours(maskImage, contours, index, Scalar::all(compCount + 1), -1, 8, hierarchy, INT_MAX);}//為每一個輪廓生成一個隨機顏色vector<Vec3b>colorTab;for (int i = 0; i < compCount; i++){int b = theRNG().uniform(0, 255);int g = theRNG().uniform(0, 255);int r = theRNG().uniform(0, 255);colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));}//進行分水嶺算法并計算處理時間輸出到窗口中double dTime = (double)getTickCount();watershed(srcImage, maskImage);dTime = (double)getTickCount() - dTime;printf("\t 處理時間 = %gms\n", dTime*1000. / getTickFrequency());//雙層循環,將分水嶺圖像遍歷存入watershedImage中Mat watershedImage(maskImage.size(), CV_8UC3);for (i = 0; i < maskImage.rows; i++)for (j = 0; j < maskImage.cols; j++){int index = maskImage.at<int>(i, j);if (index == -1)watershedImage.at<Vec3b>(i, j) = Vec3b(255, 255, 255);//maskImage中區域間值像素為-1,將對應watershed圖像像素設置為白色(255,255,255)else if (index <= 0 || index > compCount)watershedImage.at<Vec3b>(i, j) = Vec3b(0, 0, 0); //maskImage中非標記區域,將對應watershed圖像像素設置為黑色(0,0,0)elsewatershedImage.at<Vec3b>(i, j) = colorTab[index - 1]; //maskImage中標記區域,將對應watershed圖像像素設置為之前隨機出的顏色colorTab}//混合灰度圖和分水嶺效果圖并顯示最終的窗口watershedImage = watershedImage * 0.5 + grayImage * 0.5;imshow(WINDOW_NAME3, watershedImage);}}return 0;
}//鼠標消息回調函數
void on_Mouse(int event, int x, int y, int flags, void*)
{//處理鼠標不在窗口中的情況if (x < 0 || x >= g_srcImage.cols || y < 0 || y >= g_srcImage.rows)return;//處理鼠標左鍵相關消息if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON)) //松開左鍵prevPt = Point(-1, -1);else if (event == EVENT_LBUTTONDOWN)//按下左鍵prevPt = Point(x, y); //記錄鼠標按下時的位置,作為白色線條的起始點//鼠標左鍵呈按下狀態并移動,繪制出白色線條else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)){Point pt(x, y);if (prevPt.x < 0) //如果白線起始點范圍溢出,則白線起始點為當前點ptprevPt = pt;line(g_maskImage, prevPt, pt, Scalar::all(255), 2, 8, 0);//畫白線line(g_srcImage, prevPt, pt, Scalar::all(255), 2, 8, 0); //畫白線prevPt = pt;imshow(WINDOW_NAME1, g_srcImage);}
}
static void ShowHelpText()
{printf("\n\n\t歡迎來到【分水嶺算法】示例程序~\n");printf("\n\t請先用鼠標在圖片窗口中標記出大致的區域\n");printf("\n\t然后再按鍵【1】啟動分水嶺算法\n");printf("\n\t按鍵操作說明:\n");printf("\t\t\t鍵盤按鍵【1】--運行分水嶺分割算法\n");printf("\t\t\t鍵盤按鍵【2】--恢復原始圖\n");printf("\t\t\t鍵盤按鍵【ESC】--退出程序\n");
}
運行效果:
總結
以上是生活随笔為你收集整理的《OpenCV3编程入门》学习笔记8 图像轮廓与图像分割修复(五)分水岭算法(watershed algorithm)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求越字开头的成语接龙!
- 下一篇: 《OpenCV3编程入门》学习笔记8 图