2015-10-16 207 views
1

如何從MATLAB的圖像邊緣去除圖像邊緣的紫色部分。請給我一個通用代碼,可以應用於任何類型的圖像。使用MATLAB刪除圖像的特定顏色邊緣部分

主影像

enter image description here

如果你沒有得到這部分是要刪除我用紅色標出的部分

enter image description here

+0

但是,事情是由紫色各地surounded。爲什麼只有那一個? –

+0

抱歉給您帶來不便。我必須從圖像中去除所有紫色環境,而不僅僅是紅色標記區域。我希望你明白。 –

回答

1

所以,這就是我隨同。我希望代碼是自我解釋的。

有2個值需要玩。其中之一是imdilate中的數字。那將會定義「邊界有多大」。當然這取決於你。

另一個是HSV顏色分割的值。在HSV中,H是顏色,而紫色在250-335範圍內。問題是藍色與紫色非常相似,紫色和藍色之間的界限非常模糊。我在代碼中使用了250作爲下限,但是您可能需要修改它。

如果您有任何問題,請詢問。

% The image is indexed image. Else convert. 
[img,c]=imread('https://i.imgur.com/dxkJSi0.png'); 

% Get part with color 
bwimg=img~=0; 

% get only the biggest part 
lblimg=bwlabel(bwimg,4); 
stat = regionprops(lblimg,'Centroid','Area','PixelIdxList'); 
[maxValue,index] = max([stat.Area]); 

todelete=1:size(stat,1); 
todelete(index)=[]; 
for ii=todelete 
    bwimg(stat(ii).PixelIdxList)=0; 
end 

%update image with without "noise" 
img(~bwimg)=0; 

% get the contour of the image (thanks @rayryeng) 
er = imerode(bwimg, strel('square', 3)); 
out = imsubtract(bwimg, er); 

% we will increase the boundary so we pick a larger region 
% Here you need your input. it depedns how much you dilate the image, the 
% part of the of the image that will be considered boudnary will increase. 
boundary=imdilate(out,strel('square', 10)); 

% now lets see withc colors are purple. For that we get HSV space. Shades 
% of purple are aruond 265~335 H 

hsvc=rgb2hsv(c); 
purple=find(hsvc(:,1)>250/360&hsvc(:,1)<335/360); 


% Get purple in the whole image 
purpleimg=zeros(size(img)); 
for ii=1:size(purple) 
    purpleimg(img==purple(ii))=purple(ii); 
end 
% get locations of purple in the boudnary 
purpbound=purpleimg.*boundary~=0; 
% delete them from the original image 
imgNOpurple=img; 
imgNOpurple(purpbound)=0; 

% plot results 

figure(1) 
subplot(221) 

imshow(purpleimg,c) 
title('purple in the image') 
subplot(222) 
imshow(purpleimg.*boundary,c); 
title('purple boundary') 

subplot(223) 
imshow(img,c) 
title('original image') 

subplot(224) 
imshow(imgNOpurple,c); 
title('Image without purple boundary') 

enter image description here

enter image description here

+0

非常感謝。這非常有幫助。 –

+0

你能幫我解決另一個問題嗎?我有100多個這種類型的圖像。我想以* .tif格式將所有最終圖像(沒有紫色邊界的圖像)保存在我的計算機文件夾中。我怎樣才能做到這一點?謝謝 –

+0

@jonsnowknowsnothing如果你有另一個問題要問另一個問題;)。因此,這個答案在這裏已經有好幾次了,所以googleingWill會幫助你! –