1

プッシュボタンを押すたびに画像を上書きせずに保存したい。オリジナルを上書きせずに画像を保存する方法を教えてください。私がやりたいことは、プッシュボタンを押すたびに、元の画像を削除せずに一度に 1 つの画像を生成することです。

デジタル カメラと同じように、トリガー ボタンを押すたびに 1 つの画像が保存され、ファイル名はimage1.jpgになります。つまり、もう一度トリガーを押すと、もう一度 1 枚の画像がキャプチャされ、ファイル名はimage2.jpgなどになります。

ここに私のコードがあります:

counter = 1;  %initialize filename increment
vid = videoinput('winvideo',2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Task\images\image_' ,num2str(counter), '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);
counter = counter +1;  %counter should increment each time you push the button

私のコードはファイル名 image1.jpg を保存して上書きし続けます。物事を明確にするために

プッシュボタンを1回押すと、1枚の画像が保存されます。

プッシュボタンを押すたびにブロックコード全体を呼び出すようなものです。皆さんが私を助けてくれることを願っています。私は今本当に困っています:(ありがとう:)

4

3 に答える 3

2

これがそのプッシュボタンのコールバック関数を構成するコードである場合、確かに、プッシュするたびにブロック全体が実行されます。

その場合は、次のように変更する必要があります。

%// initialize filename increment
persistent counter;
if isempty(counter)
    counter = 1; end

vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);

%// this is where and what your image will be saved
savename = [...
    'C:\Users\Sony Vaio\Documents\Task\images\image_', ...
    num2str(counter), '.jpg']; 
imwrite(img, savename);

%// counter should increment each time you push the button
counter = counter + 1;  

または、実際に存在するファイルを確認し、シーケンス内の次の論理ファイル名を使用できます。

vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);

%// this is where and what your image will be saved
counter  = 1;
baseDir  = 'C:\Users\Sony Vaio\Documents\Task\images\';
baseName = 'image_';
newName  = [baseDir baseName num2str(counter) '.jpg'];
while exist(newName,'file')
    counter = counter + 1;
    newName = [baseDir baseName num2str(counter) '.jpg'];
end    
imwrite(img, newName);
于 2013-09-12T05:55:35.893 に答える
0

そのボタンを押すたびに、最初のステートメントのためにカウンターの値が 1 にリセットされます。

カウンター = 1

したがって、エラー。

counter = length(dir('*.jpg')) + 1; %Counts the number of .jpg files in the directory

それは仕事をするはずです。

于 2014-05-23T09:19:24.930 に答える
0

Zaher: 私は、MATLAB を使用して、カメラからの画像処理と画像取得に関するオンライン プログラムを作成しています。数秒ごとに画像を受信すると、カメラの写真が取得されます。写真は、統計的工程管理図に保存および処理する必要があります。画像取得プログラムの後の最初の画像がハングして停止したとき。カメラからオンラインで 10 秒ごとに画像を取得するようにコーディングしてください。この画像は、統計的プロセス管理に使用できます。感謝

于 2017-01-10T18:20:45.287 に答える