Matlab得到二值图像中最大连通区域
有时候要将二值化图像中最大的连通域保存下来。以下函数提供了一种方法:
%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得到二值图像中最大连通区域的更多相关文章
- 使用OpenCV查找二值图中最大连通区域
http://blog.csdn.net/shaoxiaohu1/article/details/40272875 使用OpenCV查找二值图中最大连通区域 标签: OpenCVfindCoutour ...
- [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), ...
- [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), ...
- [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), ...
- matlab函数_连通区域
1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下使用8邻域.算法:(1)De ...
- OpenCV:二值图像连通区域分析与标记算法实现
http://blog.csdn.net/cooelf/article/details/26581539?utm_source=tuicool&utm_medium=referral Open ...
- 【转】matlab函数_连通区域
转载自einyboy的博文Matlab的regionprops详解 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二 ...
- Opencv2系列学习笔记10(提取连通区域轮廓)
连通区域指的是二值图像中相连像素组成的形状.而内.外轮廓的概念及opencv1中如何提取二值图像的轮廓见我的这篇博客:http://blog.csdn.net/lu597203933/article/ ...
- Opencv2系列学习笔记10(提取连通区域轮廓) 另一个
http://blog.csdn.net/lu597203933/article/details/17362457 连通区域指的是二值图像中相连像素组成的形状.而内.外轮廓的概念及opencv1中如何 ...
随机推荐
- javascript异步下载 Promise实现
一般下载都是直接打开一个链接就行.var URL = 'XXXX';window.open(URL)其实这样会有些问题:1. 浏览器禁止打开新窗口,导致无法下载 那么怎么解决呢?这样: <a h ...
- Android RxJava1.X升级到RxJava2.X笔记
简书地址 http://www.jianshu.com/p/2badfbb3a33b 描述 RxJava 1.X RxJava 2.X package包名 rx.xxx io.reactivex.xx ...
- Python 快排[pythonnic]
def QS(array): less = [] more = [] if len(array) <= 1: return array head = array.pop() for x in a ...
- QT开发之旅-Udp聊天室编程
一.概要设计 登录对话框(继承自QDialog类)进行用户登录查询数据库用户是否存在,注册插入数据到用户表.用户表字段: (chatid int primary key, passwd varchar ...
- PHP——基本使用(一)
Apache安装与配置 install 下载地址:https://www.apachelounge.com/download/,选择2.4.33版本64位 将程序解压到一个英文目录下,以管理身份打开c ...
- 世界上最受欢迎的10个Linux发行版
帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...
- Vue简易博客总结
项目结构: 首先,编写博客的导航栏组件BlogHeader.vue: <template> <nav> <ul> <li> <router-lin ...
- Ceres
sudo apt-get install liblapack-dev libsuitesparse-dev libcxspares3.1.2 libgflags-dev libggoogle-glog ...
- ArrayAccess(数组式访问)
实现该接口后,可以像访问数组一样访问对象. 接口摘要: ArrayAccess { abstract public boolean offsetExists ( mixed $offset ) abs ...
- 模板—treap
#include<iostream> #include<cstdio> #include<cstdlib> #define INF 0x7fffffff using ...