简体   繁体   English

如何在MATLAB GUI中添加图像?

[英]How to add image in MATLAB GUI?

我想在两个图像之间来回切换,如闪烁:第一个图像为1秒,第二个图像为1秒。

I'm not totally sure of what you want to do (specifically what type of images you are trying to display), but here's some sample code that may do what you want: 我不完全确定你想做什么(特别是你试图显示什么类型的图像),但这里有一些示例代码可以做你想要的:

image1 = imread('cameraman.tif');  % Load a test image
image2 = imread('circles.png');    % Load another test image

hAxes = gca;  % Get a handle to the current axes

for iLoop = 1:5,  % Loop five times
  imshow(image1,'Parent',hAxes);
  pause(1);
  imshow(image2,'Parent',hAxes);
  pause(1);
end

I used the general function IMSHOW , but this sometimes changes other properties of the figure/axes and that may not be to your liking (since you mention adding this to an existing GUI). 我使用了通用函数IMSHOW ,但这有时会改变图形/轴的其他属性,这可能不符合您的喜好(因为您提到将其添加到现有GUI)。 You may want to use the IMAGE function instead. 您可能希望使用IMAGE功能。 Also, instead of the for loop you could use a while loop that stops switching images when a condition is met (such as a button press). 此外,代替for循环,您可以使用while循环,在满足条件时停止切换图像(例如按下按钮)。

How are your images stored in Matlab? 你的图像如何存储在Matlab中? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. 作为matlab电影或3或4维矩阵,取决于图像是彩色还是灰度。 Also, if you have the image processing toolbox, implay and immovie . 此外,如果你有图像处理工具箱, implayimmovie Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. 假设您的图像采用mxnx3xk (rgb颜色)或mxnxk (灰度)矩阵的另一种选择。 Then the following should work. 然后以下应该工作。 Assuming the following 假设如下

  • Img - matrix storing image data either with dimensions mxnx3xk or mxnxk Img - 矩阵存储图像数据,尺寸为mxnx3xkmxnxk

  • handles.imageAxes - handle for the axis you want to display the image (set the tag of the axis to imageAxes in GUIDE) handles.imageAxes - 要显示图像的轴的句柄(在GUIDE中将轴的标记设置为imageAxes)

Now you can loop through Img 现在你可以循环Img了

for i=1:k
    % display the i^th image use `Img(:,:,i)` for a gray scale stack
    image(Img(:,:,:,i),'parent',handles.imageAxes);
    pause(1) % pause one second
end

that's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM