sakurarandom
Junior Member level 3
I had try on laser tracking on my green laser with coding as shown below. It works if the laser dot in a dark room. Is there any suggestion on how to improve it? I would like to improve it to scan only for coordinate x from 100 to 260 while coordinate y from 300 to 360. Thanks.
close all%Ckise all figure windows except those created by imtool
%closepreview(vid);
%delete(vid);
clear; %Delete all variables
%close(gcf);
clc %Clear command window
clear
imtool close all %Make sure the workspsace panel in showing
vid=videoinput('winvideo',1,'RGB24_640X480');
vid.FramesPerTrigger = 1;
start(vid);
wait(vid);
preview(vid);
xResolution = 640;
yResolution = 480;
intensityThreshold = 250;
tic
while toc < 15
theSnapshot = getsnapshot(vid);
figure(1);
imshow(theSnapshot);
% reset the corner extremes
leftMost = xResolution;
rightMost = 0;
topMost = yResolution;
bottomMost = 0;
[x y] = size (theSnapshot);
for i = 1: xResolution
for j = 1:yResolution
if theSnapshot(i,j,2) > intensityThreshold
% bright green pixel found
if i < topMost
topMost = i;
end
if i > bottomMost
bottomMost = i;
end
if j < leftMost
leftMost = j;
end
if j > rightMost
rightMost = j;
end
end
end
end
% calculate the midpoints of the area
if leftMost ~= xResolution && rightMost ~= 0
% both leftMost and RightMost have been updated
laserFound = true;
laserX = (leftMost + rightMost)/2
laserY = (topMost + bottomMost)/2
else
% unable to find laser
laserFound = false;
end
% plot laser area
% for ease of viewing, y coordinates are viewed flipped around into
% the cartesian system instead of image coordinate system
if laserFound == true
figure(1);
impixelinfo
hold on;
plot((0: xResolution),laserY*ones(1, xResolution+1),'r');
plot(laserX*ones(1, yResolution+1),(0:yResolution),'r');
hold off;
axis([0 xResolution 0 yResolution]);
drawnow;
end
end
% Stop the acquisition, remove the object from memory,
% and clear the variable.
stop(vid)
delete(vid)
clear vid
close all%Ckise all figure windows except those created by imtool
%closepreview(vid);
%delete(vid);
clear; %Delete all variables
%close(gcf);
clc %Clear command window
clear
imtool close all %Make sure the workspsace panel in showing
vid=videoinput('winvideo',1,'RGB24_640X480');
vid.FramesPerTrigger = 1;
start(vid);
wait(vid);
preview(vid);
xResolution = 640;
yResolution = 480;
intensityThreshold = 250;
tic
while toc < 15
theSnapshot = getsnapshot(vid);
figure(1);
imshow(theSnapshot);
% reset the corner extremes
leftMost = xResolution;
rightMost = 0;
topMost = yResolution;
bottomMost = 0;
[x y] = size (theSnapshot);
for i = 1: xResolution
for j = 1:yResolution
if theSnapshot(i,j,2) > intensityThreshold
% bright green pixel found
if i < topMost
topMost = i;
end
if i > bottomMost
bottomMost = i;
end
if j < leftMost
leftMost = j;
end
if j > rightMost
rightMost = j;
end
end
end
end
% calculate the midpoints of the area
if leftMost ~= xResolution && rightMost ~= 0
% both leftMost and RightMost have been updated
laserFound = true;
laserX = (leftMost + rightMost)/2
laserY = (topMost + bottomMost)/2
else
% unable to find laser
laserFound = false;
end
% plot laser area
% for ease of viewing, y coordinates are viewed flipped around into
% the cartesian system instead of image coordinate system
if laserFound == true
figure(1);
impixelinfo
hold on;
plot((0: xResolution),laserY*ones(1, xResolution+1),'r');
plot(laserX*ones(1, yResolution+1),(0:yResolution),'r');
hold off;
axis([0 xResolution 0 yResolution]);
drawnow;
end
end
% Stop the acquisition, remove the object from memory,
% and clear the variable.
stop(vid)
delete(vid)
clear vid