필터 지우기
필터 지우기

too many input arguments in my code

조회 수: 21 (최근 30일)
Mohammad Mohammad
Mohammad Mohammad 2024년 1월 6일
댓글: Voss 2024년 1월 10일
I wrote this code to create a gui to process images but I get this this error
Error using createGUI>processImage
Too many input arguments.
how can I fix it
This is part of the code where I get the error
Error while evaluating UIControl Callback.
createGUI
% Create the main GUI function
function createGUI()
% Create the main figure
fig = figure('Name', 'Image Processing GUI', 'NumberTitle', 'off', 'Position', [200, 200, 800, 600]);
annotation('textbox', [0.3, 0.95, 0.5, 0.05], 'String', 'Image Processing Project', 'EdgeColor', 'none', 'HorizontalAlignment', 'center', 'FontSize', 14, 'FontWeight', 'bold');
%fig.WindowState = 'maximized';
% Create axes for displaying images
axes1 = axes('Parent', fig, 'Position', [0.15, 0.4, 0.3, 0.5]);
axes2 = axes('Parent', fig, 'Position', [0.55, 0.4, 0.3, 0.5]);
% Create buttons
filter1 = {'Standard Average', 'Weighted Average'};
for i = 1:length(filter1)
uicontrol('Style', 'pushbutton', 'String', filter1{i}, 'Units', 'normalized', 'Position', [0.15, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, axes1, axes2, filter1{i}});
end
filter2 = { 'Laplacian', 'Median'};
for i = 1:length(filter2)
uicontrol('Style', 'pushbutton', 'String', filter2{i}, 'Units', 'normalized', 'Position', [0.35, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, axes1, axes2, filter2{i}});
end
filter3 = { 'Fourier Transform', 'Sobel'};
for i = 1:length(filter3)
uicontrol('Style', 'pushbutton', 'String', filter3{i}, 'Units', 'normalized', 'Position', [0.55, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, axes1, axes2, filter3{i}});
end
filter4 = { 'Gaussian Low Pass', 'Ideal High Pass'};
for i = 1:length(filter4)
uicontrol('Style', 'pushbutton', 'String', filter4{i}, 'Units', 'normalized', 'Position', [0.75, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, axes1, axes2, filter4{i}});
end
% Add a button to upload an image
uploadButton = uicontrol('Style', 'pushbutton', 'String', 'Upload Image', 'Units', 'normalized', 'Position', [0.02, 0.63, 0.1, 0.05], 'Callback', {@uploadImage, axes1, axes2});
% Create threshold entry field
thresholdField = uicontrol('Style', 'edit', 'Units', 'normalized', 'Position', [0.25, 0.3, 0.1, 0.05], 'String', '');
% Create bit plane entry field
bitPlaneField = uicontrol('Style', 'edit', 'Units', 'normalized', 'Position', [0.75, 0.3, 0.1, 0.05], 'String', '');
% Create labels for entry fields
uicontrol('Style', 'text', 'Units', 'normalized', 'Position', [0.03, 0.3, 0.2, 0.05], 'String', 'Threshold:');
uicontrol('Style', 'text', 'Units', 'normalized', 'Position', [0.52, 0.3, 0.2, 0.05], 'String', 'Bit Plane:');
end
% Create a function to handle button clicks and image processing
function processImage(originalImage, axes1, axes2, filterType)
switch filterType
case 'Standard Average'
% Apply standard average filter
processedImage = imfilter(originalImage, fspecial('average'));
case 'Weighted Average'
% Apply weighted average filter
weights = [1, 2, 1; 2, 4, 2; 1, 2, 1] / 16;
processedImage = imfilter(originalImage, weights);
case 'Laplacian'
% Apply Laplacian filter
processedImage = imfilter(originalImage, fspecial('laplacian'));
case 'Median'
% Apply Median filter
processedImage = medfilt2(originalImage);
case 'Fourier Transform'
% Apply Fourier Transform filter
fftImage = fft2(originalImage);
processedImage = fftshift(fftImage);
case 'Sobel'
% Apply Sobel filter
sobelFilter = fspecial('sobel');
processedImage = imfilter(originalImage, sobelFilter);
case 'Gaussian Low Pass'
% Apply Gaussian Low Pass filter
processedImage = imgaussfilt(originalImage, 2); % You can adjust the standard deviation (2 in this case)
case 'Ideal High Pass'
% Apply Ideal High Pass filter
[m, n] = size(originalImage);
u = 0:(m - 1);
v = 0:(n - 1);
idx = find(u > m / 2);
u(idx) = u(idx) - m;
idy = find(v > n / 2);
v(idy) = v(idy) - n;
[V, U] = meshgrid(v, u);
D = sqrt(U.^2 + V.^2);
cutoff = 30; % You can adjust the cutoff frequency
H = double(D > cutoff);
processedImage = real(ifft2(ifftshift(fftshift(fft2(originalImage)) .* H)));
otherwise
% Default case
processedImage = originalImage;
end
% Display the original and processed images
axes(axes1);
imshow(originalImage);
title('Original Image');
axes(axes2);
imshow(processedImage);
title('Processed Image');
end
  댓글 수: 6
Mohammad Mohammad
Mohammad Mohammad 2024년 1월 6일
how can I complete it?

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

답변 (1개)

Voss
Voss 2024년 1월 6일
편집: Voss 2024년 1월 10일
The reason for the error "Too many input arguments" while evaluating the callbacks is that MATLAB always provides the first two inputs to a callback function, which are the "source" (the object generating the callback) and "event" (a structure containing information about the callback), and your callback function definitions do not take these two first inputs into account.
To fix this, you'd need to modify the definition of processImage to include two additional inputs at the start. For example, like:
function processImage(~, ~, originalImage, axes1, axes2, filterType)
where the two "~" indicate that the function does not use the first two inputs.
Now that would fix the error you saw, but then you'd run into another problem because there's still a mismatch between how processImage is defined and how it's used as a callback: the function definition includes the variable originalImage, but originalImage is not included in the cell array that assigns the function+arguments to be the uicontrol Callback, e.g.:
'Callback', {@processImage, axes1, axes2, filter1{i}})
% ^ no originalImage argument listed
In fact, as you probably already know, you can't use originalImage as an input at that point because originalImage is not defined until the user selects an image (I presume; I don't have uploadImage, but I can guess what it is intended to do). To get past that, you'd have to think a bit about how to share data between callbacks in a GUI. Here's a page that has some ideas: https://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
Of all the ways to share data among callback functions, I prefer to use nested functions. The idea is that the callbacks are all nested inside another, higher-level parent function (the nesting function), so the callbacks all have access to any variable defined in the parent function.
Modifying your current code to make use of nested callbacks is pretty easy: wrap everything in a simple parent function, initialize in that parent function any variable to be shared by a callback, and remove those shared variables from your callback definitions. For instance, originalImage, axes1, and axes2 no longer need to be passed in as arguments to the callbacks because they are defined in the shared parent function's workspace. I've done those things (and also defined uploadImage) in the code below, which works more-or-less as I suppose you intended.
function ImageProcessingProject()
% the callbacks will need these variables, so they must be defined here
% in the parent function:
originalImage = [];
axes1 = [];
axes2 = [];
% create the GUI
createGUI()
% main GUI function
function createGUI()
% Create the main figure
fig = figure('Name', 'Image Processing GUI', 'NumberTitle', 'off', 'Position', [200, 200, 800, 600]);
annotation('textbox', [0.3, 0.95, 0.5, 0.05], 'String', 'Image Processing Project', 'EdgeColor', 'none', 'HorizontalAlignment', 'center', 'FontSize', 14, 'FontWeight', 'bold');
%fig.WindowState = 'maximized';
% Create axes for displaying images
axes1 = axes('Parent', fig, 'Position', [0.15, 0.4, 0.3, 0.5]);
axes2 = axes('Parent', fig, 'Position', [0.55, 0.4, 0.3, 0.5]);
% Create buttons
filter1 = {'Standard Average', 'Weighted Average'};
for i = 1:length(filter1)
uicontrol('Style', 'pushbutton', 'String', filter1{i}, 'Units', 'normalized', 'Position', [0.15, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, filter1{i}});
end
filter2 = { 'Laplacian', 'Median'};
for i = 1:length(filter2)
uicontrol('Style', 'pushbutton', 'String', filter2{i}, 'Units', 'normalized', 'Position', [0.35, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, filter2{i}});
end
filter3 = { 'Fourier Transform', 'Sobel'};
for i = 1:length(filter3)
uicontrol('Style', 'pushbutton', 'String', filter3{i}, 'Units', 'normalized', 'Position', [0.55, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, filter3{i}});
end
filter4 = { 'Gaussian Low Pass', 'Ideal High Pass'};
for i = 1:length(filter4)
uicontrol('Style', 'pushbutton', 'String', filter4{i}, 'Units', 'normalized', 'Position', [0.75, 0.3 - i * 0.1, 0.15, 0.05], 'Callback', {@processImage, filter4{i}});
end
% Add a button to upload an image
uploadButton = uicontrol('Style', 'pushbutton', 'String', 'Upload Image', 'Units', 'normalized', 'Position', [0.02, 0.63, 0.1, 0.05], 'Callback', @uploadImage);
% Create threshold entry field
thresholdField = uicontrol('Style', 'edit', 'Units', 'normalized', 'Position', [0.25, 0.3, 0.1, 0.05], 'String', '');
% Create bit plane entry field
bitPlaneField = uicontrol('Style', 'edit', 'Units', 'normalized', 'Position', [0.75, 0.3, 0.1, 0.05], 'String', '');
% Create labels for entry fields
uicontrol('Style', 'text', 'Units', 'normalized', 'Position', [0.03, 0.3, 0.2, 0.05], 'String', 'Threshold:');
uicontrol('Style', 'text', 'Units', 'normalized', 'Position', [0.52, 0.3, 0.2, 0.05], 'String', 'Bit Plane:');
end
% upload an image file
function uploadImage(~,~)
[fname,pname] = uigetfile('*.*','Select An Image File');
if isnumeric(fname)
% User cancelled
return
end
try
originalImage = imread([pname,fname]);
catch
uiwait(errordlg('Unable to read that image file.','Error','modal'));
return
end
% Display the original image
imshow(originalImage,'Parent',axes1);
title(axes1,'Original Image');
% Reset axes2
cla(axes2,'reset')
end
% handle button clicks and image processing
function processImage(~,~,filterType)
switch filterType
case 'Standard Average'
% Apply standard average filter
processedImage = imfilter(originalImage, fspecial('average'));
case 'Weighted Average'
% Apply weighted average filter
weights = [1, 2, 1; 2, 4, 2; 1, 2, 1] / 16;
processedImage = imfilter(originalImage, weights);
case 'Laplacian'
% Apply Laplacian filter
processedImage = imfilter(originalImage, fspecial('laplacian'));
case 'Median'
% Apply Median filter
processedImage = medfilt2(originalImage);
case 'Fourier Transform'
% Apply Fourier Transform filter
fftImage = fft2(originalImage);
processedImage = fftshift(fftImage);
case 'Sobel'
% Apply Sobel filter
sobelFilter = fspecial('sobel');
processedImage = imfilter(originalImage, sobelFilter);
case 'Gaussian Low Pass'
% Apply Gaussian Low Pass filter
processedImage = imgaussfilt(originalImage, 2); % You can adjust the standard deviation (2 in this case)
case 'Ideal High Pass'
% Apply Ideal High Pass filter
[m, n] = size(originalImage);
u = 0:(m - 1);
v = 0:(n - 1);
idx = find(u > m / 2);
u(idx) = u(idx) - m;
idy = find(v > n / 2);
v(idy) = v(idy) - n;
[V, U] = meshgrid(v, u);
D = sqrt(U.^2 + V.^2);
cutoff = 30; % You can adjust the cutoff frequency
H = double(D > cutoff);
processedImage = real(ifft2(ifftshift(fftshift(fft2(originalImage)) .* H)));
otherwise
% Default case
processedImage = originalImage;
end
% Display the processed image
imshow(processedImage,'Parent',axes2);
title(axes2,'Processed Image');
end
end

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by