如何在MATLAB GUI中添加图像?

时间:2021-10-07 19:34:59

I want to switch back and forth between two images, like blinking: 1 second for the first image and one second for second image.

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

2 个解决方案

#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). You may want to use the IMAGE function instead. 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).

我使用了通用函数IMSHOW,但这有时会改变图形/轴的其他属性,这可能不符合您的喜好(因为您提到将其添加到现有GUI)。您可能希望使用IMAGE功能。此外,代替for循环,您可以使用while循环,在满足条件时停止切换图像(例如按下按钮)。

#2


How are your images stored in Matlab? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. Also, if you have the image processing toolbox, implay and immovie. Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. Then the following should work. Assuming the following

你的图像如何存储在Matlab中?作为matlab电影或3或4维矩阵,取决于图像是彩色还是灰度。此外,如果你有图像处理工具箱,implay和immovie。假设您的图像采用mxnx3xk(rgb颜色)或mxnxk(灰度)矩阵的另一种选择。然后以下应该工作。假设如下

  • Img - matrix storing image data either with dimensions mxnx3xk or mxnxk

    Img - 矩阵存储图像数据,尺寸为mxnx3xk或mxnxk

  • 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.

#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). You may want to use the IMAGE function instead. 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).

我使用了通用函数IMSHOW,但这有时会改变图形/轴的其他属性,这可能不符合您的喜好(因为您提到将其添加到现有GUI)。您可能希望使用IMAGE功能。此外,代替for循环,您可以使用while循环,在满足条件时停止切换图像(例如按下按钮)。

#2


How are your images stored in Matlab? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. Also, if you have the image processing toolbox, implay and immovie. Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. Then the following should work. Assuming the following

你的图像如何存储在Matlab中?作为matlab电影或3或4维矩阵,取决于图像是彩色还是灰度。此外,如果你有图像处理工具箱,implay和immovie。假设您的图像采用mxnx3xk(rgb颜色)或mxnxk(灰度)矩阵的另一种选择。然后以下应该工作。假设如下

  • Img - matrix storing image data either with dimensions mxnx3xk or mxnxk

    Img - 矩阵存储图像数据,尺寸为mxnx3xk或mxnxk

  • 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.