2015-10-16 253 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会帮助你! –