how to press button continuously?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Mohammad Sami
le 6 Sep 2020
Modifié(e) : Mohammad Sami
le 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
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!