Display image in axes Matlab GUI.

조회 수: 96 (최근 30일)
Alexandru Vasile
Alexandru Vasile 2015년 6월 5일
댓글: Walter Roberson 2021년 3월 2일
Hello!
I want to display an image in an axes Matlab GUI. Therefore, I selected an axes and a button to trigger the moment. In the button function I wrote:
myImage = imread('as.jpg');
axes(handles.axes7);
imshow(myImage);
The problem is that the image doesn't cover the all surface of the axes. Is it displayed little in the center of the axes. How can I display my image on entire surface of the axes?
Thank you!
  댓글 수: 1
B.k Sumedha
B.k Sumedha 2015년 6월 5일
Check the size of the image

댓글을 달려면 로그인하십시오.

채택된 답변

Ingrid
Ingrid 2015년 6월 5일
this indeed means that the size of your image is much smaller than the size of your axes. To make it work on different computers (with different resolutions) and if you have set all units to normalized to obtain this you can use this code to circumvent the problem:
myImage = imread('as.jpg');
set(handles.axes7,'Units','pixels');
resizePos = get(handles.axes7,'Position');
myImage= imresize(myImage, [resizePos(3) resizePos(3)]);
axes(handles.axes7);
imshow(myImage);
set(handles.axes7,'Units','normalized');
  댓글 수: 4
Runo Insan
Runo Insan 2020년 4월 11일
So, we should not need a panel to use, we need Axis to input an image into it on Matlab gui. thanks.
Jian Kang
Jian Kang 2020년 12월 7일
Thank you very much. It is very helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Image Analyst
Image Analyst 2015년 6월 5일
I know you already accepted an answer but I don't think you have to do all that - I never do and my images fill the axes. That would be a nightmare if you had to do that every time.
In my experience, the cause for this behavior is that you set "hold on" on that axes sometime in the past. Then when you go to display the new image, the axes size is sort of "locked" to the size of the image that was in there before you put hold on. To get around this, call "hold off" after you call axes() and before you call imshow(). If even that doesn't fix it, call "cla reset" between axes() and imshow(). That will certainly fix it and restore the axes back to its initial default state, which is to display the images with 'InitialMagnification' as 'fit'.
If even that doesn't fix it (never had this happen before), then you must not have your imshow() preferences set to fit. Look at iptprefs and at the 'ImshowInitialMagnification' setting. It should be set to 'fit'
  댓글 수: 5
Victoria Odoemelam
Victoria Odoemelam 2021년 3월 1일
Good evening all.. please I need to write if statement for images to be displayed if a particular image is read...
Walter Roberson
Walter Roberson 2021년 3월 2일
dinfo = dir('/MATLAB/toolbox/images/*/*.jpg');
filenames = fullfile({dinfo.folder}, {dinfo.name});
[~, barenames, ~] = cellfun(@fileparts, filenames, 'uniform', 0)
barenames = 1x38 cell array
{'baby'} {'car1'} {'car2'} {'car_1'} {'car_2'} {'car_3'} {'car_4'} {'colorCheckerTestImage'} {'eSFRTestImage'} {'flamingos'} {'foggyroad'} {'foggysf1'} {'foggysf2'} {'foosball'} {'football'} {'greens'} {'hallway'} {'hands1'} {'hands2'} {'indiancorn'} {'llama'} {'lowlight_1'} {'lowlight_2'} {'micromarket'} {'office_1'} {'office_2'} {'office_3'} {'office_4'} {'office_5'} {'office_6'} {'parkavenue'} {'peacock'} {'sevilla'} {'sherlock'} {'strawberries'} {'trailer'} {'wagon'} {'yellowlily'}
if ismember('cameraman', barenames)
disp('found camerman!');
else
disp('no cameraman');
end
no cameraman
if ismember('flamingos', barenames)
disp('found flamingos');
else
disp('no flamingos')
end
found flamingos
if ismember('lena', barenames)
disp('found lena')
else
disp('no lena')
end
no lena

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2015년 6월 5일
You can pass x and y coordinates to imshow() to indicate where you want the image to be placed in data coordinates. Please read the documentation as the positioning is a little unusual.
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 5월 6일
편집: Walter Roberson 2017년 5월 6일
Peter Manley-Cooke:
Look at the XData and YData options
These XData and YData are passed by imshow() to image(), so to get the full explanation you need to look there https://www.mathworks.com/help/matlab/ref/image.html#inputarg_x
=== quote ===
  • Two-element vector — Use the first element as the location for the center of C(1,1) and the second element as the location for the center of C(m,n), where [m,n] = size(C). If C is a 3-D array, then m and n are the first two dimensions. Evenly distribute the centers of the remaining elements of C between those two points.
The width of each pixel is determined by the expression:
(x(2)-x(1))/(size(C,2)-1)
If x(1) > x(2), then the image is flipped left-right.
  • Scalar — Center C(1,1) at this location and each following element one unit apart.
=== end quote ===
Notice the positions are centers of pixels, so if you used whole units then to put the bottom left corner of an image at the origin, you would have to specify the XData and YData as starting from (1/2, 1/2), instead of the more natural specification of (0, 0)
Peter Manley-Cooke
Peter Manley-Cooke 2017년 5월 6일
Once again, the famous Walter has the answers. Where TMW fail to mention any of this connection which would have been useful. Anyhoo, this works a treat. Thanks Walter.

댓글을 달려면 로그인하십시오.


thinh dang
thinh dang 2018년 1월 18일
i have a problem that the guide work fluently if i run in command window. but if i open the guide(.fic), it doesn't work! help me please! thank you!
  댓글 수: 1
Image Analyst
Image Analyst 2018년 1월 18일
I made a quick launch shortcut that says "guide". Or you can also type guide onto the command line. Clicking on the .fig file in the current folder panel doesn't work. I know it brings up something but it's like a crippled version that doesn't really function, so don't try to launch either your program or the GUIDE interface by double clicking on the fig file. Use the green run triangle either in MATLAB or in GUIDE.

댓글을 달려면 로그인하십시오.


robert arnold
robert arnold 2020년 5월 7일
how can i make the axis automatically adjust to anysize image?
  댓글 수: 1
Image Analyst
Image Analyst 2020년 5월 7일
It does that already, doesn't it? No matter what size image you're trying to show, it will pick a good size for an axes (if you don't already have an active one), and then fit the desired image into it. Please show a screenshot where it was not adjusted automatically properly and say what you'd want it to look like.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by