how can give an image as input in a fuction?

2 ビュー (過去 30 日間)
sara
sara 2014 年 9 月 19 日
コメント済み: sara 2014 年 9 月 22 日
hi I want to define a function that every time that I call it, it get an image address and do some process on this image I don't want to use 'input.tif' because I want to use this function in a loop and use the result of this for the next image...
if true
function [ maskedImage] = myprog(folder,baseFileName)
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
....
end
end
  4 件のコメント
Image Analyst
Image Analyst 2014 年 9 月 19 日
編集済み: Image Analyst 2014 年 9 月 19 日
We don't know what line 29 is. You forgot to post the error message - you just snipped out an almost useless part of it. Please post ALL the red text - don't snip out part of it or paraphrase it, just give it exactly. Presumably you called it with an actual names of some folder and file and didn't pass in 'folder' and 'baseFileName.tif'.
sara
sara 2014 年 9 月 19 日
line 29 is
grayImage = imread(fullFileName);
I attach my code below thanks Image Analyst

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

回答 (4 件)

Image Analyst
Image Analyst 2014 年 9 月 21 日
In myprog(), you have this line:
clear; % Erase all existing variables. Or clearvars if you want.
you must remember to never, ever have that in a function . If you use it, it must be in a script . What happens is that when you call clear, it wipes out the filename that you passed in.
  1 件のコメント
sara
sara 2014 年 9 月 22 日
oh thanks dear Image Analyst you are a clever person.. I have an other question... if I want to save the results of my code in an array Is there any way to save them ...I mean that I want to have an array that I can to save for 6 grayscale images in it...I couldn't save 6 images in an array.... thanks

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


Iain
Iain 2014 年 9 月 19 日
Ok, if by "image" you mean filename, then you can do it like this:
folder = 'C:\My Images\';
files = dir([folder '\*.jpg']);
for i = 1:numel(files)
filename = [folder files(i).name];
% ... etc etc
end
  1 件のコメント
David Young
David Young 2014 年 9 月 19 日
It's more robust to use
filename = fullfile(folder, files(i).name);

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


sara
sara 2014 年 9 月 19 日
編集済み: sara 2014 年 9 月 19 日
thanks but when I do what you say .I have this error
if true
Reference to a cleared variable fullFileName
end
..
I attach my codes I want to use myprog in loop ...do you know where did I do wrong??

sara
sara 2014 年 9 月 21 日
ok. thanks I am beginner can you help me more? I was in the folder that includes my images. I use this
if true
filename=ones(1,12);
files = dir(['\*.tif']);
for i = 1:12(files)
filename = files(i)
end
end
but I have this message:
if true
files =
0x1 struct array with fields:
name
date
bytes
isdir
datenum end
where is my error... thanks.

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by