一、RGB彩色空間中實作影像分割
-
原影像

-
影像二值化實作影像分割

-
邊緣檢測演算法實作影像分割



二、HSV彩色空間實作影像分割
-
原影像

-
影像二值化實作影像分割

-
邊緣檢測演算法實作影像分割



三、YUV彩色空間實作影像分割
-
原影像

-
二值影像實作彩色分割

-
邊緣檢測演算法實作影像分割



四、實作代碼
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算子結合來實作不同的分割處理,實驗結果表明:
- RGB彩色空間中在分割之后可以更加凸顯出細節部分,但是會使輪廓變得模糊,
- HSV空間和YUV空間中可以更加明顯的影像處蟲眼的輪廓,但是YUV空間中失去的細節部分會比較嚴重,當蟲眼較小時會被直接忽略,
- 在三種彩色空間中使用canny算子進行分割的效果更好,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/179104.html
標籤:其他
上一篇:力扣每日一題(1)
