使用直方圖相似性合併相似區域
#include#include#include#include#include#includeusing namespace std;
using namespace cv;
mat display(mat& markers, int noofsegment)
mat watershedimage(markers.size(), cv_8uc3);
for (int i = 0; i < markers.rows; i++) }
return watershedimage;
}//分水嶺影象分割
mat watershedsrgment(mat src, int& noofsegment)//noofsegment表示分割的類別數
imshow("markers", markers * 255);
double t = (double)gettickcount();
watershed(src, markers);
t = (double)gettickcount() - t;
cout << "time: " << t * 1000 / gettickfrequency() << endl;
mat wash = display(markers, compcount);
imshow("watershedsrgment", wash);
noofsegment = compcount;
waitkey(0);
return markers;
}//分水嶺影象合併
void segmerge(mat src, mat& segments, int& numseg)
//計算每個畫素的歸屬
for (int i = 0; i < segments.rows; i++) }
//建立直方圖
vectorhist_bases;
mat hsv_base;
int h_bins = 35;
int s_bins = 30;
int hist_size = ;
float h_ranges = ;
float s_ranges = ;
const float* ranges = ;
int channels = ;
ma*** hist_base;
for (int i = 0; i <= numseg; i++)
else
hist_bases.push_back(mat());
hist_base.release();
} //計算各直方圖之間的相似性
double similarity = 0;
vectormerged;
for (int k = 0; k < hist_bases.size(); k++)
for (int c = 0; c < hist_bases.size(); c++)}}
}}
}} }
numseg = newnumseg;
mat wash=display(segments, numseg);
imshow("segmerge", wash);
waitkey(0);
}int main()
opencv 分水嶺演算法
分水嶺演算法是一種簡單的影象分割演算法,具體是人為規定兩點 以兩點為例 然後把這兩點看做低谷,向裡面注水,不停地蔓延,直到兩點形成的湖交界。opencv自帶了這個程式。摘抄如下,其中我做了一些修改。本程式的目的是測試分水嶺演算法 include include include include usi...
OpenCV的分水嶺演算法
本文使用python實現 import cv2 as cv import numpy as np def watershed demo src print src.shape blurred cv.pyrmeanshiftfiltering src,10,100 去雜訊 gray cv.cvtcol...
opencv實現分水嶺演算法
分水嶺演算法原理 iplimage marker mask 0 iplimage markers 0 iplimage img0 0,img 0,img gray 0,wshed 0 iplimage img gray 0,wshed 0 cvpoint prev pt void on mouse ...