How to update the variable of a function using a dialogbox?

4 次查看(过去 30 天)
INTRO: I use the script below to track the shift of a pixel between two images:
clear all; close all;
prompt = {'CORRELATION SIZE:'};
dlg_title = 'SHIFTED PIXEL';
def = {'5'};
answer = inputdlg(prompt,dlg_title,[1 20],def);
CORRSIZE = str2num(answer{1,1}); %#ok<ST2NM>
base_points_0 = [56 37];
input_points_0 = [56 37];
imagea = imread('1.bmp');
imageb = imread('2.bmp');
image1 = rgb2gray(imagea);
image2 = rgb2gray(imageb);
d = zeros(1,2,2);
d(:,:,1) = base_points_0;
[input_points, base_points] = cpselect(image2,image1, input_points_0, base_points_0,'Wait', true);
updated_inputs = cpcorr(input_points, base_points, image2, image1);
d(:,:,2) = updated_inputs;
The shift of a pixel between two images is correct if the variable CORRSIZE found in line 76 of cpcorr.m is chosen properly. Therefore, I begin the script with a dialo-gbox to allow to chose the CORRSIZE before the script runs.
PROBLEM: CORRSIZE is updated only in the workspace but not in the function cpcorr.m and consequently I always have to open cpcorr.m from the command line, go to line 76 and change it manually.
I wonder if someone could write me how could I update CPCORR from a dialog-box incorporated in the current script and thus avoid to open cpcorr.m and do all steps manually.
I thank you in advance for your help Emerson

采纳的回答

Ahmed A. Selman
Ahmed A. Selman 2013-4-5
Try the (while) loop with simple criterion, as:
clear all; close all;
prompt = {'CORRELATION SIZE:'};
dlg_title = 'SHIFTED PIXEL';
def = {'5'};
CORRSIZE = 5; % Here. Or any initial value, see below why
while (CORRSIZE); % Here.
answer = inputdlg(prompt,dlg_title,[1 20],def);
CORRSIZE = str2num(answer{1,1}); %#ok<ST2NM>
if CORRSIZE==0; % Here. When you input ZERO it will stop.
break % Here
end % Here. To end the endless loop
base_points_0 = [56 37];
input_points_0 = [56 37];
imagea = imread('1.bmp');
imageb = imread('2.bmp');
image1 = rgb2gray(imagea);
image2 = rgb2gray(imageb);
d = zeros(1,2,2);
d(:,:,1) = base_points_0;
[input_points, base_points] = cpselect(image2,image1, input_points_0, base_points_0,'Wait', true);
updated_inputs = cpcorr(input_points, base_points, image2, image1);
d(:,:,2) = updated_inputs;
end % Here. End of while loop
Please notice that I added the lines ending with ( %Here ). Perhaps it works now as required.
  3 个评论
Ahmed A. Selman
Ahmed A. Selman 2013-4-6
Yes, sorry I didn't fully understand it the first time. In this case you must edit and change the cpcorr.m file.
First please note that this is a Matlab global function so I really don't know if someone can modify it and use it publicly, that's for you to find out :)
As for the required task, do the following (refer to the same code I posted above) :
1. Open cpcorr.m with the editor.
2. In line 1, change
function xyinput = cpcorr(varargin)
to
function xyinput = modf_cpcorr(CORRSIZE,varargin)
and save the function with the new name (modf_cpcorr.m) - I meant to rename the function to a modified cpcorr function, leaving the original one intact.
3. In the modf_cpcorr.m file, line 76, instead of
CORRSIZE = 5;
change it to
%CORRSIZE = 5;
and save and close the function.
4. In the code posted above (my answer), go to line 23 and change
updated_inputs = cpcorr(input_points, base_points, image2, image1);
to this
updated_inputs = modf_cpcorr(CORRSIZE,input_points, base_points, image2, image1);
then save and run.
This should pass the value of the variable CORRSIZE to the function modf_cpcorr from your file while it is running, acquired from the input dialog box, and with initial value (5). Only the value (0) will cause the program to stop (or you can press Ctrl+C to force it to terminate).
Please do not hesitate if you needed further modification, I hope this time I understood the problem and solved it. Regards.
Emerson De Souza
Emerson De Souza 2013-4-6
PROBLEM SOLVED:)!!!
Ahmed, thank you so much for your suggestion, your modification does exactly what I want. I appreciate you took the time to think about it. Wish you a nice weekend.
Emerson

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by