How can I keep figure window maximized when showing an image?

5 ビュー (過去 30 日間)
C
C 2013 年 11 月 14 日
編集済み: Image Analyst 2022 年 8 月 3 日
I am building a programmatic GUI 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, so desktop clutter etc. will not disturb the visual impression. An image will be displayed for a few seconds, then removed, and some sliders will appear for the rating. Next, the sliders will be hidden, and a new image will appear, etc.
What happens is, it starts out fine with the maximized figure window, right until I load an image and display it, using imshow or image command - then the figure window resizes to fit the image. I could maximize it back, but that causes some disturbing motion of the window frame.
How can I keep the window maximized, and display an image at 1:1 ratio (not scaled to fit a maximized window), so one pixel in the image fits no more/less than one "pixel" on screen?
Below is an example of what I have now (using Matlab R2013a):
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);
% creating a handle for imshow, hiding it for now
img = imread('musik.png');
hImshow = imshow(img);
set(hImshow,'Visible','off');
% Show the image
% This is where Matlab decides to modify the figure window,
% so it fits the image rather than staying maximized.
set(hImshow,'Visible','on');
% How can I display an image in an existing-and-maximized figure
% window, so that the window remains maximized, and the image
% is displayed at actual size (not scaled), so one pixel in the
% image is displayed by one "pixel" on the monitor?
Best regards, Christian

採用された回答

C
C 2013 年 11 月 15 日
編集済み: C 2013 年 11 月 15 日
A kind user over at stackoverflow.com helped solve this issue:
In short, here's the solution:
hFig = figure('Name','APP',...
'Numbertitle','off',...
'Position', [0 0 screenWidth screenHeight],...
'WindowStyle','modal',...
'Color',[0.5 0.5 0.5],...
'Toolbar','none');
img = imread('someImage.png');
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);
By first creating the figure window, then axes of proper size and position, and then displaying the image with imshow, using the axes as parent, the window resizing is avoided.
Cheers, Christian
  1 件のコメント
NHOODA
NHOODA 2019 年 5 月 24 日
Thanks a lot....u made my day :D

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2013 年 11 月 14 日
編集済み: Image Analyst 2022 年 8 月 3 日
Issue this command right after you call imshow():
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
You can now (after R2014b I believe) do it this way instead:
g = gcf;
g.WindowState = 'maximized';
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 6 月 14 日
sara commented "Great"
Muhammad Ghani
Muhammad Ghani 2022 年 8 月 3 日
awesome

サインインしてコメントする。


C
C 2013 年 11 月 14 日
Thank you for the quick response.
I tried the suggestion, but it causes a "flicker" of the window (it is noticeable that the window is being resized quickly), and the displayed image is scaled to fill out the window.
I was hoping for flicker-free and not-scaled...
Best regards, Christian
  1 件のコメント
Image Analyst
Image Analyst 2013 年 11 月 14 日
That would be nice. Sorry but I don't know of any other way.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by