이 페이지는 기계 번역을 사용하여 번역되었습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.
레이저 추적
이 예제는 움직이는 레이저 점을 추적하는 방법을 보여줍니다.
Image Acquisition Toolbox™를 사용하면 카메라의 이미지 데이터 스트림을 MATLAB®으로 직접 수집합니다. 이러한 이미지는 카메라 시야에서 객체를 추적하는 데 사용됩니다. 이 예에서 추적되는 객체는 레이저 포인터에 의해 생성된 점입니다.
MATLAB를 실행하는 컴퓨터의 모니터가 카메라 시야에 배치되고 레이저 포인터가 MATLAB 피규어 창에 빨간 점을 비춥니다. 레이저 포인터가 움직이는 동안 카메라는 MATLAB 피규어 창의 이미지를 얻는 데 사용됩니다. 레이저 도트의 움직임을 추적함으로써 레이저 포인터는 마우스와 비슷한 포인터 장치로 사용될 수 있습니다.
첫 번째 작업은 획득한 이미지 픽셀과 MATLAB 그림 축 사이의 관계를 확립하기 위해 데이터를 보정하는 것입니다. 이러한 관계가 확립되면 레이저 도트가 MATLAB 그림 창 내에서 움직이는 것을 추적할 수 있습니다.
이 예제에서는 이미지 처리에 도움이 되는 일련의 유틸리티 함수를 사용합니다. 이러한 유틸리티 함수를 사용하려면 Image Processing Toolbox™가 필요합니다.
물리적 설정
MATLAB가 실행 중인 컴퓨터 화면에 카메라를 초점을 맞춥니다.
![]()
방 안의 주변 조명을 최소화하는 것이 가장 좋습니다. 이 예제는 프로젝터를 사용하여 강당에서 성공적으로 실행되었습니다.
수집 구성하기
이미지 수집 장치는 교정 및 레이저 추적을 수행하기 위한 이미지 데이터를 수집하는 데 사용됩니다. 사용되는 장치는 일반적인 Windows® 비디오 웹캠입니다.
% Access and configure a device. vid = videoinput('winvideo', 1, 'RGB24_320x240'); vid.FramesPerTrigger = 1; vid.TriggerRepeat = Inf; triggerconfig(vid,'manual')
교정 화면 만들기
생성된 교정 화면은 레이저 포인터의 최상의 대비를 얻기 위해 의도적으로 검은색으로 설정되어 있습니다. 일부 시스템은 제목 표시줄의 창 색상을 어둡게 설정하면 더 잘 작동합니다.
빨간색 레이저를 사용하므로 이미지의 빨간색 평면이 관심 있는 유일한 색상 평면입니다. 교정 사각형은 빨간색 평면에서 "보이지 않게" 보이도록 파란색으로 표현되었습니다.
% Create the laser figure window. laserFig = figure; hBox = plot([0 0 1 1 0], [0 1 1 0 0], 'b-'); hold on % Set up calibration screen. Modify the cursor so it does not % interfere with the calibration. hTarget = plot(0, 0, 'yo'); ax = gca; ax.Color = [0, 0, 0]; laserFig.Color = [0, 0, 0]; laserFig.Menubar = 'none'; laserFig.Pointer = 'custom'; laserFig.PointerShapeCData = NaN(16, 16);
![]()
카메라 위치 지정
파란색 사각형만 보이도록 카메라 위치를 조정하세요.
% Display positioning information. posText = sprintf('%s\n%s', ... 'Position the camera and ensure the blue box', ... 'is the only thing in the camera''s view.'); infoText = text(0, -0.2, posText, 'Color', [1 1 1]); axis([-0.2 1.2 -0.2 1.2]) axis equal
![]()
% Using the preview window, request that the camera be positioned such % that the view is of the blue box and little else. preview(vid) smallFigPos = laserFig.Position; laserFig.Position = get(0, 'ScreenSize'); disp('Waiting for camera to be positioned...press any key to continue.') pause
Waiting for camera to be positioned...press any key to continue.
이미지 보정 수행
이제 카메라가 올바른 영역에 초점이 맞춰졌으므로 상자의 네 모서리에 각각 표적이 그려집니다. 교정은 파란색 사각형의 각 모서리에 레이저를 조준하여 수행되며, 이를 통해 카메라 픽셀 좌표(이미지)와 MATLAB 축 좌표(사각형) 사이의 관계를 설정할 수 있습니다. 표시된 각 대상에 대해:
레이저를 조준해야 함을 나타내는 소리를 출력합니다.
프레임이 획득되려고 한다는 것을 나타내는 소리를 출력합니다.
수집 장치를 트리거
획득한 이미지 프레임에 접근하여 픽셀 좌표에서 레이저 위치를 확인합니다.
레이저 위치는 빨간색 평면의 임계값을 정하고 높은 강도 값을 찾아 결정됩니다. 일부 웹캠의 광학 장치 불량으로 인해 발생하는 고스트 이미지로 인해 레이저가 가려지지 않도록 몇 가지 추가 처리가 수행됩니다. 또한 레이저 도트가 실제로 화면에 존재했다는 것도 확인되었습니다.
% Provide calibration instructions. calibText = sprintf('%s\n%s', ... 'Aim the laser pointer on each target as it appears.', ... 'Hold the laser on the target until the target moves.'); infoText.String = calibText;
![]()
% Start the acquisition and create a new figure to display % calibration results in a MATLAB SPY plot. start(vid) spyFig = figure; % Target 1... figure(laserFig); hTarget.XData = 0; hTarget.YData = 0; sound(1), pause(2) sound(1), trigger(vid); acqResults{1} = getdata(vid, 1); [xCalib(1), yCalib(1), laserSights] = util_findlaser(acqResults{1}); figure(spyFig); spy(laserSights) title('Target 1: Suspected Laser Sighting')
![]()
% Target 2... figure(laserFig); hTarget.XData = 0; hTarget.YData = 1; sound(1), pause(2) sound(1), trigger(vid); acqResults{2} = getdata(vid, 1); [xCalib(2), yCalib(2), laserSights] = util_findlaser(acqResults{2}); figure(spyFig); spy(laserSights) title('Target 2: Suspected Laser Sighting')
![]()
% Target 3... figure(laserFig); hTarget.XData = 1; hTarget.YData = 1; sound(1), pause(2) sound(1), trigger(vid); acqResults{3} = getdata(vid, 1); [xCalib(3), yCalib(3), laserSights] = util_findlaser(acqResults{3}); figure(spyFig); spy(laserSights) title('Target 3: Suspected Laser Sighting')
![]()
% Target 4... figure(laserFig); hTarget.XData = 1; hTarget.YData = 0; sound(1), pause(2) sound(1), trigger(vid); acqResults{4} = getdata(vid, 1); [xCalib(4), yCalib(4), laserSights] = util_findlaser(acqResults{4}); figure(spyFig); spy(laserSights) title('Target 4: Suspected Laser Sighting')
![]()
% Close the SPY plot and stop the acquisition.
close(spyFig)
stop(vid);
교정 결과
획득한 이미지와 각 대상에 대해 계산된 레이저 포인터 좌표를 표시합니다. 노란색 조준선이 각 이미지에서 적절한 위치에 배치되므로 처리 결과가 검증됩니다.
% Target 1 results...
calibFig = figure;
util_plotpos(acqResults{1}, xCalib(1), yCalib(1));
![]()
% Target 2 results...
util_plotpos(acqResults{2}, xCalib(2), yCalib(2));
![]()
% Target 3 results...
util_plotpos(acqResults{3}, xCalib(3), yCalib(3));
![]()
% Target 4 results...
util_plotpos(acqResults{4}, xCalib(4), yCalib(4));
![]()
% Close the figure illustrating calibration results.
close(calibFig)
레이저 추적
수집을 시작하고 수집된 데이터를 지정된 횟수만큼 처리합니다. 처리 과정은 획득한 이미지에서 레이저를 찾고 픽셀과 MATLAB 축 좌표로 레이저 위치를 결정하는 것으로 구성됩니다.
흥미로운 점을 찾아보려면 레이저 포인터를 사용하여 파란색 상자 안에 문자 'M' (MATLAB)을 "그리십시오".
% Update instructions on laser screen. figure(laserFig); infoText.String = 'Move the laser pointer within the blue box.'; % Start the acquisition. For each iteration: % % * output a sound to indicate a frame is about to be acquired % * trigger the device % * process the acquired image and locate the laser % * convert pixel coordinates to MATLAB axis coordinates laser.x = []; laser.y = []; start(vid) for i = 1:100, % Acquire an image frame and determine the % camera pixel coordinates. sound(1), trigger(vid); frame = getdata(vid, 1); [x, y] = util_findlaser(frame); if ~isnan(x) && ~isnan(y), % If coordinates were valid, ensure the camera pixel coordinate % was in the calibration range. x = max([x min(xCalib([1 2]))]); x = min([x max(xCalib([3 4]))]); y = min([y max(yCalib([1 4]))]); y = max([y min(yCalib([2 3]))]); % Determine spatial transformation from the unit square calibration points. tform = cp2tform([xCalib(:) yCalib(:)], [0 0; 0 1; 1 1; 1 0], 'projective'); xyScreen = tformfwd([x, y], tform); xScreen = xyScreen(1); yScreen = xyScreen(2); % Ensure the new coordinates remain within the unit square. xScreen = min([xScreen 1]); xScreen = max([xScreen 0]); yScreen = min([yScreen 1]); yScreen = max([yScreen 0]); % Store the new MATLAB axis coordinates. laser.x = [laser.x(:); xScreen]; laser.y = [laser.y(:); yScreen]; end end % Plot the tracked laser positions. laserFig.Position = smallFigPos; plot(laser.x, laser.y, 'r*');
![]()
% Close the laser figure. close(laserFig); % Stop the acquisition, remove the object from memory, % and clear the variable. stop(vid) delete(vid) clear vid