how to press button continuously?
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
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
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!