用户A提问:上面的图像是RGB,现在想把中间红色封闭线内的图形剪切出来。一直想找一个能适应多种不规则这种红色边界的裁剪的算法,因为这个红色边界还比较简单,还有很多图像要裁剪的区域是不规则的红色边界。可惜一直没有找到,各位能不能提醒一下,看看有没有什么好的办法啊?
用户B回答:
img=imread('1.jpg');
HSI=rgb2hsi(img); %HSI转化
S=HSI(:,:,2); %提取S,找到红色的轮廓
bw=im2bw(S,graythresh(S)); %找到二值化后的轮廓
bw_fill=imfill(bw,'holes'); % 填充轮廓里面的区域
img2=rgb2gray(img);
img1=img2.*uint8(bw_fill);% 与轮廓位置相应的图片
%%
subplot(2,2,1),imshow(img);
subplot(2,2,2),imshow(S,[]);
subplot(2,2,3),imshow(bw_fill);
subplot(2,2,4),imshow(img1);
用户C回复B:
关于第2行: HSI = rgb2hsi(img); %找不到rgb2hsi这个函数
我猜是否应为: HSV = rgb2hsv(img);
用户B回复C:
其实我也不清楚hsi和hsv的分别,我们最终的目的只是找到s分量,应该是一样的。rgb2hsi.m这个函数是在网上找到的,忘记在哪里了,下面是代码。rgb2hsv是matlab自带的。
function hsi=rgb2hsi(rgb)
%RGB2HSI Converts an RGB image to HSI
% HSI=RGB2HSI(rgb) converts an RGB image to HSI. The input image is
% assumed to be of size M-by-N-by-3, where the third dimension accounts
% for three image planes:red, green, and blue, in that order. If all RGB
% component images are equal, the HSI conversion is undefined. Ths input
% image can be of class double (with values in the rang[0,1]), uint8, or
% uint16.
% The output image, HSI, is of class double, where:
% hsi(:,:,1)= hue image normalized values to the range [0,1] by
% dividing all angle values by 2*pi.
% hsi(:,:,2)=saturation image, in the range [0,1].
% hsi(:,:,3)=intensity image, in the range [0,1].
%Extract the individual component images.
rgb=im2double(rgb);
r=rgb(:,:,1);
g=rgb(:,:,2);
b=rgb(:,:,3);
%Implement the conversion equations.
num=0.5*((r-g)+(r-b));
den=sqrt((r-g).^2+(r-b).*(g-b));
theta=acos(num./(den+eps));
H=theta;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
num=min(min(r,g),b);
den=r+g+b;
den(den==0)=eps;
S=1-3.*num./den;
H(S==0)=0;
I=(r+g+b)/3;
%Combine all three results into an hsi image.
hsi=cat(3,H,S,I);