How to stack colour layers

I have an initial picture and make some calculations with the individual colour layers in a for-loop going through the 3 layers. In the end I want to stack them again to get the coloured version. Instead, I reveice nothing meaningful. What is wrong in the syntax?
for ii=1:3
...
end
Img_R = uint8(abs(Img_x2(:,:,1)));
Img_G = uint8(abs(Img_x2(:,:,2)));
Img_B = uint8(abs(Img_x2(:,:,3)));
pertImgRGB = cat(3,Img_R,Img_G,Img_B);
figure(5);
subplot(1,3,2);
imagesc(x, y, pertImgRGB);
axis square;
set(gca,'YDir','normal')

9 Comments

It might help to know what you’re doing in this loop:
for ii=1:3
...
end
I hope this helps. The whole code would be over 200 lines long.
for ii=1:3
Img_k1(:,:,ii) = fftshift(fft2(fftshift(Img_x1(:,:,ii))));
Img_k1(:,:,ii) = Img_k1(:,:,ii) .* H_telvac;
Img_x1(:,:,ii) = ifftshift(ifft2(ifftshift(Img_k1(:,:,ii))));
figure(ii+1);
subplot(2,3,4);
imagesc(x, y, abs(Img_x1(:,:,ii)));
axis square;
set(gca,'YDir','normal')
Img_k2(:,:,ii) = fftshift(fft2(fftshift(Img_x2(:,:,ii))));
Img_k2(:,:,ii) = Img_k2(:,:,ii) .* H_telvac;
Img_x2(:,:,ii) = ifftshift(ifft2(ifftshift(Img_k2(:,:,ii))));
figure(ii+1);
subplot(2,3,5);
imagesc(x, y, abs(Img_x2(:,:,ii)));
axis square;
set(gca,'YDir','normal')
end
Img_R = uint8(abs(Img_x2(:,:,1)));
Img_G = uint8(abs(Img_x2(:,:,2)));
Img_B = uint8(abs(Img_x2(:,:,3)));
pertImgRGB = cat(3,Img_R,Img_G,Img_B);
figure(5);
subplot(1,3,2);
imagesc(x, y, pertImgRGB);
axis square;
set(gca,'YDir','normal')
Instead of inserting the code as text (which is a difficult to read and hard to try out), please upload the file using the paperclip button. You will need to push both the Choose file and Attach file buttons.
What is the original datatype of Img_x1 ? If it is double, with the values being between 0 and 1, then you need to multiply by 255 before you uint8
Img_R = uint8(255*abs(Img_x2(:,:,1)));
Lucius
Lucius on 15 May 2015
Edited: Lucius on 15 May 2015
yes,it's Img_x=double(ImgRGB). Just multiplied the three uint8 lines. But the figure 5 in question is still only white instead of a fuzzy, coloured (3 layers) galaxy.
There are only 4 figures, not 5. Figure 4 is below. Exactly what is all white?
No, there are really 5 figures; the fifth being introduced with line 237. And instead of showing the top left image from the above figure in a perturbed way, I only get a figure with a white subplot(1,3,2) (line 238).
Oh, you're right. For some weird reason figure 5 popped "up" exactly underneath figure #1 which totally hid/blocked figure #5. I'll look at it again.

Sign in to comment.

 Accepted Answer

Before you go to uint8, the range of your data is:
minValue =
0.0169157443510078
maxValue =
145341.029916172
Replace your code with this:
% Display what the max and min are, just for fun.
minValue = min(abs(Img_x2(:)))
maxValue = max(abs(Img_x2(:)))
% Scale to 0-255
Img_x2 = 255 * mat2gray(abs(Img_x2));
Img_R = uint8((Img_x2(:,:,1)));
Img_G = uint8((Img_x2(:,:,2)));
Img_B = uint8((Img_x2(:,:,3)));
pertImgRGB = cat(3,Img_R,Img_G,Img_B);

5 Comments

Lucius
Lucius on 15 May 2015
Edited: Lucius on 15 May 2015
Ok done. Actually, pertImgRGB should be displayed in colours, shouldn't it? What we have here is the same situation as your answer from this topic convert gray image back to rgb. Img_R/G/B are in uint8 and with a range from 0-255. cat() should make a 3dimensional matrix out of it...being a coloured stack. There seems to be something missing. But we're almost there! Thank you very much so far!
The image in question only looks as if gray, but is really coloured - one only needs to zoom in to see it better.
So is there still a problem?
one last one. In the end, I want to concatenate the corrected layers again to reveal the fully unperturbed galaxy again...to show that the correction process works. I applied the very same commands that you suggested above and the image formats seem to be ok. But the galaxy is only halfway corrected. The layers from line 307 shall be stacked. Line 318f shows the code. Btw, the same galaxy image as posted above.
I think we found the solution. The image we read in at the beginning already contains the diffraction and systematic effects of the Hubble telescope; we do not have the true planewave information from the imaged object, but only the CCD image itself (of course, as the planewave cannot be recorded withouth a recording device that introduces its own systematic effects). After propagating it trough our own aperture, we have additional diffraction patterns. We apply our disturbance to it and correct it back. This works perfectly. But it's strange that if introducing a star image, we do not get the overall star image WITH diffraction pattern. The stars are not where they are expected to be compared to the introduced image. But at least the correction algorithm works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 May 2015

Commented:

on 20 May 2015

Community Treasure Hunt

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

Start Hunting!