MATLAB: concatenition of 2 images in a frame: resulting images of 2 parts of code seem to be overwriting each other

%......section (e)
C1 = imread(InputImage);
NumberOfLevels = 2;
n = 32;
C1q1=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
imshow(C1q1,[]);
NumberOfLevels=4;
n = 32;
C1q2=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
imshow(C1q2,[]);
no8=figure;
C1img = [C1q1;C1q2];
image(C1img);
%........end of section (e)
%....section (f)
C1 = imread(InputImage);
C1 = double(C1);
NumberOfGrayLevels=32;
I= 0.299*C1(:,:,1)+0.587*C1(:,:,2)+0.114*C1(:,:,3);
C = 1;
I=(C*log(I+1))';
new=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
[m,n]= size(new);
rgb = zeros(m,n,3);
rgb(:,:,1) = new;
rgb(:,:,2) = rgb(:,:,1);
rgb(:,:,3) = rgb(:,:,2);
new = rgb/256;
imshow(new,[]);
no9=figure;
image(new);
%...........end of section (f)
when i put a break command before %.....section(f) (i.e. when i execute only section (e)) then i can see concatenated image for C1q1 and C1q2 in a single frame as it should be. but when i remove the break command (i.e.) when i execute both section(e) and section (f) then i can see that result of section (f) is overwriting result in section (e). instead of seeing C1q1 and C1q2 in a frame, i see figure (new) in both frames. i cannot see C1q1 and C1q2 anymore. can you tell me why is this happening and how to overcome this?

 Accepted Answer

Try adding a drawnow() after the image() command.
Your imshow(new,[]) is erasing your existing image() in the same axes. Where do you want the imshow() to be displayed, and is there a particular reason you have both imshow(new,[]) and image(new) ?

6 Comments

thank you...now it is working... and it was my mistake to put imshow(new,[]) and image(new) both in same section. i did not know that imshow(new,[]) is erasing my existing image. thank you
the same problem is coming at other part of code. i have used drawnow()
%...section (d)
NumberOfGrayLevels=4;
Iq1=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
image(Iq1);
NumberOfGrayLevels=16;
Iq2=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
image(Iq2);
NumberOfGrayLevels=32;
Iq3=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
image(Iq3);
NumberOfGrayLevels=64;
Iq4=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
image(Iq4);
no7 = figure;
quantimg = cat(2,Iq1, Iq2,Iq3, Iq4);
image(quantimg);
drawnow();
%............end if section (d).....
%......section (e)
C1 = imread(InputImage);
NumberOfLevels = 2;
n = 32;
C1q1=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
imshow(C1q1,[]);
NumberOfLevels=4;
n = 32;
C1q2=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
imshow(C1q2,[]);
no8=figure;
C1img = cat(2,C1q1,C1q2);
image(C1img);
drawnow();
%........end of section (e)
for section (e) it worked. i have used drawnow() but still i cannot see the concateneted image for section (d) instead i am just seeing the Iq4 in grayscale in the frame
It's NOT erasing your prior image - it's still there. It's just covered up with your latest image. That's why if you have imshow() in a loop it will get slower and slower as more and more memory gets used up as it loads more and more images into the axes. In that case you can add a "cla" command just before you call imshow().
i tried putting cla just before imshow() but it is showing the same problem. i can only see grayscale image Iq4 instead of image Iq1, Iq2, Iq3 and Iq4. but the strange thing is when generating all images, i can see 4 images in a flick for 1 second. but then the 4 images turn into only Iq4. thats why as a result i am seeing only Iq4. %.....end of section (c)....
%...section (d)
NumberOfGrayLevels=4;
Iq1=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
cla
imshow(Iq1,[]);
NumberOfGrayLevels=16;
Iq2=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
cla
imshow(Iq2,[]);
NumberOfGrayLevels=32;
Iq3=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
cla
imshow(Iq3,[]);
NumberOfGrayLevels=64;
Iq4=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
cla
imshow(Iq4,[]);
no7 = figure;
quantimg = cat(2,Iq1, Iq2,Iq3, Iq4);
image(quantimg);
drawnow();
%............end if section (d).....
cla
%......section (e)
C1 = imread(InputImage);
NumberOfLevels = 2;
n = 32;
C1q1=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
cla
imshow(C1q1,[]);
NumberOfLevels=4;
n = 32;
C1q2=uint8(rgb2ind(C1,n)*(NumberOfLevels-1));
cla
imshow(C1q2,[]);
no8=figure;
C1img = cat(2,C1q1,C1q2);
image(C1img);
drawnow();
%........end of section (e)
please let me know how to overcome this problem
Each cla will erase what has gone before. If you want to show all four images at the same time, you will need to use different figures for each, or use subplot(), or use the XData and YData parameters to imshow() in order to position the images all on the same axes but not overlapping.
thank you...i am displaying 4 images separately. it is working now.. thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!