Getframe function problem: Captured Image will not re-display at same size

goal: I want to be able to capture the color by pixel location data (C-data?) of any current figure, scramble the locations of any black pixels in it, and then re-display the figure in scrambled form.
problem: After scrambling the figure and re-displaying it, it is now a different size than the old figure. I want it to be the same size as the unscrambled version.
I think the problem lies in how I am capturing and then displaying the image. This is how I am doing that: (after creating a current figure). Attached is a screenshot of the problem for clarity. I am running 2015a.
function[CurrentFigureData, CurrentFigureMap]= WriteCF2Workspace()
F = getframe(gcf);
[CurrentFigureData, CurrentFigureMap] = frame2im(F);
end
figure(2);ScreenDims = get(0, 'ScreenSize'); set(2,'position',ScreenDims, 'menubar','none'); image(CurrentFigureData); axis off;

2 Commenti

Why not just use an axes control with an image in it, instead of trying to do the whole figure?
Thanks for the idea. Could you explain how making that change would solve the problem? -Michael

Accedi per commentare.

Risposte (2)

curfig = gcf;
curfig_units = get(curfig, 'units');
curfig_size = get(curfig, 'Position');
curimage_h = findall(curfig, 'type', image');
curax = ancestor(curimage_h, 'axes');
fig2 = figure('Units', curfig_units, 'Position', curfig_size);
ax2 = copyobj(curfig_ax, fig2);
image2 = copyobj(curimage_h, ax2);

7 Commenti

Thanks for the help so far Walter! This looks promising, but I have one question. When I run that code I get the following error: Error using copyobj Copyobj cannot create a copy of an invalid handle.
Error in Untitled (line 7) ax2 = copyobj(curfig_ax, fig2);
Do you know why that might be?
curfig = gcf;
curfig_units = get(curfig, 'units');
curfig_size = get(curfig, 'Position');
curimage_h = findall(curfig, 'type', image');
curax = ancestor(curimage_h, 'axes');
fig2 = figure('Units', curfig_units, 'Position', curfig_size);
ax2 = copyobj(curax, fig2);
image2 = copyobj(curimage_h, ax2);
Unfortunately, this solution does not work. When I insert the scrambled image on the figure that this script creates it does not align with the original.
Hi Michael, I'm facing similar difficulty, were you able to figure it out?
Here is the code I was working on at the time:
function[TransformMe,CurrentFigureData,CurrentFigureMap] = MaskFromCF() %iterate over each dimension from 1:dimsize. Find all indicies where all 3 %= 0. Do it so that you only check dim 2 if dim 1 was 0, and only dim 3 if %dim 2 was 0. Record those indicies, and you will have black pixels! %note that white is 255, 255,255 RGB
%create workspace var with image data [CurrentFigureData, CurrentFigureMap] = WriteCF2Workspace;
%now modify it to make mask, start by finding all black pixels (works only for black text on white) SizeOfData = size(CurrentFigureData); display(SizeOfData(1)) LocationsOfBlack = []; for i = 1:SizeOfData(1) for j = 1:SizeOfData(2) if CurrentFigureData(i,j,1) == 0 && CurrentFigureData(i,j,2) == 0 && CurrentFigureData(i,j,3) == 0 LocationsOfBlack{1,end+1} = {i,j}; end end end %now that you found all black pixels, find the furthest left/right values %that are black. You will scramble the pixels between these values.
%%%%you can mod these parameters to make it look only in a certain %%%%space.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% furthestX = -inf; smallestX = inf; furthestY = -inf; smallestY = inf;
%the parts after the && signs in this for loop specify screen locations to search in. Remove to search whole screen. %NOTE THAT X AND Y HERE CORRESPOND TO THE OPPOSITE SCREEN Dimension (restricting X effects Y range when shown etc.) %Also note that low numbers correspond to the top of the screen and higher %ones to the bottom of the screen for i = 1:length(LocationsOfBlack) testx = cell2mat(LocationsOfBlack{i}(1)); testy = cell2mat(LocationsOfBlack{i}(2)); if testy > 270 && testy < 1022 && testx > 80 && testx < 270 %restricts screen scan area for mask (by limiting scan for black). Change numbers to alter screen scan area. Go = 1; else Go = 0; end if Go == 1 if cell2mat(LocationsOfBlack{i}(1)) > furthestX furthestX = cell2mat(LocationsOfBlack{i}(1)); end if cell2mat(LocationsOfBlack{i}(1)) < smallestX smallestX = cell2mat(LocationsOfBlack{i}(1)); end if cell2mat(LocationsOfBlack{i}(2)) > furthestY furthestY = cell2mat(LocationsOfBlack{i}(2)); end if cell2mat(LocationsOfBlack{i}(2)) < smallestY smallestY = cell2mat(LocationsOfBlack{i}(2)); end end end %end of section where you can mod parameters to impact search field %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Xdiff = furthestX-smallestX; Ydiff = furthestY-smallestY;
%Move around the pixels ShuffleX = randperm(Xdiff+1)+smallestX; %start shuffling from smallest to largest X. ShuffleY = randperm(Ydiff+1)+smallestY; %you now have the indexes you need to shuffle, now shuffle them in %CurrentFigureData TransformMe = CurrentFigureData; for i = smallestX:furthestX for j = smallestY:furthestY k = i-(smallestX-1); l = j-(smallestY-1); TransformMe(i,j,:) = CurrentFigureData(ShuffleX(k),ShuffleY(l),:); end end
And the other function:
function[CurrentFigureData, CurrentFigureMap]= WriteCF2Workspace() F = getframe(gca); [CurrentFigureData, CurrentFigureMap] = frame2im(F); end
I think it did end up working, but unfortunately I can't test it atm because I don't have access to my normal pc. Hope the code helps. Good luck!

Accedi per commentare.

Replace gcf with gca, i.e.,
F = getframe(gca); [CurrentFigureData, CurrentFigureMap] = frame2im(F);

Risposto:

il 8 Ago 2018

Community Treasure Hunt

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

Start Hunting!

Translated by