how to split image and fill different color

微笑、不失礼 提交于 2019-12-01 21:46:37

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!