質問

I am programming a GUI in Matlab for an experiment where a test participant will view a series of images, and after each image, respond with a rating for the image.

I want to keep the window maximized at all times. An image will be displayed for a few seconds, then removed, some sliders will appear for the rating. Next, the sliders will be hidden, and a new image will appear, etc...

What I've got so far starts out fine with a maximized figure window, right until I load an image and display it, using imshow or image command, which causes the figure window to resize and fit to the image, instead of staying maximized. If I then maximize the figure window again, it causes a noticeable flicker from the window frame having first been maximized, then resized, then maximized again - a flicker I would like to avoid.

How can I keep the window maximized, and display an image at 1:1 ratio (NOT scaled or resized to fit the maximized window)?

I am aware of PsychToolbox, but it doesn't seem to have commands for creating sliders (which I would use for the rating), and I'd prefer not to have to do those from scratch. I've also looked into windowAPI from Matlab File Exchange, but still haven't found a solution.

Below is an example of what I have now (using Matlab R2013a on Windows 7 64-bit):

screenSize = get(0,'screensize');
screenWidth = screenSize(3);
screenHeight = screenSize(4);

% Create figure window, keeping it invisible while adding UI controls, etc.
hFig = figure('Name','APP',...
    'Numbertitle','off',...
    'Position', [0 0 screenWidth screenHeight],...
    'WindowStyle','modal',...
    'Color',[0.5 0.5 0.5],...
    'Toolbar','none',...
    'Visible','off');

% Make the figure window visible
set(hFig,'Visible','on');

% Maximize the figure window, using WindowAPI
WindowAPI(hFig, 'Position', 'work');

% Pause (in the full version of this script, this would instead be
% a part where some UI elements are shown and later hidden...
pause(1.0);

% Read image file
img = imread('someImage.png');

% Create handle for imshow, and hiding the image for now.
% This is where Matlab decides to modify the figure window,
% so it fits the image rather than staying maximized.
hImshow = imshow(img);
set(hImshow,'Visible','off');

% Show the image
set(hImshow,'Visible','on');

Thanks, Christian

役に立ちましたか?

解決

Try the using the 'InitialMagnification' parameter with the 'fit' option value with imshow:

hImshow = imshow(img,'InitialMagnification','fit')

From this MathWorks tutorial:

You can also specify the text string 'fit' as the initial magnification value. In this case, imshow scales the image to fit the current size of the figure window

See also this section of the imshow docs regarding 'InitialMagnification'. So, that should keep your figure window the same size.

That would solve the issue of losing the window maximization.


To get an image displayed scaled at 1 pixel to 1 point on the screen, you can create an axis of the correct size for the image, and display into that:

fpos = get(hFig,'Position')
axOffset = (fpos(3:4)-[size(img,2) size(img,1)])/2;
ha = axes('Parent',hFig,'Units','pixels',...
          'Position',[axOffset size(img,2) size(img,1)]);
hImshow = imshow(img,'Parent',ha);

Note that it is unnecessary to specify magnification since "If you specify the axes position (using subplot or axes), imshow ignores any initial magnification you might have specified and defaults to the 'fit' behavior", thus fitting to the axes specified by 'Parent'.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top