运行环境: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. 204-React DOM 元素

    一.概述 为了提高性能和跨浏览器兼容性,React实现了一个独立于浏览器的DOM系统. 在React中,所有DOM属性和属性(包括事件处理程序)都应该是camelCased的.例如,HTML属性tab ...

  2. Spark源码分析之Checkpoint的过程

    概述 checkpoint 的机制保证了需要访问重复数据的应用 Spark 的DAG执行图可能很庞大,task 中计算链可能会很长,这时如果 task 中途运行出错,那么 task 的整个需要重算非常 ...

  3. [py][mx]django实现课程机构排名

    如果是第一次做这个玩意,说实话,确实不知道怎么弄, 做一次后就有感觉了 此前我们已经完成了: 分类筛选 分页 这次我们做的是 课程机构排名 知识点: - 按照点击数从大到小排名, 取出前三名 hot_ ...

  4. 使用Ajax向服务器端发送请求

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. redis客户端hiredis

    Hiredis 在官网 http://redis.io/clients 中有说明This is the official C client. Support for the whole command ...

  6. java中string.trim()函数的作用

    trim  /[trɪm] / 英文意思:整理,修理,修剪,整齐的 trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ Strin ...

  7. OpenResty--mysql,redis 项目中的应用

    最近刚刚接手同事的OpenResty的项目,发现对mysql,redis的操作没有用连接池,故对此进行了改造. MYSQL 主要是通过mysql_pool.lua 和 dbutil.lua 来封装对数 ...

  8. Linux proc 内存

    ps: USER      PID    %CPU %MEM   VSZ   RSS  TTY  STAT  START  TIME  COMMAND root          4238     0 ...

  9. kylin与superset整合

    前提: kylin安装以及配置可以参考 https://www.cnblogs.com/654wangzai321/p/9676204.html 我这边用的Linux自带的python2.7,为了保证 ...

  10. pyDay10

    内容来自廖雪峰的官方网站. 1.python的赋值语句:a, b, c = x, y, z 相当于 a = x, b = y, c = z.(事实上等式右边是一个tuple) 2.获得genarato ...