这里是 MATLAB 初学者。我正在尝试编写一个将从文件夹中加载图像的类,这就是我所拥有的:
classdef ImageLoader
%IMAGELOADER Summary of this class goes here
% Detailed explanation goes here
properties
currentImage = 0;
filenames={};
filenamesSorted={};
end
methods
function imageLoader = ImageLoader(FolderDir)
path = dir(FolderDir);
imageLoader.filenames = {path.name};
imageLoader.filenames = sort(imageLoader.filenames);
end
function image = CurrentImage(this)
image = imread(this.filenames{this.currentImage});
end
function image = NextImage(this)
this.currentImage = this.currentImage + 1;
image = imread(this.filenames{this.currentImage});
end
end
end
这就是我所说的:
i = ImageLoader('football//Frame*');
image=i.NextImage;
imshow(image);
这些文件被命名为 Frame0000.jpg、Frame0001.jpg ... 等等。我希望构造函数加载所有文件名,这样我就可以通过调用来检索下一个文件i.NextImage
,但我无法让它工作。
得到它的工作。
班级:
classdef ImageLoader
%IMAGELOADER Summary of this class goes here
% Detailed explanation goes here
properties(SetAccess = private)
currentImage
filenames
path
filenamesSorted;
end
methods
function imageLoader = ImageLoader(Path,FileName)
imageLoader.path = Path;
temp = dir(strcat(Path,FileName));
imageLoader.filenames = {temp.name};
imageLoader.filenames = sort(imageLoader.filenames);
imageLoader.currentImage = 0;
end
function image = CurrentImage(this)
image = imread(this.filenames{this.currentImage});
end
function [this image] = NextImage(this)
this.currentImage = this.currentImage + 1;
image = imread(strcat(this.path,this.filenames{this.currentImage}));
end
end
end
称呼:
i = ImageLoader('football//','Frame*');
[i image]=i.NextImage;
imshow(image);