MATLAB实现图像分割(木材虫眼)

一、RGB彩色空间中实现图像分割

  1. 原图像
    在这里插入图片描述

  2. 图像二值化实现图像分割
    在这里插入图片描述

  3. 边缘检测算法实现图像分割
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

二、HSV彩色空间实现图像分割

  1. 原图像
    在这里插入图片描述

  2. 图像二值化实现图像分割
    在这里插入图片描述

  3. 边缘检测算法实现图像分割
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

三、YUV彩色空间实现图像分割

  1. 原图像
    在这里插入图片描述

  2. 二值图像实现彩色分割
    在这里插入图片描述

  3. 边缘检测算法实现图像分割

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

四、实现代码

clc;
clear;
close all
picture = input('请输入需要分割的图片:');
Path = 'C:\Users\SHINELON\Desktop\木材表面虫眼分割考核材料\木材表面虫眼分割提取--m文件和图像数据'; 
File = dir(fullfile(Path,'*.JPG')); 
imageNames = {
    
    File.name}';
 if picture>=1&picture<=10 
 if picture~=10
% H:颜色相位(色相)         Y:表示明亮度(灰阶值)
% S:颜色饱和度              U:
% V:颜色亮度                 V:
     RGB=imread(fullfile(Path,imageNames{
    
    picture}));%得到RGB彩色空间图像
     HSV=rgb2hsv(RGB);%得到HSV彩色空间图像
     pr = RGB(:,:,1);
     pg = RGB(:,:,2);
     pb = RGB(:,:,3);
     Y = 0.299*pr + 0.587*pg + 0.114*pb;
     U = -0.147*pr - 0.289 *pg+ 0.436*pb;
     V = 0.615*pr - 0.515*pg- 0.100*pb;
     YUV= cat(3,Y,U,V);%得到YUV彩色空间图像
     
    
     bw_rgb = im2bw(rgb2gray(RGB),0.21);%rgb图像灰度转换,二值化
     bw_hsv= im2bw(medfilt2(HSV(:,:,3)),0.44);%提取hsv中的v分量,进行中值滤波,二值化
     bw_yuv=im2bw(medfilt2(YUV(:,:,3)),0.1);%提取ysv中的v分量,进行中值滤波,二值化
     
     figure();
     subplot(131);imshow(RGB);title('RGB图像');
     subplot(132);imshow(HSV);title('HSV图像');
     subplot(133);imshow(YUV);title('YUV图像');
     
     figure();
     subplot(131);imshow(bw_rgb);title('bw+rgb');
     subplot(132);imshow(bw_hsv);title('bw+hsv');
     subplot(133);imshow(bw_yuv);title('bw+yuv');
     
     figure();
     subplot(131);imshow(edge(bw_rgb,'prewitt'));title('rgb+prewitt');
     subplot(132);imshow(edge(bw_hsv,'prewitt'));title('hsv+prewitt');
     subplot(133);imshow(edge(bw_yuv,'prewitt'));title('yuv+prewitt');
    
     figure();
     subplot(131);imshow(edge(bw_rgb,'sobel'));title('rgb+sobel');
     subplot(132);imshow(edge(bw_hsv,'sobel'));title('hsv+sobel');
     subplot(133);imshow(edge(bw_yuv,'sobel'));title('yuv+sobel');
     
     figure();
     subplot(131);imshow(edge(bw_rgb,'canny'));title('rgb+canny');
     subplot(132);imshow(edge(bw_hsv,'canny'));title('hsv+canny');
     subplot(133);imshow(edge(bw_yuv,'canny'));title('yuv+canny');
 else
for N =1:length(imageNames)
     RGB=imread(fullfile(Path,imageNames{
    
    N}));
     HSV=rgb2hsv(RGB);%得到HSV彩色空间图像
     pr = RGB(:,:,1);
     pg = RGB(:,:,2);
     pb = RGB(:,:,3);
     Y = 0.299*pr + 0.587*pg + 0.114*pb;
     U = -0.147*pr - 0.289 *pg+ 0.436*pb;
     V = 0.615*pr - 0.515*pg- 0.100*pb;
     YUV= cat(3,Y,U,V);%得到YUV彩色空间图像
     
     bw_rgb = im2bw(rgb2gray(RGB),0.21);%rgb图像灰度转换,二值化
     bw_hsv= im2bw(medfilt2(HSV(:,:,3)),0.44);%提取hsv中的v分量,进行中值滤波,二值化
     bw_yuv=im2bw(medfilt2(YUV(:,:,3)),0.1);%提取ysv中的v分量,进行中值滤波,二值化
     %显示RGB,HSV,YUV彩色空间的图像
     figure();
     subplot(131);imshow(RGB);title('RGB图像');
     subplot(132);imshow(HSV);title('HSV图像');
     subplot(133);imshow(YUV);title('YUV图像');
     %显示RGB,HSV,YUV彩色空间的二值图像
     figure();
     subplot(131);imshow(bw_rgb);title('bw+rgb');
     subplot(132);imshow(bw_hsv);title('bw+hsv');
     subplot(133);imshow(bw_yuv);title('bw+yuv');
     %显示使用prewitt算子进行边缘检测后的图像
     figure();
     subplot(131);imshow(edge(bw_rgb,'prewitt'));title('rgb+prewitt');
     subplot(132);imshow(edge(bw_hsv,'prewitt'));title('hsv+prewitt');
     subplot(133);imshow(edge(bw_yuv,'prewitt'));title('yuv+prewitt');
    %显示使用sobel算子进行边缘检测之后的图像 
     figure();
     subplot(131);imshow(edge(bw_rgb,'sobel'));title('rgb+sobel');
     subplot(132);imshow(edge(bw_hsv,'sobel'));title('hsv+sobel');
     subplot(133);imshow(edge(bw_yuv,'sobel'));title('yuv+sobel');
     %显示使用canny算子进行边缘检测之后的图像
     figure();
     subplot(131);imshow(edge(bw_rgb,'canny'));title('rgb+canny');
     subplot(132);imshow(edge(bw_hsv,'canny'));title('hsv+canny');
     subplot(133);imshow(edge(bw_yuv,'canny'));title('yuv+canny');
end
 end
 else 
     disp('输入错误,最大图片数量为:');
     disp(length(imageNames));
 end

五、结语

在分割过程中使用了RGB、HSV和YUV彩色空间、滤波器和prewitt、sobel和canny算子结合来实现不同的分割处理。实验结果表明:

  1. RGB彩色空间中在分割之后可以更加凸显出细节部分,但是会使轮廓变得模糊。
  2. HSV空间和YUV空间中可以更加明显的图像处虫眼的轮廓,但是YUV空间中失去的细节部分会比较严重,当虫眼较小时会被直接忽略。
  3. 在三种彩色空间中使用canny算子进行分割的效果更好。

猜你喜欢

转载自blog.csdn.net/weixin_44223883/article/details/109114451