运行环境:ubuntu16.04+Qt+opencv2.4.13.3

watershed.cpp

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace cv;
using namespace std; Vec3b RandomColor(int value); //生成随机颜色函数 int main( char argc, char* argv[] )
{
Mat image=imread("/home/osksh/skin_c/06Apr03Face.jpg"); // Mat image=imread('/home/osksh/skin_c/family.jpg'); //载入RGB彩色图像
imshow("Source Image",image); //灰度化,滤波,Canny边缘检测
Mat imageGray;
cvtColor(image,imageGray,CV_RGB2GRAY);//灰度转换
GaussianBlur(imageGray,imageGray,Size(,),); //高斯滤波
imshow("Gray Image",imageGray);
Canny(imageGray,imageGray,,);
imshow("Canny Image",imageGray); //查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(imageGray,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
Mat imageContours=Mat::zeros(image.size(),CV_8UC1); //轮廓
Mat marks(image.size(),CV_32S); //Opencv分水岭第二个矩阵参数
marks=Scalar::all();
int index = ;
int compCount = ;
for( ; index >= ; index = hierarchy[index][], compCount++ )
{
//对marks进行标记,对不同区域的轮廓进行编号,相当于设置注水点,有多少轮廓,就有多少注水点
drawContours(marks, contours, index, Scalar::all(compCount+), , , hierarchy);
drawContours(imageContours,contours,index,Scalar(),,,hierarchy);
} //我们来看一下传入的矩阵marks里是什么东西
Mat marksShows;
convertScaleAbs(marks,marksShows);
imshow("marksShow",marksShows);
imshow("轮廓",imageContours);
watershed(image,marks); //我们再来看一下分水岭算法之后的矩阵marks里是什么东西
Mat afterWatershed;
convertScaleAbs(marks,afterWatershed);
imshow("After Watershed",afterWatershed); //对每一个区域进行颜色填充
Mat PerspectiveImage=Mat::zeros(image.size(),CV_8UC3);
for(int i=;i<marks.rows;i++)
{
for(int j=;j<marks.cols;j++)
{
int index=marks.at<int>(i,j);
if(marks.at<int>(i,j)==-)
{
PerspectiveImage.at<Vec3b>(i,j)=Vec3b(,,);
}
else
{
PerspectiveImage.at<Vec3b>(i,j) =RandomColor(index);
}
}
}
imshow("After ColorFill",PerspectiveImage); //分割并填充颜色的结果跟原始图像融合
Mat wshed;
addWeighted(image,0.4,PerspectiveImage,0.6,,wshed);
imshow("AddWeighted Image",wshed); waitKey();
} Vec3b RandomColor(int value)
{
value=value%; //生成0~255的随机数
RNG rng;
int aa=rng.uniform(,value);
int bb=rng.uniform(,value);
int cc=rng.uniform(,value);
return Vec3b(aa,bb,cc);
}

#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/highgui/highgui.hpp"

#include<iostream>

usingnamespacecv;
usingnamespacestd;

Vec3bRandomColor(intvalue);//生成随机颜色函数

intmain(charargc,char*argv[])
{
Matimage=imread("/home/osksh/skin_c/06Apr03Face.jpg");

//Matimage=imread('/home/osksh/skin_c/family.jpg');//载入RGB彩色图像
imshow("SourceImage",image);

//灰度化,滤波,Canny边缘检测
MatimageGray;
cvtColor(image,imageGray,CV_RGB2GRAY);//灰度转换
GaussianBlur(imageGray,imageGray,Size(,),);//高斯滤波
imshow("GrayImage",imageGray);
Canny(imageGray,imageGray,,);
imshow("CannyImage",imageGray);

//查找轮廓
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imageGray,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
MatimageContours=Mat::zeros(image.size(),CV_8UC1);//轮廓
Matmarks(image.size(),CV_32S);//Opencv分水岭第二个矩阵参数
marks=Scalar::all();
intindex=;
intcompCount=;
for(;index>=;index=hierarchy[index][],compCount++)
{
//对marks进行标记,对不同区域的轮廓进行编号,相当于设置注水点,有多少轮廓,就有多少注水点
drawContours(marks,contours,index,Scalar::all(compCount+),,,hierarchy);
drawContours(imageContours,contours,index,Scalar(),,,hierarchy);
}

//我们来看一下传入的矩阵marks里是什么东西
MatmarksShows;
convertScaleAbs(marks,marksShows);
imshow("marksShow",marksShows);
imshow("轮廓",imageContours);
watershed(image,marks);

//我们再来看一下分水岭算法之后的矩阵marks里是什么东西
MatafterWatershed;
convertScaleAbs(marks,afterWatershed);
imshow("AfterWatershed",afterWatershed);

//对每一个区域进行颜色填充
MatPerspectiveImage=Mat::zeros(image.size(),CV_8UC3);
for(inti=;i<marks.rows;i++)
{
for(intj=;j<marks.cols;j++)
{
intindex=marks.at<int>(i,j);
if(marks.at<int>(i,j)==-)
{
PerspectiveImage.at<Vec3b>(i,j)=Vec3b(,,);
}
else
{
PerspectiveImage.at<Vec3b>(i,j)=RandomColor(index);
}
}
}
imshow("AfterColorFill",PerspectiveImage);

//分割并填充颜色的结果跟原始图像融合
Matwshed;
addWeighted(image,0.4,PerspectiveImage,0.6,,wshed);
imshow("AddWeightedImage",wshed);

waitKey();
}

Vec3bRandomColor(intvalue)
{
value=value%;//生成0~255的随机数
RNGrng;
intaa=rng.uniform(,value);
intbb=rng.uniform(,value);
intcc=rng.uniform(,value);
returnVec3b(aa,bb,cc);
}

分水岭分割算法(watershed segmentation)的C++实现(法2)的更多相关文章

  1. Matlab的标记分水岭分割算法

    1 综述 Separating touching objects in an image is one of the more difficult image processing operation ...

  2. [ZZ] 基于Matlab的标记分水岭分割算法

    基于Matlab的标记分水岭分割算法 http://blog.sina.com.cn/s/blog_725866260100rz7x.html 1 综述 Separating touching obj ...

  3. 基于Matlab的标记分水岭分割算法

    转自:http://blog.sina.com.cn/lyqmath 1 综述 Separating touching objects in an image is one of the more d ...

  4. 分水岭分割算法(watershed segmentation)的C++实现(法1)

    运行环境:ubuntu16.04+Qt+opencv2.4.13 参考链接:http://blog.csdn.net/u010741471/article/details/45193521 water ...

  5. 基于标记的分水岭分割算法/OpenCV中距离变换

    Opencv分水岭算法——watershed自动图像分割用法 OpenCV距离变换distanceTransform应用 图像分割作为图像识别的基础,在图像处理中占有重要地位,通常需要在进行图像分割算 ...

  6. Opencv分水岭算法——watershed自动图像分割用法

    分水岭算法是一种图像区域分割法,在分割的过程中,它会把跟临近像素间的相似性作为重要的参考依据,从而将在空间位置上相近并且灰度值相近的像素点互相连接起来构成一个封闭的轮廓,封闭性是分水岭算法的一个重要特 ...

  7. 从Random Walk谈到Bacterial foraging optimization algorithm(BFOA),再谈到Ramdom Walk Graph Segmentation图分割算法

    1. 从细菌的趋化性谈起 0x1:物质化学浓度梯度 类似于概率分布中概率密度的概念.在溶液中存在不同的浓度区域. 如放一颗糖在水盆里,糖慢慢溶于水,糖附近的水含糖量比远离糖的水含糖量要高,也就是糖附近 ...

  8. 三维网格分割算法(Random Walks)

    首先以一维随机游走(1D Random Walks)为例来介绍下随机游走(Random Walks)算法,如下图所示,从某点出发,随机向左右移动,向左和向右的概率相同,都为1/2,并且到达0点或N点则 ...

  9. VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法]

    VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法] - tingya的专栏 - 博客频道 - CSDN.NET VIPS:基于视觉的页面分割算法[微软下一代搜索引擎核心分页算法] 分类 ...

随机推荐

  1. zcash 的资料

    我的比特币使用的是 electrum 2.9.3 版本.我的zcash用的是 jaxx 1.2 版. zcash又叫zec,可以在 bithumb (韩国的) 平台上进行交易zcash. zcash ...

  2. spring boot security 登出

    <!DOCTYPE html> <html lang="zh-cn" xmlns:th="http://www.thymeleaf.org" ...

  3. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  4. 跟我学Makefile(二)

    命令出错: 每当命令运行完后, make 会检测每个命令的返回码,如果命令返回成功,那么 make 会执行下一条命令. 如果一个规则中的某个命令出错了(命令退出码非零),那么 make 就会终止执行当 ...

  5. 在线学习--online learning

    在线学习 online learning Online learning并不是一种模型,而是模型的训练方法.能够根据线上反馈数据,实时快速的进行模型调优,使得模型能够及时反映线上的变化,提高线上预测的 ...

  6. LCS 最长公共子序列

    区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...

  7. EditPlus 4.3.2463 中文版已经发布(10月16日更新)

    距离上个版本在本年5月发布后,EditPlus 网站沉寂多月.日前终于发布了一个新的小版本. 该版本却具有多项改进,值得一提: * Ctrl+Alt+Up/Down 键在列选模式下可插入多个插入点. ...

  8. MAC、MII、PHY的关系与区别

    嗯,实验室的嵌入式项目需要写设备驱动,我分到了网络驱动的活,写一个适配SylixOS的(这里夸一句,这个真是国内相当不错的嵌入式实时操作系统了)MPC8377的网卡驱动,说实话原来从来没接触过写驱动的 ...

  9. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  10. UVa 11489 整数游戏

    https://vjudge.net/problem/UVA-11489 题意: 给出一个数字串n,两个人轮流从中取出一个数字,要求每次取完之后剩下的数是3的倍数,不能取数者输. 思路: 要想取掉一个 ...