Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
replace image color values with real values from scale

Subject: replace image color values with real values from scale

From: Dave B

Date: 25 Mar, 2012 20:06:11

Message: 1 of 7

Hi,

I have a 2D bitmap image of a top down view of a surface. The color of the image pixels represent the height of the surface. The color scale is also an image.

What I want to do is read in both the 2D top view image and replace the color values of the image with the closest from the color scale.

Can anyone advise a way to do this please? Thanks.

Subject: replace image color values with real values from scale

From: ImageAnalyst

Date: 26 Mar, 2012 00:40:03

Message: 2 of 7

On Mar 25, 4:06 pm, "Dave B" <davebrack...@hotmail.com> wrote:
> Hi,
>
> I have a 2D bitmap image of a top down view of a surface. The color of the image pixels represent the height of the surface. The color scale is also an image.
>
> What I want to do is read in both the 2D top view image and replace the color values of the image with the closest from the color scale.
>
> Can anyone advise a way to do this please? Thanks.

---------------------------------------------------------------------------------------
Dave:
Don't you have the original height image that the colormap was applied
to? Or are you doing something like scanning in a pseudocolored image
from an article or something? If you have the image, simply don't
apply the colormap:

imshow(heightImage, []);
colormap(gray(256));
colorbar;

If you don't have the original heightImage, you'll have to figure out
the colormap that was used and then use rgb2ind() to convert the rgb
image into a height image.
ImageAnalyst

Subject: replace image color values with real values from scale

From: Dave B

Date: 26 Mar, 2012 08:56:11

Message: 3 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b2969eb8-bb26-4701-8a22-176dc4a0f799@qg3g2000pbc.googlegroups.com>...
> On Mar 25, 4:06 pm, "Dave B" <davebrack...@hotmail.com> wrote:
> > Hi,
> >
> > I have a 2D bitmap image of a top down view of a surface. The color of the image pixels represent the height of the surface. The color scale is also an image.
> >
> > What I want to do is read in both the 2D top view image and replace the color values of the image with the closest from the color scale.
> >
> > Can anyone advise a way to do this please? Thanks.
>
> ---------------------------------------------------------------------------------------
> Dave:
> Don't you have the original height image that the colormap was applied
> to? Or are you doing something like scanning in a pseudocolored image
> from an article or something? If you have the image, simply don't
> apply the colormap:
>
> imshow(heightImage, []);
> colormap(gray(256));
> colorbar;
>
> If you don't have the original heightImage, you'll have to figure out
> the colormap that was used and then use rgb2ind() to convert the rgb
> image into a height image.
> ImageAnalyst

Hi, thanks for your reply. Below is my code attempt so far. It doesn't quite work correctly as a surface plot check of the result is wrong. Can you see what the problem is? Thanks a lot.

% read in data image and scale image
image=imread('image_actual.bmp');
color_scale=imread('scale_actual.bmp');

no_pixels_sample=220; % at pixel 220 along the scale the real value is 1200
range_pixels=1200;
lower_lim=0;
upper_lim=size(color_scale,1)*(range_pixels/no_pixels_sample);

scale_real=lower_lim:((upper_lim-lower_lim))/(size(color_scale,1)-1):upper_lim;

% convert rgb images to indexed
[image_ind,image_map] = rgb2ind(image, 65536); % image
[scale_ind,scale_map] = rgb2ind(color_scale, image_map); % scale using same map as image

scale_real_lookup=fliplr(scale_real);
image_real=zeros(size(image_ind,2),size(image_ind,3));

% find closest value
for i=1:size(image_ind,1)
    for j=1:size(image_ind,2)
        replicated=double(repmat(image_ind(i,j),size(scale_ind,1),1));
        diff=abs(replicated-double(scale_ind));
        
        % find minimum diff
        min_diff=min(diff);
        [row col]=find(diff==min_diff,1,'first');
        closest_color_from_scale=scale_ind(row,col);
        real_value=scale_real_lookup(row);
        image_real(i,j)=real_value;
    end
end

% surf check
surf(flipud(image_real))

Subject: replace image color values with real values from scale

From: ImageAnalyst

Date: 26 Mar, 2012 11:35:22

Message: 4 of 7

Where did you post your pictures?

Subject: replace image color values with real values from scale

From: Dave B

Date: 26 Mar, 2012 11:56:14

Message: 5 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <035df942-41e2-472f-8c36-ac7fbea187a1@oq7g2000pbb.googlegroups.com>...
> Where did you post your pictures?

here they are:

http://img208.imageshack.us/img208/5914/imageactual.png
http://img12.imageshack.us/img12/7924/scaleactual.png

They were bmps but seem to have been converted during hosting. Thanks.

Subject: replace image color values with real values from scale

From: ImageAnalyst

Date: 26 Mar, 2012 14:55:56

Message: 6 of 7

On Mar 26, 7:56 am, "Dave B" <davebrack...@hotmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <035df942-41e2-472f-8c36-ac7fbea18...@oq7g2000pbb.googlegroups.com>...
> > Where did you post your pictures?
>
> here they are:
>
> http://img208.imageshack.us/img208/5914/imageactual.pnghttp://img12.imageshack.us/img12/7924/scaleactual.png
>
> They were bmps but seem to have been converted during hosting. Thanks.

----------------------------------------------------------------------------------------
Do you know the heights associated with each end of the color bar? If
you do then you can read in the color bar image, use size() to find
out how many rows it has, then use linspace(lowerHeightValue,
upperHeightValue, numberOfColors) to create a mapping between the
color and the actual height. Each row in the colorbar image will then
have both a color and a height associated with that color, in other
words, a colormap. You can then use this colormap in rgb2ind() to get
your height data for the whole image (I've left out a few steps
though).

Subject: replace image color values with real values from scale

From: Dave B

Date: 2 Apr, 2012 14:30:11

Message: 7 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2e3f46bc-08ef-44f1-b20c-7ea3a1d6efcf@pg2g2000pbb.googlegroups.com>...
> On Mar 26, 7:56 am, "Dave B" <davebrack...@hotmail.com> wrote:
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <035df942-41e2-472f-8c36-ac7fbea18...@oq7g2000pbb.googlegroups.com>...
> > > Where did you post your pictures?
> >
> > here they are:
> >
> > http://img208.imageshack.us/img208/5914/imageactual.pnghttp://img12.imageshack.us/img12/7924/scaleactual.png
> >
> > They were bmps but seem to have been converted during hosting. Thanks.
>
> ----------------------------------------------------------------------------------------
> Do you know the heights associated with each end of the color bar? If
> you do then you can read in the color bar image, use size() to find
> out how many rows it has, then use linspace(lowerHeightValue,
> upperHeightValue, numberOfColors) to create a mapping between the
> color and the actual height. Each row in the colorbar image will then
> have both a color and a height associated with that color, in other
> words, a colormap. You can then use this colormap in rgb2ind() to get
> your height data for the whole image (I've left out a few steps
> though).


Yes I know the heights associated with each color of the scale image.

scale_real contains the actual height values and is a 1 x 225 double vector. This corresponds to the 225x1x3 uint8 read in scale image.

How can I put this into the right format for the rgb2ind function?

Thanks.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us