【问题标题】:how to split image and fill different color如何分割图像并填充不同的颜色
【发布时间】:2017-02-28 16:01:51
【问题描述】:

如这张图,如何用matlab代码将其分割成不同的部分,然后填充颜色呢?另外,如何在第二个代码中设置渐变色???

图片分割代码如下:

clc
rgb=imread('sample1.bmp');
bw=im2bw(rgb2gray(rgb),.8);
bw=medfilt2(bw);
planes=bwareaopen(bw,800);
D=mat2gray(bwdist(imcomplement(planes)));
stats=regionprops(D>.8,'Centroid');
planes_centroid=cat(1,stats.Centroid);
planes_mask=false(size(bw));     
planes_mask(sub2ind(size(bw),round(planes_centroid(:,2)),...
        round(planes_centroid(:,1))))=1;
M=imimposemin(imcomplement(D),planes_mask);
L=watershed(M);
r=L & planes;
stats=regionprops(r,'BoundingBox','Centroid')
bb=cat(1,stats.BoundingBox);
c=cat(1,stats.Centroid);
figure,imshow(planes)
hold on
for i=1:length(stats)
   rectangle('Position',bb(i,:),'EdgeColor','b')
   plot(c(i,1),c(i,2),'r*')
   text(c(i,1)-5,c(i,2)-10,num2str(i))
end
%second code
clc;clf;close all;clear all;
color=cell(4,1);
for i=1:4
input=imread(['heartspline2_4_',num2str(i)],'bmp');  
figure,imshow(input);
BW=im2bw(input,graythresh(input));
[B,L]=bwboundaries(BW,'noholes');
for k=1:length(B)
   boundary=B{k};
   ind=size(boundary(:,2));
   plot(boundary(:,2),boundary(:,1),'k','LineWidth',2);
   hold on;
   axis off;
   if (k==1)
       patch(boundary(:,2),boundary(:,1),'w');
   else
       patch(boundary(:,2),boundary(:,1),???);
   end
end
saveas(gca,['y_','heartspline2_4_',num2str(i)],'bmp')
close(gcf)
end

【问题讨论】:

标签: matlab image-processing computer-vision image-segmentation


【解决方案1】:

您可以使用bwlabel 为每个图像区域分配不同的索引:

img = imread('http://i.stack.imgur.com/F1Iya.jpg');  %// read image
bw = img(:,:,1) > 128;  %// convert to binary mask
lb = bwlabel(bw,4);  %// extract distinct regions

结果:

figure; imshow(lb, [], 'border', 'tight'); colormap(rand(256,3));


如果你想要颜色的渐变效果,你可以

[x y] = meshgrid(linspace(0,1,size(bw,2)), linspace(0,1,size(bw,1)));
rand('seed',543310);
rgb_lb = ind2rgb(lb, rand(max(lb(:)+1),3)); %// convert to RGB color image
gx = x; 
gx(lb==1)=1;  %// use the horizontal gradient
gx = gx./max(gx(:));

应用渐变:

rgb_lb = bsxfun(@times, rgb_lb, gx);

结果:

【讨论】:

  • @AnderBiguri tnx!我自己随机挑选了颜色;)
  • 相当简洁,同时注意细节(bwlabel(bw,4) 中的4)。
  • 没错。有些问题在于分辨率,我还有一个问题。如何设置渐变色填充?
  • 旁注:不需要%//,所以支持MATLAB!
  • 哦,真可爱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 2018-07-21
  • 2017-07-24
  • 2014-03-30
  • 1970-01-01
  • 2020-05-22
相关资源
最近更新 更多