1

ここで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);
4

1 に答える 1

1

最後にオブジェクト自体の値を明示的に更新せずに、オブジェクトの状態を変更することはできません(ポインターをインクリメントする場合のように)。currentimageAFAIKのすべての関数呼び出しは、オブジェクトbyvalを渡します。つまり、NextImageのローカルコピーthis(現在のオブジェクトへのポインタ/参照ではなく、コピー)を変更するだけです。

したがって、あなたができることはあなたのメソッドを次のように書くことです

  function [this image] = NextImage(this)
        this.currentImage = this.currentImage + 1;
        image = imread(this.filenames{this.currentImage});
  end

そしてそれを次のように呼びます

 [i image]=i.NextImage;
于 2012-11-26T19:48:00.613 に答える