エラー: videoinput​ が出て、USBカメ​ラが使えません。

8 ビュー (過去 30 日間)
james,k
james,k 2019 年 9 月 25 日
コメント済み: Naoya 2019 年 9 月 27 日
大学生の初心者です。
こちらのサイトの赤色追跡のプログラムを実行して、Let's noteのインカメで問題なく動かすことはできましたが
winvideoの所をwebcamに変更してUSBカメラをさして実行しようとすると
エラー: videoinput (line 219)
Invalid ADAPTORNAME specified. Type 'imaqhwinfo' for a list of available ADAPTORNAMEs. Image acquisition adaptors may be available as downloadable support
packages. Open Add-Ons Explorer to install additional adaptors.
こちらのエラーが出て imaqhwinfo を打つと
InstalledAdaptors: {'dcam' 'kinect' 'winvideo'}
MATLABVersion: '9.4 (R2018a)'
ToolboxName: 'Image Acquisition Toolbox'
ToolboxVersion: '5.4 (R2018a)'
となります、また Add-Ons Explorer をクリックしてアドオンエクスプローラーを開き出てくるほとんどのものをインストールしましたがダメでした。
また、他にUSBカメラを使った時にMATLAB Support Package for USB Webcams もインストール済みです。
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput( 'winvideo',1, 'YUY2_320x240');
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
figure;hold on
while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
%a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
%set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
clear all
sprintf('%s','That was all about Image tracking, Guess that was pretty easy :) ')
このコードの3行目です。
操作の勉強不足でできないのか、インストールするパッケージが間違っているのか分かりません。
USBカメラでプログラムを動かせるようにしたいです。

回答 (2 件)

Naoya
Naoya 2019 年 9 月 26 日
MATLAB Support Package for USB Webcams は、 Image Acquisition Toolbox ではなく、MATLAB 本体で提供されるサポートパッケージとなります。
そのため、 videoinput関数でアダプタ名を指定するのではなく、
camObj = webcam;
のように、webcamオブジェクトを定義して、
preview(camObj)
で映像のプレニューを行い、
img = snapshot(camObj);
で単一フレームをキャプチャします。
<参考>
  1 件のコメント
james,k
james,k 2019 年 9 月 26 日
ご回答ありがとうございます。
USBカメラを呼び出すことができましたが、エラーが出てプログラムは作動しませんでした。
エラー: set
無効なハンドル
エラー: redobjecttrack_working (line 10)
set(img, 'FramesPerTrigger', Inf);
初めに回答にあった、構文を入れて
vid = videoinput( 'winvideo',1, 'YUY2_320x240');
の部分を削除し、vidをすべて、imgに変えたところこのようになりました。
申し訳ございません操作に慣れてなく回答に合った通りに出来ているか不安です。別の問題なのか、手順が間違っているのかよろしければご回答お願い致します。

サインインしてコメントする。


james,k
james,k 2019 年 9 月 26 日
ご回答ありがとうございます。
USBカメラを呼び出すことができましたが、エラーが出てプログラムは作動しませんでした。
エラー: set
無効なハンドル
エラー: redobjecttrack_working (line 10)
set(img, 'FramesPerTrigger', Inf);
初めに回答にあった、構文を入れて
vid = videoinput( 'winvideo',1, 'YUY2_320x240');
の部分を削除し、vidをすべて、imgに変えたところこのようになりました。
申し訳ございません操作に慣れてなく回答に合った通りに出来ているか不安です。別の問題なのか、手順が間違っているのかよろしければご回答お願い致します。
  1 件のコメント
Naoya
Naoya 2019 年 9 月 27 日
webcam は、 MATLAB 本体で提供される機能になりますので、基本的には preview と snapshot の 2種類の機能のみとなります。
※ snapshot(camObj) は、 videoinput オブジェクトでいう getsnapshot(vid) に相当します。
Image Acqusition Toolbox の videoinput オブジェクトのsetメソッドのように、 1回のトリガーで何フレーム取得したり、 フレームを取得する間隔を設定するといった機能はありません。

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!