使用MATLAB删除图像的特定彩色边缘部分

时间:2015-10-16 12:59:32

标签: matlab

如何使用MATLAB从图像边缘删除紫色部分,而不是从图像内部删除。请向我推荐一个通用代码,可以在任何类型的此类图像中使用。

主要图片

enter image description here

如果你没有得到要删除的部分我用红色标记了部分

enter image description here

1 个答案:

答案 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