Image processing using indexing

Hi. I am wondering if anyone can help me. I'm not sure wether this is possible but i will try to explain it as best I can.
I have two images with the same resolution.
Image A, a RGB image.
Image B, a Gray scale image the with only a small region of postive values the rest black (0 values).
I wish to map a jet colormap to Image B and substitute these potive pixels into image A (repleaceing the pixels from A)
I understand that a grayscale image with a colormap is of a different format to a RGB image which is (m*n*3). But is it possible.
I hope i have explained it clearly enough.
I would be gratefull for any help. Thank you
Regards Dana

 采纳的回答

The details depend on what class your images are, but here's an example that you can probably modify as needed.
% test data - an rgb image and a greyscale image the same size, with lots
% of zeros in it
A = imread('saturn.png');
p = imread('pout.tif');
B = zeros(size(A,1), size(A,2), class(p));
B(1001:1000+size(p,1), 501:500+size(p,2)) = p;
% apply the jet map to the greyscale image
if strcmp(class(B), 'double')
% assume values in range 0-1
Brgb = ind2rgb(B, jet);
else
nvals = double(intmax(class(B))) + 1;
Brgb = ind2rgb(B, nvals*jet(nvals));
end
% copy non-zero parts of B into A
ind = repmat(B ~= 0, [1 1 3]); % index array
Amod = A;
Amod(ind) = Brgb(ind);
% look at the result
imshow(Amod);

5 个评论

Hey david thanks for your response. Sorry to be a bother but I have been playing around with you code trying to get it to work with my images for the last hour with no success.
The two images i have are both (936 * 1404 * double)
One of the errors i keep coming across is
Attempt to grow array along ambiguous dimension.
Error in rgb_jet (line 34)
Amod(ind) = Brgb(ind);
And thanks again for your help
Regards
My code assumes that A is RGB, so is M x N x 3, while B is greyscale so is M x N. Could you execute
size(A)
size(B)
and post what's printed please?
Also, I guess you realise that you shouldn't be executing any of my code before the comment "apply the jet map to the greyscale image". The stuff before that is just setting up A and B for the demonstration.
... and I'd recommend looking at the demo that Image Analyst mentions below, though you might want to get my simpler code going first.
Hey David, Thanks for your help I have spent all this morning playing around with your code but i have gotten it working.
My problem was that image p = imread('pout.tif'); is a smaller size than image A = imread('saturn.png');
I did not realise that the lines:
B = zeros(size(A,1), size(A,2), class(p));
B(1001:1000+size(p,1), 501:500+size(p,2)) = p;
were creating an image the same size as A and placing the photo in it.
So i cut down the code to
nvals = double(intmax(class(B))) + 1;
Brgb = ind2rgb(B, nvals*jet(nvals));
ind = repmat(B ~= 0, [1 1 3]); % index array
Amod = A;
Amod(ind) = Brgb(ind);
% look at the result
imshow(Amod);
As my images are already the same size and unit 8.
I also applied my own colormap which took some playing around but it worked.
So thanks again for your help, you really helped me out I cant believe the solution was so simple. You should have seen the code i had written myself to try and solve this.
Regards
Dana

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2011-12-15

0 个投票

Dana: See my full blown demo in another related posting:

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by