Screen capture area on UIpanel?

Sorry for an extension of previous question but I thought it deserves a new thread.
I am plotting images as thumbnails to form a montage.These are all plotted on a UIPanel in a loop, and there are typically 6 columns x 8 rows of thumbnail images.
Previously, I wanted to redraw all of these with a different scaling if required uisng:
% get the handles to the axes/subplots within the uipanelM
hAxes = findobj('Parent',handles.uipanelM,'Type','axes');
% decide which axes you want to do the adjustment on - if doing both
% then use a loop
for k=1:length(hAxes)
hImg = findobj('Parent',hAxes(k),'Type','image');
if ~isempty(hImg)
imgData = get(hImg,'CData');
J = imadjust(imgData,stretchlim(imgData),[0 1]);
imshow(J,'Parent',hAxes(k));
end
end
This works well, but I was wondering can I get the whole montage as a single entity rather than have to loop through the handles and address each on seperately. The reason being is I want to then take this set of "montaged" images, and shrink and place on another figure of UIPanel. I guess Im asking if its possible to do a sort of "screen capture"?
Thanks Jason

1 commentaire

Jason
Jason le 2 Oct 2014
Modifié(e) : Jason le 2 Oct 2014
Maybe the way I create the montaged image by displaying one image at a time shrinked and on a UIPanel is not the best approach as then I have many handles to deal with?

Connectez-vous pour commenter.

Réponses (1)

Geoff Hayes
Geoff Hayes le 2 Oct 2014

0 votes

Hi Jason - perhaps I'm misunderstanding what you want, but if you want to create one image that is the combination (montage) of all images from each of the subplots that are arranged in 8 rows of 6 columns, then you could do something like the following. Note that the assumption here is that each image is of the same dimension. If this assumption is invalid, then you can modify the code to resize each image to some fixed mxnx3 dimension using imresize.
First create some random images arranged in an 8x6 subplot grid
% generate mxn subplots with solid colour images
close all;
figure;
m = 8;
n = 6;
hAxes = [];
for u=1:m
for v=1:n
hAxes(u,v) = subplot('Position',[(v-1)/n 1-(u/m) 1/n 1/m]);
image(repmat(uint8(randi(255,1,1,3)),100,200,1),'Parent',hAxes(u,v));
set(hAxes(u,v),'XTick',[],'YTick',[]);
end
end
So this will give us our 48 subplots on the figure where each subplot contains a 100x200x3 image of a solid colour.
Now we just take the image from each subplot and combine it into a larger image
% create a montage
allImgs = cell(m,n);
for u=1:m
for v=1:n
hImg = findobj('Parent',hAxes(u,v),'Type','image');
allImgs{u,v} = get(hImg,'CData');
end
end
figure;
montageImg = cell2mat(allImgs);
image(montageImg);
set(gca,'XTick',[],'YTick',[]); % remove the ticks
We collect the images from each subplot into a cell array, and then convert that into a matrix/image which is just the combination of all subplot images.
Note how that hAxes is a 8x6 matrix of handles versus the one-dimensional array that you are using. I found that the 2D approach is a little easier since I know exactly where each subplot is, and so know where each image should go in the montage.

8 commentaires

Jason
Jason le 3 Oct 2014
Modifié(e) : Jason le 3 Oct 2014
Hi Geoff. I have tried to understand your code and incorporate it into mine, but I'm not quite there yet. I have my hAxes as a function of k only i.e. a single variable as I obtain it from the UIPanel via:
hAxes = findobj('Parent',handles.uipanelM,'Type','axes');
I then loop through all k ,
k=1:length(hAxes)
to get a handle on the image of each hAxes.
hImg = findobj('Parent',hAxes(k),'Type','image');
So now Im struggling with converting hAxes(k) to your format hAxes(u,v)
After every count of 6 in k, the position of the next image follows a raster scan i.e. it goes back to column 1, and down an extra row.
So I though within the k loop, I could do:
if k>6
u=1
v=v+1
else
u=u+1
end
hAxes(u,v)=hAxes(k);
hImg = findobj('Parent',hAxes(u,v),'Type','image');
allImgs{u,v} = get(hImg,'CData');
end
But it throws an error:
Error using cell2mat (line 46)
All contents of the input cell array must be of the same data type.
Error in Montage>pushbutton17_Callback (line 1143)
montageImg = cell2mat(allImgs);
Geoff Hayes
Geoff Hayes le 3 Oct 2014
Modifié(e) : Geoff Hayes le 3 Oct 2014
Jason - the error that you are observing may be due to an empty matrix being set somewhere in the allImgs cell array, or there may be a different sized matrix in one of the elements. Just view the contents of allImgs by typing it into the Command Window and see what the contents for each element is.
As for mapping the 1D hAxes to a 2D matrix, try reshaping it as follows
% dummy initialization of hAxes
hAxes = 1:48;
% reshape to 8x6 matrix
hAxes2D = reshape(hAxes,[6 8])';
hAxes2D =
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
37 38 39 40 41 42
43 44 45 46 47 48
Note that the 1D array is reshaped to a 6x8 matrix, and then the transpose of it is taken to get the 8x6.
I have this - but no images appear ?
hAxes = [];
hAxes = findobj('Parent',handles.uipanelM,'Type','axes');
hAxes2D = reshape(hAxes,[6 8])';
hImg = findobj('Parent',hAxes2D,'Type','image');
%allImgs{u,v} = get(hImg,'CData');
allImgs = get(hImg,'CData');
%figure(hFig);
figure;
montageImg = cell2mat(allImgs);
%subplot(3,2,1);
imshow(montageImg);
set(gca,'XTick',[],'YTick',[]); % remove the ticks
colormap(jet);
Jason
Jason le 3 Oct 2014
Modifié(e) : Jason le 3 Oct 2014
hmmm.. hImg is empty but hAxes2D is correct. So my error is soemthing to do with
hImg = findobj('Parent',hAxes2D,'Type','image');
Im assuming you no longer have to loop over u & v?
OK, got it working but the montage seems out by 1 in each dimension.
m=8;
n=6;
% create a montage of size mxn
allImgs = cell(m,n);
hAxes = [];
hAxes = findobj('Parent',handles.uipanelM,'Type','axes')
hAxes2D = reshape(hAxes,[6 8])'
%hImg([6 8]) = findobj('Parent',hAxes2D,'Type','image')
%allImgs = get(hImg,'CData');
for u=1:m
for v=1:n
hImg = findobj('Parent',hAxes2D(u,v),'Type','image');
allImgs{u,v} = get(hImg,'CData');
end
end
%figure(hFig);
figure;
montageImg = cell2mat(allImgs);
J = imadjust(montageImg,stretchlim(montageImg),[0 1]);
%subplot(3,2,1);
%imshow(montageImg);
imshow(J);
set(gca,'XTick',[],'YTick',[]); % remove the ticks
colormap(jet);
I suppose that is a problem with not maintaining the matrix of handles as they were created on the figure. If you do it that way (as in my code example from above) you know exactly which subplot corresponds to which row/column in the montage. Calling
hAxes = findobj('Parent',handles.uipanelM,'Type','axes');
just grabs all axes handles, in some order which you and I don't know. It obviously maintains some sort of order, but it is unclear which kind.
If I were to call
hAxes2 = findobj('Parent',gcf,'Type','axes');
on my example (just after all 48 subplots have been created) then the order of the elements in this column array/vector are with the last subplot added in the first position, hAxes2(1), and the first subplot added in the last position, hAxes2(48). If I want to correct the ordering, I would do something like the following
hAxes2 = findobj('Parent',gcf,'Type','axes');
hAxes2 = flipud(hAxes2);
hAxes2D = reshape(hAxes2,[6 8])';
Now, my hAxes2D will have its ordering identical to hAxes (as constructed in the code by hAxes(u,v) = subplot('Position',[(v-1)/n 1-(u/m) 1/n 1/m]);).
So I think you have two options:
  • as you add the subplots, construct the hAxes matrix and store that matrix to the handles structure so it can be obtained in other callbacks
  • determine your ordering of the subplots, and compensate for that ordering when you call hAxes = findobj('Parent',handles.uipanelM,'Type','axes');
Jason
Jason le 5 Oct 2014
Thankyou Geoff, it was the flipud thats corrected it. Thanks for all your time. Jason
Geoff Hayes
Geoff Hayes le 5 Oct 2014
Glad that you got it working, Jason!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Animation dans Centre d'aide et File Exchange

Question posée :

le 2 Oct 2014

Commenté :

le 5 Oct 2014

Community Treasure Hunt

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

Start Hunting!

Translated by