how to press button continuously?

2 次查看(过去 30 天)
a.s
a.s 2020-9-5
评论: a.s 2020-9-6
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)

回答(1 个)

Mohammad Sami
Mohammad Sami 2020-9-6
编辑:Mohammad Sami 2020-9-6
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
  1 个评论
a.s
a.s 2020-9-6
Thank you , but I want to push the key s for just once( that works properly until this part).
then by pressing the F key for example 5 times..5 screenshots of the video at the moment of pressing the key and then stop the video by pressing the p key.
And finally show me 6 images that were the result of pressing the keys of s and f.
-------------------------------------------------------------------------------------------------------------------------------------------------
Error in images.internal.imageDisplayValidateParams (line 11)
validateattributes(common_args.CData, {'numeric','logical'},...
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 245)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in m22 (line 91)
imshow(img2)

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by