基于颜色的图像分割
2018-03-26 17:20
453 查看
在实际处理图像时,经常需要对图像进行分割,然后提取ROI,本学习笔记记录怎么用Matlab实现基于颜色的图像分割。
1、RGB到YCBCR的色彩空间转换。2、用各个通道的阈值对图像进行二值化。3、形态学处理:腐蚀、膨胀、孔洞填充。4、连通区域提取。主要涉及到的Matla图形处理函数如下:rgb2ycbcr(色彩空间转换),roicolor(ROI二值化),imerode(腐蚀),imdilate(膨胀),imfill(孔洞填充),regionprops(区域属性)。这些函数的具体实现还是参考冈萨雷斯的《数字图像处理》,及matlab帮助文档,及论文引用。不多说了,具体看代码,每一步都有注释.%清空变量,读取图像
clear;close all
RGB = imread('images/girl.jpg');
figure('name','process'),
subplot(2,2,1),imshow(RGB),title('原始RGB'),
%convert frame from RGB to YCBCR colorspace(转换到YCBCR空间)
YCBCR = rgb2ycbcr(RGB);
whos,
subplot(2,2,2),imshow(YCBCR),title('YCBCR'),
%filter YCBCR image between values and store filtered image to threshold
%matrix(用各个通道的阈值对其进行二值化处理)
Y_MIN = 0; Y_MAX = 256;
Cb_MIN = 100; Cb_MAX = 127;
Cr_MIN = 138; Cr_MAX = 170;
threshold=roicolor(YCBCR(:,:,1),Y_MIN,Y_MAX)&roicolor(YCBCR(:,:,2),Cb_MIN,Cb_MAX)&roicolor(YCBCR(:,:,3),Cr_MIN,Cr_MAX);
subplot(2,2,3),imshow(threshold),title('YCBCR二值化'),
%perform morphological operations on thresholded image to eliminate noise
%and emphasize the filtered object(s)(进行形态学处理:腐蚀、膨胀、孔洞填充)
erodeElement = strel('square', 3) ;
dilateElement=strel('square', 8) ;
threshold = imerode(threshold,erodeElement);
threshold = imerode(threshold,erodeElement);
threshold=imdilate(threshold, dilateElement);
threshold=imdilate(threshold, dilateElement);
threshold=imfill(threshold,'holes');
subplot(2,2,4),imshow(threshold),title('形态学处理'),
%获取区域的'basic'属性, 'Area', 'Centroid', and 'BoundingBox'
figure('name','处理结果'),
stats = regionprops(threshold, 'basic');
[C,area_index]=max([stats.Area]);
%定位脸部区域
face_locate=[stats(area_index).Centroid(1),stats(area_index).Centroid(2)];
imshow(RGB);title('after'),hold on
text(face_locate(1),face_locate(2)-40, 'face','color','red');
plot(face_locate(1),face_locate(2), 'b*');
rectangle('Position',[stats(area_index).BoundingBox],'LineWidth',2,'LineStyle','--','EdgeColor','r'),
hold off
运行之后的效果如下图:
1、RGB到YCBCR的色彩空间转换。2、用各个通道的阈值对图像进行二值化。3、形态学处理:腐蚀、膨胀、孔洞填充。4、连通区域提取。主要涉及到的Matla图形处理函数如下:rgb2ycbcr(色彩空间转换),roicolor(ROI二值化),imerode(腐蚀),imdilate(膨胀),imfill(孔洞填充),regionprops(区域属性)。这些函数的具体实现还是参考冈萨雷斯的《数字图像处理》,及matlab帮助文档,及论文引用。不多说了,具体看代码,每一步都有注释.%清空变量,读取图像
clear;close all
RGB = imread('images/girl.jpg');
figure('name','process'),
subplot(2,2,1),imshow(RGB),title('原始RGB'),
%convert frame from RGB to YCBCR colorspace(转换到YCBCR空间)
YCBCR = rgb2ycbcr(RGB);
whos,
subplot(2,2,2),imshow(YCBCR),title('YCBCR'),
%filter YCBCR image between values and store filtered image to threshold
%matrix(用各个通道的阈值对其进行二值化处理)
Y_MIN = 0; Y_MAX = 256;
Cb_MIN = 100; Cb_MAX = 127;
Cr_MIN = 138; Cr_MAX = 170;
threshold=roicolor(YCBCR(:,:,1),Y_MIN,Y_MAX)&roicolor(YCBCR(:,:,2),Cb_MIN,Cb_MAX)&roicolor(YCBCR(:,:,3),Cr_MIN,Cr_MAX);
subplot(2,2,3),imshow(threshold),title('YCBCR二值化'),
%perform morphological operations on thresholded image to eliminate noise
%and emphasize the filtered object(s)(进行形态学处理:腐蚀、膨胀、孔洞填充)
erodeElement = strel('square', 3) ;
dilateElement=strel('square', 8) ;
threshold = imerode(threshold,erodeElement);
threshold = imerode(threshold,erodeElement);
threshold=imdilate(threshold, dilateElement);
threshold=imdilate(threshold, dilateElement);
threshold=imfill(threshold,'holes');
subplot(2,2,4),imshow(threshold),title('形态学处理'),
%获取区域的'basic'属性, 'Area', 'Centroid', and 'BoundingBox'
figure('name','处理结果'),
stats = regionprops(threshold, 'basic');
[C,area_index]=max([stats.Area]);
%定位脸部区域
face_locate=[stats(area_index).Centroid(1),stats(area_index).Centroid(2)];
imshow(RGB);title('after'),hold on
text(face_locate(1),face_locate(2)-40, 'face','color','red');
plot(face_locate(1),face_locate(2), 'b*');
rectangle('Position',[stats(area_index).BoundingBox],'LineWidth',2,'LineStyle','--','EdgeColor','r'),
hold off
运行之后的效果如下图:
相关文章推荐
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- 爬山法处理基于颜色的图像分割
- 基于颜色信息的图像分割算法
- 【图像算法】彩色图像分割专题六:一种基于颜色直方图的图像分割
- 图像处理学习笔记(二):基于颜色的图像分割
- 基于颜色分量HSV的图像分割:基于opencv的分割调试工具源码
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- (学习笔记)基于opencv颜色过滤只保留红色区域(适用于图像分割方面)
- Matlab图像处理学习笔记(二):基于颜色的图像分割
- 冈萨雷斯数字图像处理学习7:图像分割 基于区域的分割分水岭
- 基于聚类的“图像分割”实例编写
- 基于投影点密度的车载激光扫描距离图像分割方法
- 基于颜色特征的图像识别
- 基于分割的图像恢复-A New Approach
- 【图像算法】彩色图像分割专题一:颜色空间1(原理)
- 使用颜色空间进行图像分割
- [置顶] 【译】DeepLab V2:基于深度卷积网、孔洞算法和全连接CRFs的语义图像分割
- GrabCut in One Cut(基于图割算法grabcut的一次快速图像分割的OpenCV实现)----目前效果最好的图割
- 一种基于局部最小代价分水岭变换的图像分割新方法 阅读笔记
- 基于PCNN的图像分割的matlab实现