to create a loop to assign names
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm analyzing multiple images at the same time, and in my function multiple different figures show different features. I wanted to create a code to determine each individual' specific name. here is my code to name the figures. This is not entire function but I only need to learn how to change the name. I don't know what I should write the line of k=
function AnalyzeImage(originalImage, lowThreshold, highThreshold)
fontSize = 14;
for k= [originalImage]
figure(k); imagesc(originalImage); colormap(gray(256));
title(k, 'FontSize', fontSize);
impixelinfo;
end
...
end
I will be grateful if you could help me
Thank you
4 个评论
dpb
2022-8-21
But when you write
AnalyzeImage2(flowerimage, 50, 150)
flowerimage is a variable name and you've set it somewhere to be that fixed name.
Somewhere that had to have been defined as being the image of a flower (or something else), probably in the name of the file in which the image resides, one would guess.
To make the code more generic, you need to hold those file name(s) as a variable and besides using it to open the file (also into a generic variable name), use the name part of the file as the title text. That presumes that that text is in the filename as wanted...
d=dir(fullfile('yourDataImagesFolder','*image.jpg')); % find the .jpt files ending in "image"
for i=numel(d) % iterate over the list returned
[~,imageTitle]=fileparts(d(i).name); % get the name from the filename
image=imread(fullfile(d(i).folder,d(i).name)); % read the file
AnalyzeImage2(imageTitle,image, 50, 150) % call the analysis routine with image, title
end
And then the function would need similar modification to use the new title variable something like--
function AnalyzeImage2(imageTitle, originalImage, lowThreshold, highThreshold)
fontSize = 14;
binaryImage = (originalImage >= lowThreshold) & (originalImage <= highThreshold);
binaryImage = imfill(binaryImage, 'holes');
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8); % Label each blob so can do calculations on it
coloredLabels = label2rgb(labeledImage,'hsv','k','shuffle'); % pseudo random color labels
% Display various images
figure(); imagesc(originalImage); colormap(gray(256));
title(imageTitle, 'FontSize', fontSize);
impixelinfo; % Set it up so user can mouse around over image and see (x,y) and Gray Level or RGB color in status bar in the lower left of the figure.
end
The above function has some other peculiarities, however, in that the binaryImage and all the other machinations don't have any bearing on anything else...all that's done is to show the input image. One presumes other stuff there has been elided for brevity, maybe???
But, the point is, you've got to get the title text from somewhere or otherwise define it as the text to be displayed; I've just shown one way if the filens were named in a particular manner (and a not very flexible one at that). Otherwise, you need a database of names to display to go along with the images defined somehow/somewhere.
回答(1 个)
Steven Lord
2022-8-21
If you're just using this to obtain the name of the variable in the workspace from which your function was called, use inputname but make sure to include some code to handle the case where the input argument doesn't have a name.
x = 1:10;
showInputName(x)
showInputName(1:10) % The input has no name
function showInputName(~)
fprintf("The name of the input argument is %s\n", inputname(1))
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!