如何分割图像和填充不同的颜色 [英] how to split image and fill different color

查看:275
本文介绍了如何分割图像和填充不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>



图片,如何使用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 =分水岭(M);
r = L&飞机;
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')
(c(i,1)-5,c(i,2) -10,num2str(i))
end
%第二个代码
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


解决方案

您可以使用






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

  [xy] = 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) %//转换为RGB彩色图像
gx = x;
gx(lb == 1)= 1; %//使用水平渐变
gx = gx./max(gx(:));

应用渐变:

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

结果:




As this picture, how to use matlab code to split it into different parts, and then fill color in it? In addition, how to set gradient color in the second code ???

Here is the picture segmentation code:

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

解决方案

You can use bwlabel to assign different index to each image region:

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

The result:

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


If you want a gradient effect to the colors, you can

[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(:));

Apply the gradient:

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

The result:

这篇关于如何分割图像和填充不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆