need to gray-rgb transforming

1 view (last 30 days)
Osman Taha Altunok
Osman Taha Altunok on 24 Apr 2017
Answered: Image Analyst on 24 Apr 2017
i'm trying to transform rice.png to this image. i tried different ways to do it bu it's not working. the images background will be white, and the rices will be colorful as in the image. someone can suggest me somethings? ı'm need help. (by the way, im sorry for bad resolution :/ )
Osman Taha Altunok
Osman Taha Altunok on 24 Apr 2017
Thanks for help! I looked that. Then I opened Matlab's help and looked for label2rgb command. then I solve all of problems. Thank you again :)

Sign in to comment.

Answers (2)

Thorsten on 24 Apr 2017
Edited: Thorsten on 24 Apr 2017
The idea is to convert a color map to an RGB image stripe of size 1 x N, where N is the number of entries in the colormap, and then adjust the this stripe to the size of the rice image and use a binary image of the rice as mask. If the number of colors in the colormap does not match the number of columns in the image, you have to interpolate the color stripe.
I = imread('rice.png');
B = im2bw(I);
B3 = repmat(B, [1, 1, 3]);
c = colormap('jet');
C = reshape(c, [1, size(c, 1) 3]);
C3 = repmat(C, [size(I, 1), 1, 1]);
imshow(im2double(C3) .*im2double(B3) + 1 - B3)

Image Analyst
Image Analyst on 24 Apr 2017
How about this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
I = imread('rice.png');
title('Original Image', 'FontSize', fontSize);
% Threshold the image.
binaryImage = im2bw(I);
title('Binary Image', 'FontSize', fontSize);
% Identify individual blobs by seeing which pixels are connected to each other.
% Each group of connected pixels will be given a label, a number, to identify it and distinguish it from the other blobs.
% Do connected components labeling with either bwlabel() or bwconncomp().
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% labeledImage is an integer-valued image where all pixels in the blobs have values of 1, or 2, or 3, or ... etc.
subplot(2, 2, 3);
imshow(labeledImage, []); % Show the gray scale image.
title('Labeled Image, from bwlabel()', 'FontSize', fontSize);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 4);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom,\nthen from left to right.');
title(caption, 'FontSize', fontSize);
If you really need the background to be white instead of black, let me know.


Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!