Screen capture area on UIpanel?
Afficher commentaires plus anciens
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
Réponses (1)
Geoff Hayes
le 2 Oct 2014
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
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.
Jason
le 3 Oct 2014
Jason
le 3 Oct 2014
Geoff Hayes
le 3 Oct 2014
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
le 5 Oct 2014
Geoff Hayes
le 5 Oct 2014
Glad that you got it working, Jason!
Catégories
En savoir plus sur Animation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

