分水岭算法常用于目标分割研究,其将图像以灰度为参考可视化为一种拓扑地貌,灰度大小等价于海拔高低,如果向地貌中的山谷进行漫水,一定程度时山丘将被分开成独立个体
1.1基于降雨法实现分水岭
如上图所示,假设雨水从而给高出降落,最终在山谷聚集,不同的山谷被赋予不同颜色,然而,在不同颜色出现汇聚的地方,混合颜色的位置就可以修建大坝,将目标分割开
1.2基于灌水法实现分水岭
如上图所示平面图,从各个山谷挖个洞向上灌水(灌水位置对应局部极小值,伴随水位上升,不同山谷的水相遇时,就可以修建大坝
附:opencv官方应用举例:
基于距离变换标记分水岭实现分割
const int SHED = -1; const int INQUEUE = -2; struct point2D {
int r, c; }; void WaterShed_(Mat & gray_img, cv::Mat & markers) /* * gray_img: a 1 channel 8-bit img. * markers: different basins marked as different positive integers, * other position is 0.(32-bit 1 channel img) * * return: watershed mask.(1 channel) */ {
if (gray_img.channels() == 3) {
cvtColor(gray_img, gray_img, COLOR_BGR2GRAY); } const int graylevelnum = 256; vector<queue<point2D>> queues; queues.reserve(graylevelnum); for (int i = 0; i < graylevelnum; i++) {
queues.emplace_back(queue<point2D>()); } //res设为输出标记图像 int Rows = res.rows - 1, Cols = res.cols - 1; int *p = res.ptr<int>(0); for (int i = 0; i < res.cols; i++) {
p[i] = SHED; }//第一行置为-1 p = res.ptr<int>(Rows); for (int i = 0; i < res.cols; i++) {
p[i] = SHED; }//最后一行置为-1 for (int i = 1; i < Rows; i++)//当前res代表标记图像 {
int *res_p_pre = res.ptr<int >(i - 1); int *res_p_cur = res.ptr<int >(i); int *res_p_next = res.ptr<int >(i + 1); uchar *gray_p = gray.ptr<uchar >(i); res_p_cur[0] = SHED; res_p_cur[Cols] = SHED;//结果图第一列和最后一列置为-1 for (int j = 1; j < Cols; j++)//遍历每一列 {
if (res_p_cur[j] < 0) {
res_p_cur[j] = 0; }//?不存在<0的情况吧 //check 4 neighbors. int intensity = graylevelnum;//强度初始化为256 if (res_p_cur[j] == 0 && (res_p_pre[j] > 0 || res_p_next[j] > 0 || res_p_cur[j - 1] > 0 || res_p_cur[j + 1] > 0))//如果标记当前点为0+左一点>0+右一点>0+上一点>0+下一点>0 {
intensity = gray_p[j]; } //强度置为标记点在原图的灰度 if (intensity < graylevelnum) //如果小于256,保存到每个灰度级对应的容器中,将其视为将要操作的点,暂时灰度值设为-2 {
queues[intensity].push({
i,j }); res_p_cur[j] = INQUEUE; } } } int ind = 0; for (; ind < graylevelnum; ind++) {
if (!queues[ind].empty()) {
break; }//找到第一个储存灰度级的不为空的容器索引 } if (ind < graylevelnum)//如果小于最大灰度级 {
int lab, t; while (true) {
if (queues[ind].empty()) {
for (; ind < graylevelnum; ind++) {
if (!queues[ind].empty()) {
break; }//灰度级遍历时也会出现大小为0的灰度级容器,跳过它找出下一个索引 } } if (ind >= graylevelnum) {
break; } point2D cur = queues[ind].front();//读取当前种子点 queues[ind].pop();//删除栈顶元素 //std::cout << ind << " " << cur.r << " " << cur.c << std::endl; //mark current pixel.获取label值,用于标记? lab = 0; t = res.at<int>(cur.r - 1, cur.c);//此处四邻域与上边记录四邻域点存邻域时保持一致 if (t > 0) lab = t; t = res.at<int>(cur.r + 1, cur.c);//公共区域修建大坝? if (t > 0) {
if (lab == 0) {
lab = t; } else if (lab != t) {
lab = SHED; } } t = res.at<int>(cur.r, cur.c - 1); if (t > 0) {
if (lab == 0) {
lab = t; } else if (lab != t) {
lab = SHED; } } t = res.at<int>(cur.r, cur.c + 1); if (t > 0) {
if (lab == 0) {
lab = t; } else if (lab != t) {
lab = SHED; } } assert(lab != 0); res.at<int>(cur.r, cur.c) = lab;//当前种子点在结果图像标记为lab //std::cout << "lab:" << lab << std::endl; if (lab != SHED)//如果标记不是大坝,应继续进行生长漫水==>将原始标记图中未标记的点压入容器进行标记 {
//check 4 neighbors for unmarked pixels. int r_ind = cur.r - 1, c_ind = cur.c;//r、c表示行与列 if (res.at<int>(r_ind, c_ind) == 0)//未进行标记的点 {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } r_ind = cur.r + 1; c_ind = cur.c; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t; } r_ind = cur.r; c_ind = cur.c - 1; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t; } r_ind = cur.r; c_ind = cur.c + 1; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t; } } } } markers = res.clone(); }
加入梯度图进行改进:
Mat sobel_x, sobel_y; Sobel(gray_img, sobel_x, CV_64F, 1, 0, 3); Sobel(gray_img, sobel_y, CV_64F, 0, 1, 3); convertScaleAbs(sobel_x, sobel_x); convertScaleAbs(sobel_y, sobel_y); Mat grad = sobel_x + sobel_y;
针对梯度图中的背景点并进行操作
if (lab != SHED)//如果标记不是大坝,应继续进行生长漫水==>将原始标记图中未标记的点压入容器进行标记 {
//check 4 neighbors for unmarked pixels. int r_ind = cur.r - 1, c_ind = cur.c;//r、c表示行与列 if (res.at<int>(r_ind, c_ind) == 0)//未进行标记的点 {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; if (t != 0) {
queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } //queues[t].push({ r_ind,c_ind }); //res.at<int>(r_ind, c_ind) = INQUEUE; //ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } r_ind = cur.r + 1; c_ind = cur.c; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; if (t != 0) {
queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } /*queues[t].push({ r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;*/ } r_ind = cur.r; c_ind = cur.c - 1; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; if (t != 0) {
queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } /*queues[t].push({ r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;*/ } r_ind = cur.r; c_ind = cur.c + 1; if (res.at<int>(r_ind, c_ind) == 0) {
t = gray.at<uchar >(r_ind, c_ind); //std::cout << "t:" << t << std::endl; if (t != 0) {
queues[t].push({
r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;//ind为当前遍历的灰度级 } /*queues[t].push({ r_ind,c_ind }); res.at<int>(r_ind, c_ind) = INQUEUE; ind = ind < t ? ind : t;*/ } }
主程序:
static void help(char** argv) {
cout << "\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n" "Usage:\n" << argv[0] << " [image_name -- default is fruits.jpg]\n" << endl; cout << "Hot keys: \n" "\tESC - quit the program\n" "\tr - restore the original image\n" "\tw or SPACE - run watershed segmentation algorithm\n" "\t\t(before running it, *roughly* mark the areas to segment on the image)\n" "\t (before that, roughly outline several markers on the image)\n"; } Mat markerMask, img; Point prevPt(-1, -1); static void onMouse(int event, int x, int y, int flags, void*) {
if (x < 0 || x >= img.cols || y < 0 || y >= img.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) prevPt = pt; line(markerMask, prevPt, pt, Scalar(12,234,12), 40, 8, 0); line(img, prevPt, pt, Scalar(12,234,12), 40, 8, 0); prevPt = pt; imshow("image", img); } } int main(int argc, char** argv) {
cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | fruits.jpg | }"); if (parser.has("help")) {
help(argv); return 0; } //string filename = samples::findFile(parser.get<string>("@input")); Mat img0 = imread("..."), imgGray; if (img0.empty()) {
cout << "Couldn't open image "; help(argv); return 0; } help(argv); //img0 = img0(Rect(1000, 1000, 800, 800)); namedWindow("image", WINDOW_NORMAL); img0.copyTo(img); cvtColor(img, markerMask, COLOR_BGR2GRAY); cvtColor(markerMask, imgGray, COLOR_GRAY2BGR); markerMask = Scalar::all(0); imshow("image", img); setMouseCallback("image", onMouse, 0); for (;;) {
char c = (char)waitKey(0); if (c == 27) break; if (c == 'r') {
markerMask = Scalar::all(0); img0.copyTo(img); namedWindow("image", WINDOW_NORMAL); imshow("image", img); } if (c == 'w' || c == ' ') {
int i, j, compCount = 0; vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(markerMask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE); if (contours.empty()) continue; Mat markers(markerMask.size(), CV_32S); markers = Scalar::all(0); int idx = 0; for (; idx >= 0; idx = hierarchy[idx][0], compCount++) drawContours(markers, contours, idx, Scalar::all(compCount + 1), -1, 8, hierarchy, INT_MAX); if (compCount == 0) continue; vector<Vec3b> colorTab; for (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 t = (double)getTickCount(); medianBlur(img0, img0, 7); //watershed(img0, markers); //myWatered(img0, markers); //myWaterThred(img0, markers); WaterShed_(img0, markers); //imgseg::watershedColor(img0, markers); //imgseg::vfWatershed(img0, markers); t = (double)getTickCount() - t; printf("execution time = %gms\n", t*1000. / getTickFrequency()); Mat wshed(markers.size(), CV_8UC3); // paint the watershed image for (i = 0; i < markers.rows; i++) for (j = 0; j < markers.cols; j++) {
int index = markers.at<int>(i, j); if (index == -1) wshed.at<Vec3b>(i, j) = Vec3b(255, 255, 255); else if (index <= 0 || index > compCount) wshed.at<Vec3b>(i, j) = Vec3b(0, 0, 0); else wshed.at<Vec3b>(i, j) = colorTab[index - 1]; } wshed = wshed * 0.5 + imgGray * 0.5; namedWindow("watershed transform", WINDOW_NORMAL); imshow("watershed transform", wshed); } } return 0; }
主要参考:出自—会飞的吴克