matlab距离变换,距离变换的图像分割和Watershed算法
目标
在本教程中,您将学习如何:使用OpenCV函数cv :: filter2D为了执行一些拉普拉斯滤波来进行图像锐化
使用OpenCV函数cv :: distanceTransform来获得二进制图像的导出表示,其中每个像素的值被替换为最近的背景像素的距离
使用OpenCV函数cv :: watershed来隔离图像中的对象与背景
Code
本教程代码如下所示。您也可以从这里下载。#include
#include
using namespace std;
using namespace cv;
int main()
{
// Load the image
Mat src = imread("../data/cards.png");
// Check if everything was fine
if (!src.data)
return -1;
// Show source image
imshow("Source Image", src);
// Change the background from white to black, since that will help later to extract
// better results during the use of Distance Transform
for( int x = 0; x < src.rows; x++ ) {
for( int y = 0; y < src.cols; y++ ) {
if ( src.at(x, y) == Vec3b(255,255,255) ) {
src.at(x, y)[0] = 0;
src.at(x, y)[1] = 0;
src.at(x, y)[2] = 0;
}
}
}
// Show output image
imshow("Black Background Image", src);
// Create a kernel that we will use for accuting/sharpening our image
Mat kernel = (Mat_(3,3) <<
1, 1, 1,
1, -8, 1,
1, 1, 1); // an approximation of second derivative, a quite strong kernel
// do the laplacian filtering as it is
// well, we need to convert everything in something more deeper then CV_8U
// because the kernel has some negative values,
// and we can expect in general to have a Laplacian image with negative values
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
// so the possible negative number will be truncated
Mat imgLaplacian;
Mat sharp = src; // copy source image to another temporary one
filter2D(sharp, imgLaplacian, CV_32F, kernel);
src.convertTo(sharp, CV_32F);
Mat imgResult = sharp - imgLaplacian;
// convert back to 8bits gray scale
imgResult.convertTo(imgResult, CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );
imshow( "New Sharped Image", imgResult );
src = imgResult; // copy back
// Create binary image from source image
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
imshow("Binary Image", bw);
// Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, CV_DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1., NORM_MINMAX);
imshow("Distance Transform Image", dist);
// Threshold to obtain the peaks
// This will be the markers for the foreground objects
threshold(dist, dist, .4, 1., CV_THRESH_BINARY);
// Dilate a bit the dist image
Mat kernel1 = Mat::ones(3, 3, CV_8UC1);
dilate(dist, dist, kernel1);
imshow("Peaks", dist);
// Create the CV_8U version of the distance image
// It is needed for findContours()
Mat dist_8u;
dist.convertTo(dist_8u, CV_8U);
// Find total markers
vector > contours;
findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// Create the marker image for the watershed algorithm
Mat markers = Mat::zeros(dist.size(), CV_32SC1);
// Draw the foreground markers
for (size_t i = 0; i < contours.size(); i++)
drawContours(markers, contours, static_cast(i), Scalar::all(static_cast(i)+1), -1);
// Draw the background marker
circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1);
imshow("Markers", markers*10000);
// Perform the watershed algorithm
watershed(src, markers);
Mat mark = Mat::zeros(markers.size(), CV_8UC1);
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
// image looks like at that point
// Generate random colors
vector colors;
for (size_t i = 0; i < contours.size(); i++)
{
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
// Create the result image
Mat dst = Mat::zeros(markers.size(), CV_8UC3);
// Fill labeled objects with random colors
for (int i = 0; i < markers.rows; i++)
{
for (int j = 0; j < markers.cols; j++)
{
int index = markers.at(i,j);
if (index > 0 && index <= static_cast(contours.size()))
dst.at(i,j) = colors[index-1];
else
dst.at(i,j) = Vec3b(0,0,0);
}
}
// Visualize the final image
imshow("Final Result", dst);
waitKey(0);
return 0;
}
说明/结果加载源图像并检查是否加载没有任何问题,然后显示:// Load the image
Mat src = imread("../data/cards.png");
// Check if everything was fine
if (!src.data)
return -1;
// Show source image
imshow("Source Image", src);
那么如果我们有一个有白色背景的图像,那么将它变成黑色是很好的。这将有助于我们在应用距离变换时更容易地描绘前景对象:// Change the background from white to black, since that will help later to extract
// better results during the use of Distance Transform
for( int x = 0; x < src.rows; x++ ) {
for( int y = 0; y < src.cols; y++ ) {
if ( src.at(x, y) == Vec3b(255,255,255) ) {
src.at(x, y)[0] = 0;
src.at(x, y)[1] = 0;
src.at(x, y)[2] = 0;
}
}
}
// Show output image
imshow("Black Background Image", src);
之后,我们将锐化我们的形象,以锐化前景对象的边缘。我们将应用一个具有相当强的滤波器的拉普拉斯滤波器(二阶导数近似):// Create a kernel that we will use for accuting/sharpening our image
Mat kernel = (Mat_(3,3) <<
1, 1, 1,
1, -8, 1,
1, 1, 1); // an approximation of second derivative, a quite strong kernel
// do the laplacian filtering as it is
// well, we need to convert everything in something more deeper then CV_8U
// because the kernel has some negative values,
// and we can expect in general to have a Laplacian image with negative values
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
// so the possible negative number will be truncated
Mat imgLaplacian;
Mat sharp = src; // copy source image to another temporary one
filter2D(sharp, imgLaplacian, CV_32F, kernel);
src.convertTo(sharp, CV_32F);
Mat imgResult = sharp - imgLaplacian;
// convert back to 8bits gray scale
imgResult.convertTo(imgResult, CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );
imshow( "New Sharped Image", imgResult );
现在我们分别从我们的新锐的源图像转换为灰度和二进制图像:// Create binary image from source image
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
imshow("Binary Image", bw);
我们准备好在二进制图像上应用Distance Tranform。此外,我们规范化输出图像,以便能够可视化和阈值结果:// Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, CV_DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1., NORM_MINMAX);
imshow("Distance Transform Image", dist);
我们阈值dist图像,然后执行一些形态学操作(即扩张),以从上述图像中提取峰值:// Threshold to obtain the peaks
// This will be the markers for the foreground objects
threshold(dist, dist, .4, 1., CV_THRESH_BINARY);
// Dilate a bit the dist image
Mat kernel1 = Mat::ones(3, 3, CV_8UC1);
dilate(dist, dist, kernel1);
imshow("Peaks", dist);
从每个blob,然后我们在cv :: findContours函数的帮助下创建一个watershed算法的种子/标记:// Create the CV_8U version of the distance image
// It is needed for findContours()
Mat dist_8u;
dist.convertTo(dist_8u, CV_8U);
// Find total markers
vector > contours;
findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// Create the marker image for the watershed algorithm
Mat markers = Mat::zeros(dist.size(), CV_32SC1);
// Draw the foreground markers
for (size_t i = 0; i < contours.size(); i++)
drawContours(markers, contours, static_cast(i), Scalar::all(static_cast(i)+1), -1);
// Draw the background marker
circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1);
imshow("Markers", markers*10000);
最后,我们可以应用watershed算法,并可视化结果:// Perform the watershed algorithm
watershed(src, markers);
Mat mark = Mat::zeros(markers.size(), CV_8UC1);
markers.convertTo(mark, CV_8UC1);
bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark
// image looks like at that point
// Generate random colors
vector colors;
for (size_t i = 0; i < contours.size(); i++)
{
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
// Create the result image
Mat dst = Mat::zeros(markers.size(), CV_8UC3);
// Fill labeled objects with random colors
for (int i = 0; i < markers.rows; i++)
{
for (int j = 0; j < markers.cols; j++)
{
int index = markers.at(i,j);
if (index > 0 && index <= static_cast(contours.size()))
dst.at(i,j) = colors[index-1];
else
dst.at(i,j) = Vec3b(0,0,0);
}
}
// Visualize the final image
imshow("Final Result", dst);
matlab距离变换,距离变换的图像分割和Watershed算法相关推荐
- python图像分割算法_OpenCV-Python 图像分割与Watershed算法 | 三十四
目标 在本章中, 我们将学习使用分水岭算法实现基于标记的图像分割 我们将看到:cv.watershed() 理论 任何灰度图像都可以看作是一个地形表面,其中高强度表示山峰,低强度表示山谷.你开始用不同 ...
- python图像分割_OpenCV-Python系列之图像分割与Watershed算法
本次我们来看图像分割,同样也是OpenCV中较为重要的一个部分.图像分割是按照一定的原则,将一幅图像分为若干个互不相交的小局域的过程,它是图像处理中最为基础的研究领域之一.目前有很多图像分割方法,其中 ...
- OpenCV系列之图像分割与Watershed算法 | 三十四
目标 在本章中, 我们将学习使用分水岭算法实现基于标记的图像分割 我们将看到:cv.watershed() 理论 任何灰度图像都可以看作是一个地形表面,其中高强度表示山峰,低强度表示山谷.你开始用不同 ...
- Opencv图像分割与Watershed算法
文章目录 1 理论 2 测试图像 3 寻找硬币的近似估计 4 去除图像中的白色噪点 5 分水岭 1 理论 任何灰度图像都可以看作是一个地形表面,其中高强度表示山峰,低强度表示山谷.当用不同颜色的水 ...
- 【matlab 图像处理】离散傅里叶变换离散余弦变换K-L变换小波变换
[matlab 图像处理]离散傅里叶变换&离散余弦变换&K-L变换&小波变换 正交变换是信号处理的一种有效工具.图像信号不仅可以在空间域表示,也可以在频域表示,后者将有利于许多 ...
- 为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换?
https://www.douban.com/note/164400821/ 写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,绝大部分内容非我所原创.在此向多位原创作者致敬!!! ...
- z变换的零极点图matlab,实验三 Z变换零极点分布及部分分式展开的MATLAB实现.doc...
实验三 Z变换零极点分布及部分分式展开的MATLAB实现 实验三 Z变换零极点分布及部分分式展开的MATLAB实现 一.仿真实验目的 1.学会运用MATLAB分析离散时间系统的系统函数的零极点分布与时 ...
- 【转】为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换?...
写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,绝大部分内容非我所原创.在此向多位原创作者致敬!!!一.傅立叶变换的由来关于傅立叶变换,无论是书本还是在网上可以很容易找到关于傅立叶 ...
- 为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换
[纯技术帖]为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换来源: 陈诚--WECN的日志 写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,内容 ...
最新文章
- 电子科大计算机组成原理ppt,电子科技大学,计算机组成原理3计算机组成原理-3-5-组合逻辑控制方式.ppt...
- electron 剪贴板 截图_用electron开发了一个屏幕截图工具
- python语言入门m-python基础入门这一篇就够
- 四篇NeurIPS 2019论文,快手特效中的模型压缩了解一下
- .net pdf转图片_pdf2image类库实现批量pdf转图片
- webpack4打包工具
- 华为root工具_华为手机EMUI9 ROOT通用操作方法
- axios请求跨域前端解决_前端跨域请求axios里面withCredentials: true
- 【Spring Boot】Spring Boot之整合RabbitMQ并实现消息的发送和接收
- java 修改win7系统时间_win7如何禁止更改系统时间
- 【资源】图深度学习文献列表
- 使用Object.prototype.toString判断数据类型
- 【ADMM】ADMM Gap
- Sketch54 for mac汉化破解版
- Word XP 中目录的编制方法,Word XP 功能键使用大全
- 计算机科学二审需要多久,寒冬展风采,文体两开花——记计算机科学学院迎新晚会节目二审暨素美部“计体两开花”品牌活动...
- 恺撒密码算法 Java实现
- XXX is not defined.eslint no-undef
- instr,left,mid 定位、取内容(字符串)函数
- 在oracle 中编写一个程序,用VC 开 发 基 于ORACLE 数 据 库 应 用 程 序 的 两 种 方 法...