need to gray-rgb transforming
1 次查看(过去 30 天)
显示 更早的评论
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 :/ )
回答(2 个)
Thorsten
2017-4-24
编辑:Thorsten
2017-4-24
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)
0 个评论
Image Analyst
2017-4-24
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');
subplot(2,2,1);
imshow(I);
title('Original Image', 'FontSize', fontSize);
% Threshold the image.
binaryImage = im2bw(I);
subplot(2,2,2);
imshow(binaryImage);
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);
imshow(coloredLabels);
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 3-D Volumetric Image Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!