Coloring an 8-bit grayscale image in MATLAB -
i have 8-bit grayscale image different values (0,1,2,3,4,..., 255). want color grayscale image colors blue, red, etc. until now, have been doing coloring in greyscale. how can actual colors?
here code have written far. searching values white in image , replacing them darkish gray:
for k = 1:length(tiffiles) basefilename = tiffiles(k).name; fullfilename = fullfile(myfolder, basefilename); fprintf(1, 'now reading %s\n', fullfilename); imagearray = imread(fullfilename); %// logic replace white grayscale values darkish gray here ind_plain = find(imagearray == 255); imagearray(ind_plain) = 50; imwrite(imagearray, fullfilename); end
what asking perform pseudo colouring of image. doing in matlab quite easy. can use grayscale intensities index colour map, , each intensity generate unique colour. first, need create colour map 256 elements long, use ind2rgb
create colour image given grayscale intensities / indices of image.
there many different colour maps available in matlab. here current available colour maps in matlab without added parula colour map introduced in r2014:
how colour maps work lower indices / grayscale values have colours move toward left side of spectrum , higher indices / grayscale values have colours move toward right side of spectrum.
if want create colour map 256 elements, use 1 of colour maps function , specify 256 input parameter generate 256 element colour map you. example, if wanted use hsv colour map, in matlab:
cmap = hsv(256);
now, given grayscale image in matlab workspace stored in imagearray
, use ind2rgb
way:
colourarray = ind2rgb(double(imagearray)+1, cmap);
the first argument grayscale image want pseudocolour, , second input colour map produced 1 of matlab's colour mapping functions. colourarray
contain pseudo coloured image. take note offset grayscale image 1 , cast double
. reason because matlab 1-indexed programming language, have start indexing arrays / matrices starting @ 1. because intensities range [0,255]
, , want use index colour map, must make go [1,256]
allow indexing. in addition, using uint8
images, , adding 1 uint8
saturate values @ 255 255. won't able go 256. therefore, need cast image temporarily double
can increase precision of image , add 1 allow image go 256 if merited.
here's example using cameraman.tif
image that's part of image processing toolbox. looks like:
so can load in image in matlab so:
imagearray = imread('cameraman.tif');
next, can use above image, generate hsv colour map pseudocolour image:
cmap = hsv(256); colourarray = ind2rgb(imagearray+1, cmap);
we get:
take note don't have use of colour maps matlab provides. in fact, can create own colour map. have create 256 x 3 matrix each column denotes proportion of red (first column), green (second column) , blue (third column) values per intensity. therefore, first row gives colour mapped intensity 0, second row gives colour mapped intensity 1 , on. also, need make sure intensities floating-point , range [0,1]
. example, these first 10 rows of hsv colour map generated above:
>> cmap(1:10,:) ans = 1.0000 0 0 1.0000 0.0234 0 1.0000 0.0469 0 1.0000 0.0703 0 1.0000 0.0938 0 1.0000 0.1172 0 1.0000 0.1406 0 1.0000 0.1641 0 1.0000 0.1875 0 1.0000 0.2109 0
you can use custom colour map ind2rgb
pseudocolour image.
good luck , have fun!