Colormap doesn't align with indexed image after using rgb2ind

101 views Asked by At

When I run this section of code, figure1 displays the converted image file with the correct color values stored in X.

After copying I onto Grid, the colors are wrong.

I've figured out that the colormap values are somehow getting shifted up by one row. It's hard to explain, but at every point during run, X has these shifted values. In figure1 for whatever reason, the image displays with the correct colors, but by fig2 the map is shifted. I could write a few lines that works around this, but I still have no clue what is happening.

Anyone have any thoughts? Here's the link to the peppers.png file if anyone wants to run it. https://homepages.cae.wisc.edu/~ece533/images/peppers.png

Grid = ones(1000);   %create background image
    for m = 100:100:1000
        Grid(m, :) = 2;
        Grid(:, m) = 2;
    end

[I, X] = rgb2ind(imread('peppers.png'), 256); %read foreground image

IM = size(I,1);
IN = size(I,2);
imageCP= [round(IM/2) round(IN/2)];
CP = [500 500];
upperLeft = [CP(1)-imageCP(1) CP(2)-imageCP(2)];


figure(1) %display foreground/background separately
subplot 211
imshow(Grid,X)
subplot 212
imshow(I,X)


for m = 0:IM-1 %copy foreground image onto center of background image
    for n = 0:IN-1
        Grid(upperLeft(1)+m, upperLeft(2)+n) = I(m+1,n+1);
    end
end

figure(2)
imshow(Grid,X)
...
0

There are 0 answers