有时候要将二值化图像中最大的连通域保存下来。以下函数提供了一种方法:

%function [img]=maxLianTongYu(I):求图像中最大的连通域
%输入:I 输入图像
%输出:img 仅包括最大连通域的图像
function [img]=maxLianTongYu(I)
if length(size(I))>2
I = rgb2gray(I);
end
if ~islogical(I)
imBw = im2bw(I); %转换为二值化图像
else
imBw = I;
end
imBw = im2bw(I); %转换为二值化图像
imLabel = bwlabel(imBw); %对各连通域进行标记
stats = regionprops(imLabel,'Area'); %求各连通域的大小
area = cat(1,stats.Area);
index = find(area == max(area)); %求最大连通域的索引
img = ismember(imLabel,index); %获取最大连通域图像

原图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3psY3cx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

执行:

<pre name="code" class="plain">img=maxLianTongYu(I);
imshow(img);
</pre>

得到:

假设返回图像中连通域大小大于阈值的连通域。可用matlab自带函数:BW2 = bwareaopen(BW, P)

或:

%function [img]=chooseLianTongYu(I,thres):  返回图像中连通域大小大于阈值的连通域
%输入:I 输入图像 thres 阈值
%输出:img 图像中连通域大小大于阈值的连通域
function [img]=chooseLianTongYu(I,thres)
if length(size(I))>2
I = rgb2gray(I);
end
if ~islogical(I)
imBw = im2bw(I); %转换为二值化图像
else
imBw = I;
end
imLabel = bwlabel(imBw); %对各连通域进行标记
stats = regionprops(imLabel,'Area'); %求各连通域的大小
area = cat(1,stats.Area);
index = find(area > thres); %求连通域大小大于阈值的索引
img = ismember(imLabel,index(:)); %获取指定连通域

Matlab得到二值图像中最大连通区域的更多相关文章

  1. 使用OpenCV查找二值图中最大连通区域

    http://blog.csdn.net/shaoxiaohu1/article/details/40272875 使用OpenCV查找二值图中最大连通区域 标签: OpenCVfindCoutour ...

  2. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  3. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  5. matlab函数_连通区域

    1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下使用8邻域.算法:(1)De ...

  6. OpenCV:二值图像连通区域分析与标记算法实现

    http://blog.csdn.net/cooelf/article/details/26581539?utm_source=tuicool&utm_medium=referral Open ...

  7. 【转】matlab函数_连通区域

    转载自einyboy的博文Matlab的regionprops详解 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二 ...

  8. Opencv2系列学习笔记10(提取连通区域轮廓)

    连通区域指的是二值图像中相连像素组成的形状.而内.外轮廓的概念及opencv1中如何提取二值图像的轮廓见我的这篇博客:http://blog.csdn.net/lu597203933/article/ ...

  9. Opencv2系列学习笔记10(提取连通区域轮廓) 另一个

    http://blog.csdn.net/lu597203933/article/details/17362457 连通区域指的是二值图像中相连像素组成的形状.而内.外轮廓的概念及opencv1中如何 ...

随机推荐

  1. Microsoft SQL Server学习(三)

    1.表:表示一个实体(客观存在的事物或抽象时间),可实现对实体的数据描述和数据操作. 2.表结构:二位平面(行.列) 3.数据类型: 类型名称 类型 整形 bit(只存储0.1) samllint i ...

  2. 安装好Pycharm后如何配置Python解释器简易教程

    呃呃,遇到坑了...... 安装完Python,没有去配置好Python解释器,直接打开Python项目包,去运行程序,程序输出结果只是显示 Process finished with exit co ...

  3. codeforces_333B_水过

    B. Chips time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. 网络编程基础_3.APC队列

    APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...

  5. idea之快速查看类所在jar包

  6. css3--伪元素和伪类

    1,定义 W3C定义:伪元素伪类 都可以向某些选择器设置特殊效果.(css2中定义) css3中的定义: 1).伪元素:在DOM树中创建了一些抽象元素(虚拟的容器).由两个冒号::开头(css2中并没 ...

  7. Linux之iptables(五、firewall命令及配置)

    firewalld服务 firewalld是CentOS 7.0新推出的管理netfilter的工具 firewalld是配置和监控防火墙规则的系统守护进程.可以实现iptables,ip6table ...

  8. iframe子页面操作父页面并实现屏蔽页面弹出层效果

  9. C语言结构体用法

    结构体的定义: 方法一: struct student { char name[10]; int age; int number; }; struct student stu1; 方法二: struc ...

  10. 实验1“C语言开发环境使用和数据类型、运算符、表达式”总结与体会

    一.实验结论 1.判断奇偶 // 程序功能: // 要求用户从键盘输入一个整数,判断其是奇数还是偶数 #include <stdio.h> int main() { int x; prin ...