写一个输出图像的MATLAB函数
时间: 2024-03-23 17:37:56 浏览: 125
以下是一个简单的MATLAB函数,用于输出图像:
```matlab
function outputImage = outputImage(image, filename, format)
% 输出图像函数
% 输入参数:
% - image: 待输出的图像
% - filename: 输出文件名
% - format: 输出文件格式(可选参数,默认为PNG格式)
% 输出参数:
% - outputImage: 输出的图像
% 如果未指定输出格式,默认为PNG格式
if nargin < 3
format = 'png';
end
% 将图像输出为指定格式的文件
imwrite(image, filename, format);
% 读取输出的图像
outputImage = imread(filename);
end
```
使用示例:
```matlab
% 读取图像
image = imread('peppers.png');
% 输出图像,并保存为JPEG格式
outputImage = outputImage(image, 'output.jpg', 'jpg');
```
该函数使用MATLAB内置函数imwrite将输入的图像保存为指定格式的文件,然后再使用imread读取输出的图像,并将其作为函数的输出。
相关问题
写一个输出两个图像的MATLAB函数
好的,以下是一个输出两个图像的MATLAB函数示例:
```matlab
function plotTwoImages(img1, img2)
% 该函数用于输出两个图像
% 输入参数:img1,img2为两个要输出的图像
subplot(121); % 将图像1显示在左边
imshow(img1);
title('Image 1');
subplot(122); % 将图像2显示在右边
imshow(img2);
title('Image 2');
end
```
使用方法:
假设有两张图片`img1`和`img2`,可以使用以下命令调用该函数:
```matlab
plotTwoImages(img1, img2);
```
函数会将两张图片分别显示在左右两个子图中。
阅读全文
相关推荐













