How to use multiple user input variable?

Hello everyone,
I want you to help me how to solve this appeared error when i run this code.
First, i want to use video capture from web.
2. And then i want to ask: Do you want to save image from webcam?
3. I want to input dialog box for input variable (ImageName, Num_images, Start_num_Im).
4. And the i want to write this capture images to database with this input variables.
Please help me how to solve appeared error.
Thank you very much.
for my sample code:
clear all
close all
clc
% Create the webcam object.
cam = webcam(1, 'Resolution','640x480');
preview(cam);
count = 0;
promptMessage = sprintf('Do you want to capture frame from webcam?');
button = questdlg(promptMessage, ...
'Save individual frames?',...
'Yes', 'No', 'No');
hold on;
%compare the string
if strcmp (button, 'Yes')
x = inputdlg({'Name','Number_of_images','start_number'},...
'Please enter the Imageinfo', [1 20; 1 20; 1 20]);
videoFrame = snapshot(cam);
figure, imshow (videoFrame);
%this is where we will be save face image
baseDir = 'C:\Program Files\MATLAB\R2018a\bin\testimages\' + Name;
% Finally, create the folder if it doesn't exist already.
if ~exist(baseDir, 'dir')
mkdir(baseDir);
end
%baseName = image
baseName = join(baseDir + num2str(start_number) + '.jpg');
%check exit file or not!
while Num_images < Number_of_images
Num_images = count + 1;
baseName = join(baseDir + num2str(start_number) + '.jpg');
Iresize = imresize (videoFrame, [500 500]);
imwrite (Iresize, baseName);
%figure();
imshow (Iresize);
title ('Capture image');
end
elseif strcmp (button, 'No')
disp ('Ok! close all');
closePreview(cam);
clear cam;
end

댓글 수: 1

Rik
Rik 2020년 10월 1일
  • Replace clear all with clear or clearvars. Or even better: use functions to keep your workspace clean. Don't ignore the warning mlint is giving you.
  • Don't use close all. Maybe your user has useful GUIs or figures open. You aren't shutting down Matlab or the computer at the end of the code either. You should be using explicit handles to a figure so you can close that figure at the end of your code.
  • You don't print anything to the command window, so why are you calling clc?
You didn't mention which line is triggering an error, and what the full error message is.

댓글을 달려면 로그인하십시오.

답변 (1개)

Subhadeep Koley
Subhadeep Koley 2020년 10월 1일
편집: Subhadeep Koley 2020년 10월 1일

1 개 추천

Alex, the below code might help! Have a look.
% Create the webcam object.
cam = webcam(1, 'Resolution', '640x480');
preview(cam);
promptMessage = sprintf('Do you want to capture frame from webcam?');
button = questdlg(promptMessage, 'Save individual frames?', 'Yes', 'No',...
'No');
% Compare the string
if strcmp (button, 'Yes')
x = inputdlg({'Name', 'Number_of_images', 'Start_number'},...
'Please enter the image info', [1 20; 1 20; 1 20]);
name = x{1};
numberOfImages = str2double(x{2});
startNumber = str2double(x{3});
% Paste your dir path here
baseDir = 'C:\testImages\';
% Finally, create the folder if it doesn't exists already
if ~exist(baseDir, 'dir')
mkdir(baseDir);
end
figHan = figure;
axHan = axes(figHan);
for idx = 1:numberOfImages
videoFrame = snapshot(cam);
imshow(videoFrame, 'Parent', axHan)
title(['Frame number: ', num2str(idx)])
videoFrame = imresize(videoFrame, [500, 500]);
baseName = [baseDir, name, num2str(startNumber), '.jpg'];
imwrite (videoFrame, baseName);
startNumber = startNumber + 1;
end
clc
disp ('Done!')
closePreview(cam)
clear cam
elseif strcmp (button, 'No')
disp ('Ok! Closed all');
closePreview(cam)
clear cam
end

댓글 수: 4

Rik
Rik 2020년 10월 1일
This code is closing all other figures, even if the user chooses not to capture the frames. It also opens a large amount of figures (numberOfImages), instead of updating the CData property of the image object. This will result in slow code that you can't leave in the background because of the constant poping up of figures.
@Rik Thanks for the suggestions! The code is not by no means an optimized one. I provided that code snipped just as an example. I've edited the answer to incorporate the suggestions.
Alex Zai
Alex Zai 2020년 10월 2일
편집: Alex Zai 2020년 10월 2일
Great! That's help to me. Thanks for your answers.
I have another question, pls. In this code, if input value for x isempty (user din't entered nothing ), can i use this?:
if isempty(x)
disp ('You did not entered the input values');
closePreview(cam)
clear cam
end
I added after input dialog bo and tried. But it still running loop and appered error. How can i fix this pls?
Have a great day!
Rik
Rik 2020년 10월 2일
If you have this as a function you could use return at the end of the if section. Unfortunately you are now forced to put the rest of your loop in the else section.
Another strategy would be to put the actual code in a try part, and put this in the catch block. Then you can trigger errors in places you need to exit the code. That way you don't need to copy and paste that code many times.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2020년 10월 1일

댓글:

Rik
2020년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by