im2grayが未定義となり先に進めません。

8 views (last 30 days)
Noruji Muto
Noruji Muto on 10 Dec 2021
Commented: Noruji Muto on 10 Dec 2021
お世話になります。
現在、動画内を移動する微粒子の数を取得するというコードの作成のために、
ヘルプセンターにある「オブジェクトのカウント」を参照しています。↓
このURLのコードの中で、「im2gray」という関数が出てくるのですが、コードをそのままコピペして自分の開発環境で実行すると
「関数または変数 'im2gray' が未定義です。」と表示されてしまい、先に進めず困っています。
image = im2gray(im2single(readFrame(hvfr)));
エラーは以下のとおりです。
関数または変数 'im2gray' が未定義です。
エラー: Untitled2 (line 2)
image = im2gray(im2single(readFrame(hvfr)));
実際に実行したコードの全体以下のとおりです。
VideoSize = [432 528];
filename = 'ecolicells.avi';
hvfr = VideoReader(filename);
hblob = vision.BlobAnalysis( ...
'AreaOutputPort', false, ...
'BoundingBoxOutputPort', false, ...
'OutputDataType', 'single', ...
'MinimumBlobArea', 7, ...
'MaximumBlobArea', 300, ...
'MaximumCount', 1500);
% Acknowledgement
ackText = ['Data set courtesy of Jonathan Young and Michael Elowitz, ' ...
'California Institute of Technology'];
hVideo = vision.VideoPlayer;
hVideo.Name = 'Results';
hVideo.Position(1) = round(hVideo.Position(1));
hVideo.Position(2) = round(hVideo.Position(2));
hVideo.Position([4 3]) = 30+VideoSize;
which
frameCount = int16(1);
while hasFrame(hvfr)
% Read input video frame
image = im2gray(im2single(readFrame(hvfr)));%ここでエラーが発生
% Apply a combination of morphological dilation and image arithmetic
% operations to remove uneven illumination and to emphasize the
% boundaries between the cells.
y1 = 2*image - imdilate(image, strel('square',7));
y1(y1<0) = 0;
y1(y1>1) = 1;
y2 = imdilate(y1, strel('square',7)) - y1;
th = multithresh(y2); % Determine threshold using Otsu's method
y3 = (y2 <= th*0.7); % Binarize the image.
Centroid = step(hblob, y3); % Calculate the centroid
numBlobs = size(Centroid,1); % and number of cells.
% Display the number of frames and cells.
frameBlobTxt = sprintf('Frame %d, Count %d', frameCount, numBlobs);
image = insertText(image, [1 1], frameBlobTxt, ...
'FontSize', 16, 'BoxOpacity', 0, 'TextColor', 'white');
image = insertText(image, [1 size(image,1)], ackText, ...
'FontSize', 10, 'AnchorPoint', 'LeftBottom', ...
'BoxOpacity', 0, 'TextColor', 'white');
% Display video
image_out = insertMarker(image, Centroid, '*', 'Color', 'green');
step(hVideo, image_out);
frameCount = frameCount + 1;
end
エラーは全く同じものが出てきます。
どうか皆様のお知恵をお貸し下さい。

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 10 Dec 2021
ドキュメントの最後に書いてあるのですが、im2gray関数は「R2020b で導入」という事なので、R2019aでは使用できません。代わりに、もっと前から在るrgb2gray関数でも機能すると思います。
  1 Comment
Noruji Muto
Noruji Muto on 10 Dec 2021
ご回答ありがとうございます。
そのとおりにしたところ、無事に動きました。
ありがとうございました。

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!