how to press button continuously?
2 views (last 30 days)
Show older comments
hello!
I want to take a screenshot of the video several times when I press the f key , And when I press the p button, the video stops.
But when I run the program:
it displays a large number of figures that result from separating all the video frames and displaying it as a figure, and each time the f key is pressed, All these figures are repeated again.Thanks for your help!
clc
clear all
close all
a = imaqhwinfo;
vid = videoinput('winvideo',1);
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
start(vid)
while(vid.FramesAcquired<=200)
Key=get(gcf,'CurrentKey');
if Key =='s'
img1 = getsnapshot(vid);
break;
end
data = getsnapshot(vid);
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
imshow(data)
hold on
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
h=0;
while(vid.FramesAcquired<=800)
Key1=get(gcf,'CurrentKey');
if Key1 =='f'
h=h+1;
img2{h} = getsnapshot(vid);
end
if Key1 =='p'
img3 = getsnapshot(vid);
break;
end
data = getsnapshot(vid);
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
diff_im = bwareaopen(diff_im,300);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid');
imshow(data)
hold on
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
stop(vid);
ya=cell(h+2,1);
ya{1}=img1;
for o=2:h+1
ya{o}=img2{o-1};
end
ya{h+2}=img3;
stats_f=cell(2,1);
stats_f=cell2mat(stats_f);
figure;
imshow(img1)
figure
imshow(img2)
0 Comments
Answers (1)
Mohammad Sami
on 6 Sep 2020
Edited: Mohammad Sami
on 6 Sep 2020
If you want to update the figure rather then create new figures, you need to explicitly update the CData property of existing imshow. A minimum example is as follows.
if true
height = 1080;
width = 1920;
% create new figure
figure;
Display1 = imshow(zeros(height,width,3,'uint8'));
Frame = getsnapshot(vid);
Display1.CData = Frame;
end
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!