Changing Values in Function

14 次查看(过去 30 天)
Hussam Ibrahim
Hussam Ibrahim 2016-7-19
Dear Community,
I have attached below a function that is supposed to accept three inputs, and return two outputs. I am supposed to change the value of outputs. I have attached this function to my matlab script, but when I run the program. I get the following error:
Output argument "lowThreshold" (and maybe others) not assigned during call to "thresh".
Error in UsingThisThreshold (line 71)
[lowThreshold, highThreshold] = thresh(grayLevels, pixelCount, imageToThreshold);
function [lowThreshold, highThreshold] = thresh(grayLevels, pixelCount, imageToThreshold)
% create the figure
fontSize = 20;
figure
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen
subplot(1,2,1)
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
title('Histogram of Original Integer Image', 'FontSize', fontSize);
xlim([0 max(grayLevels(:))]); % Scale x axis manually.
grid on;
subplot(1,2,2)
imshow(imageToThreshold)
title('Original Grayscale Image After Thresholding', 'FontSize', fontSize);
% create the edit boxes
%Edit boxes where the user inserts or changes data
e0 = uicontrol('Style','edit','Position',[150 80 80 20],'String','15','Callback',@updateTxtBox);
e1 = uicontrol('Style','edit','Position',[150 55 80 20],'String','500','Callback',@updateTxtBox);
[row,col] = size(imageToThreshold);
% define the callback
function updateTxtBox(object,eventdata)
lowThreshold = str2num(get(e0,'String'));
highThreshold = str2num(get(e1,'String'));
for i=1:row
for j=1:col
if imageToThreshold(i,j) < lowThreshold
imageToThreshold(i,j) = 0;
else if imageToThreshold(i,j) > highThreshold
imageToThreshold(i,j) = 0;
end
end
end
end
subplot(1,2,1)
hold on;
yAxisLimits = ylim();
line([lowThreshold, lowThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
line([highThreshold, highThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
caption = sprintf(' Low Threshold = %.1f', get(e0,'String'));
text(lowThreshold, 0.7*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
caption = sprintf(' High Threshold = %.1f', get(e1,'String'));
text(highThreshold, 0.9*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
end
end

回答(2 个)

Thorsten
Thorsten 2016-7-19
It may help to define the variables outside to the local function
% create the figure
lowThreshold = 0; % assign dummy values
highTreshold = 0;
fontSize = 20;

Star Strider
Star Strider 2016-7-19
It seems that ‘lowThreshold’ and ‘highThreshold’ are defined inside the ‘updateTxtBox’ function, but never returned from it. (While nested functions have access to variables in the function they are nested in, the reverse is not true. You have to return them as with any other function.)
I would assign them outside (before) ‘updateTxtBox’ and then, just to be safe, pass them as arguments to it.
  4 个评论
Hussam Ibrahim
Hussam Ibrahim 2016-7-19
Ok, I think this is working. I just have one problem. The dialog inputs are only showing once. I need this to be set up like the following workflow:
1- User enters values 2- The result shows, if he likes then these values will be passed on. if he doesn't like these values, he can input other values to view.
This is how my function is setup right now: function [lowThreshold, highThreshold] = thresh(grayLevels, pixelCount, imageToThreshold)
% create the figure
lowThreshold = 15; % assign dummy values
highThreshold = 5000;
fontSize = 10;
figure
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen
subplot(1,2,1)
imshow(imageToThreshold)
title('Original Grayscale Image After Thresholding', 'FontSize', fontSize);
subplot(1,2,2)
bar(grayLevels, pixelCount, 'BarWidth', 1, 'FaceColor', 'b');
title('Histogram of Original Integer Image', 'FontSize', fontSize);
xlim([0 max(grayLevels(:))]); % Scale x axis manually.
grid on;
% create the edit boxes
%Edit boxes where the user inserts or changes data
%e0 = uicontrol('Style','edit','Position',[150 105 80 20],'String','15','Callback', @updateTxtBox); %Default value, but user can change it.
% e0 = uicontrol('Style','edit','Position',[150 80 80 20],'Callback',@updateTxtBox);
% e1 = uicontrol('Style','edit','Position',[150 55 80 20],'Callback',@updateTxtBox);
lowThreshc = inputdlg('Enter your minimum Threshold value: ');
highThreshc = inputdlg('Enter your maximum Threshold value: ');
lowThreshold = str2double(lowThreshc{:});
highThreshold = str2double(highThreshc{:});
[row,col] = size(imageToThreshold);
%e3 = uicontrol('Style','edit','Position',[150 20 80 20],'Callback',@updateTxtBox);
%Edit box that needs to be updated only when the user inputs data in the previous edit boxes.
%e4 = uicontrol('Style','edit','Position',[285 140 50 20],'Enable','off');
% define the callback
function updateTxtBox(object,eventdata)
for i=1:row
for j=1:col
if imageToThreshold(i,j) < lowThreshold
imageToThreshold(i,j) = 0;
else if imageToThreshold(i,j) > highThreshold
imageToThreshold(i,j) = 0;
end
end
end
end
hold on;
yAxisLimits = ylim();
line([lowThreshold, lowThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
line([highThreshold, highThreshold], yAxisLimits, 'Color', 'r', 'LineWidth', 2);
caption = sprintf(' Low Threshold = %.1f', get(e0,'String'));
text(lowThreshold, 0.7*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
caption = sprintf(' High Threshold = %.1f', get(e1,'String'));
text(highThreshold, 0.9*yAxisLimits(2), caption, 'Color', 'r', 'FontWeight', 'Bold', 'FontSize', 15);
hold off
figure
imshow(imageToThreshold, []);
title('Original GrayScale Image after Applying the Threshold', 'FontSize', fontSize);
end % updateTxtBox
end % myGui
Star Strider
Star Strider 2016-7-19
With inputdlg, you would have to put the inputdlg calls in a loop. The function has the ability to display default values, so these could initially be set to zero, and subsequently be set to the previous values entered. That is how I would do it, anyway.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by