badprogrammer
badprogrammer

Reputation: 23

Creating my own colormap, such that two different colors can be plotted on one image in Matlab

I want to have the brighter pixel come out in green and the not at bright features to come out in blue. here's what I tried

for i=1:128
mycolormap(64:128,2)=i/128;
mycolormap(1:63,3)=i/128;
mycolormap(1:63,2)=0;
mycolormap(64:128,3)=0;
mycolormap(i,1)=0;
end

the images i'm working with a Uint16 .tif I've considered casting the colormap to a double before instantiating. thanks!

reading the commented stuff you added really help me think it through, so I adjusted my code and got what I want by doing some cheap coding. but now I got what I want thanks!

     for i=1:128                  % start a loop (see comment above about i variable)
                             % then in EACH iteration
mycolormap(i,2)=i/128;  % fill bottom half of 2nd column of MYCOLORMAP with i/128
mycolormap(i:63,3)=i/128;    % fill top    half of 3rd column of MYCOLORMAP with i/128
mycolormap(1:63,2)=0;        % fill top    half of 2nd column of MYCOLORMAP with 0
mycolormap(64:128,3)=0;      % fill bottom half of 3rd column of MYCOLORMAP with 0
mycolormap(i,1)=0;           % fill i-th   row  of 1st column with 0
end
>> mycolormap

mycolormap =

         0         0    0.0078
         0         0    0.0156
         0         0    0.0234
         0         0    0.0313
         0         0    0.0391
         0         0    0.0469
         0         0    0.0547
         0         0    0.0625
         0         0    0.0703
         0         0    0.0781
         0         0    0.0859
         0         0    0.0938
         0         0    0.1016
         0         0    0.1094
         0         0    0.1172
         0         0    0.1250
         0         0    0.1328
         0         0    0.1406
         0         0    0.1484
         0         0    0.1563
         0         0    0.1641
         0         0    0.1719
         0         0    0.1797
         0         0    0.1875
         0         0    0.1953
         0         0    0.2031
         0         0    0.2109
         0         0    0.2188
         0         0    0.2266
         0         0    0.2344
         0         0    0.2422
         0         0    0.2500
         0         0    0.2578
         0         0    0.2656
         0         0    0.2734
         0         0    0.2813
         0         0    0.2891
         0         0    0.2969
         0         0    0.3047
         0         0    0.3125
         0         0    0.3203
         0         0    0.3281
         0         0    0.3359
         0         0    0.3438
         0         0    0.3516
         0         0    0.3594
         0         0    0.3672
         0         0    0.3750
         0         0    0.3828
         0         0    0.3906
         0         0    0.3984
         0         0    0.4063
         0         0    0.4141
         0         0    0.4219
         0         0    0.4297
         0         0    0.4375
         0         0    0.4453
         0         0    0.4531
         0         0    0.4609
         0         0    0.4688
         0         0    0.4766
         0         0    0.4844
         0         0    0.4922
         0    0.5000         0
         0    0.5078         0
         0    0.5156         0
         0    0.5234         0
         0    0.5313         0
         0    0.5391         0
         0    0.5469         0
         0    0.5547         0
         0    0.5625         0
         0    0.5703         0
         0    0.5781         0
         0    0.5859         0
         0    0.5938         0
         0    0.6016         0
         0    0.6094         0
         0    0.6172         0
         0    0.6250         0
         0    0.6328         0
         0    0.6406         0
         0    0.6484         0
         0    0.6563         0
         0    0.6641         0
         0    0.6719         0
         0    0.6797         0
         0    0.6875         0
         0    0.6953         0
         0    0.7031         0
         0    0.7109         0
         0    0.7188         0
         0    0.7266         0
         0    0.7344         0
         0    0.7422         0
         0    0.7500         0
         0    0.7578         0
         0    0.7656         0
         0    0.7734         0
         0    0.7813         0
         0    0.7891         0
         0    0.7969         0
         0    0.8047         0
         0    0.8125         0
         0    0.8203         0
         0    0.8281         0
         0    0.8359         0
         0    0.8438         0
         0    0.8516         0
         0    0.8594         0
         0    0.8672         0
         0    0.8750         0
         0    0.8828         0
         0    0.8906         0
         0    0.8984         0
         0    0.9063         0
         0    0.9141         0
         0    0.9219         0
         0    0.9297         0
         0    0.9375         0
         0    0.9453         0
         0    0.9531         0
         0    0.9609         0
         0    0.9688         0
         0    0.9766         0
         0    0.9844         0
         0    0.9922         0
         0    1.0000         0

Upvotes: 2

Views: 142

Answers (1)

anandr
anandr

Reputation: 1672

Your code looks somewhat strange:

for i=1:128                  % start a loop (see comment above about i variable)
                             % then in EACH iteration
mycolormap(64:128,2)=i/128;  % fill bottom half of 2nd column of MYCOLORMAP with i/128
mycolormap(1:63,3)=i/128;    % fill top    half of 3rd column of MYCOLORMAP with i/128
mycolormap(1:63,2)=0;        % fill top    half of 2nd column of MYCOLORMAP with 0
mycolormap(64:128,3)=0;      % fill bottom half of 3rd column of MYCOLORMAP with 0
mycolormap(i,1)=0;           % fill i-th   row  of 1st column with 0
end

So, after your loop
- bottom half of 2nd column of MYCOLORMAP is filled with 1
- top half of 3rd column of MYCOLORMAP is filled with 1.

This is because in your loop you fill exactly the same elements of the colormap with different values so only the last one with i=128 remains.

This result is easier achieved in the following way:

mycolormap = zeros(128,3);
mycolormap(64:128,2)=1;
mycolormap(1:63,3)=1;

However, if this is not the colormap you expect, explain what you need in more details.

Upvotes: 1

Related Questions