Unable to do subtraction of images in GUI

1 view (last 30 days)
Hi,
I have built a matlab GUi where I load two separate sets of images in two different axes. I want to subtract each of the both sets of images from each other and display the result in another image axes(axes3). I would browse through each set of images from the first two image axes using a image slider. Their corresponding subtraction result would be displayed in the third axes. However , I am getting an error message.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value1=handles.value1;
orig_imgs1 = handles.InputImage1;
output_img1=handles.InputImage2;
handles.dblSubtractedImage = {};
[rowsorig_imgs1 colsorig_imgs1 numberOfColorChannelsorig_imgs1]=size(orig_imgs1);
[rowsoutput_img1 colsoutput_img1 numberOfColorChannelsoutput_img1]=size(output_img1);
if rowsoutput_img1 ~= rowsorig_imgs1 || colsorig_imgs1 ~= colsoutput_img1
output_img1 = imresize(output_img1, [rowsorig_imgs1 colsorig_imgs1]);
end
dblSubtractedImage = (orig_imgs1{value1}) - (output_img1{value1});
imshow(dblSubtractedImage,'parent',handles.axes3);
handles.dblSubtractedImage{end+1}= dblSubtractedImage;
guidata(hObject, handles);
end
I get the following error message:
Error using imresize>parsePreMethodArgs (line 379)
Invalid input syntax; input image missing from argument list.
Error in imresize>parseInputs (line 273)
parsePreMethodArgs(varargin, method_arg_idx, first_param_string_idx);
Error in imresize (line 152)
params = parseInputs(args{:});
Error in ng>pushbutton3_Callback (line 226)
output_img1 = imresize(output_img1, [rowsorig_imgs1 colsorig_imgs1]);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ng (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ng('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

Accepted Answer

Image Analyst
Image Analyst on 12 Jun 2021
What is this????
InputImage2 = imresize(InputImage2(value1), [rowsInputImage1 colsInputImage1]);
You're saying the image is InputImage2(value1). So what is value1? Is it an integer or a vector? So if value1 is an integer then you're basically using it as the linear index into the InputImage2 image, which gives you only a single pixel. Then you blow up this pixel to a whole image with imresize - so now you have a totally uniform image. Now you subtract that from InputImage1 and it will just look like a dark version of InputImage1. However you used [] in imshow() to scale the image to fit the dynamic range so of course it will look identical to InputImage1.
Try
InputImage2 = imresize(InputImage2, [rowsInputImage1 colsInputImage1]);
and when you subtract, cast the images to double or else negative numbers will get clipped.
dblSubtractedImage = double(InputImage1) - double(InputImage2);
or you can use imabsdiff(), which essentially flips negative numbers positive.
dblSubtractedImage = imabsdiff(InputImage1, InputImage2);
  12 Comments
Warid Islam
Warid Islam on 14 Jun 2021
That really helped a lot. I have one last query. I tried to save the image displayed in image axes 3 everytime when I browse through the slider. I used a SAVE button to do the operation. However, I am getting an error message.
function pushbutton4_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
value1=handles.value1;
dblSubtractedImage=handles.dblSubtractedImage;
startingFolder = 'D:\regionGrowing_MLT'; % Or "pwd" or wherever you want.
if ~isfolder(startingFolder)
startingFolder = pwd;
end
defaultFileName = fullfile(startingFolder, '*.jpg');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
% If they entered an extension, throw it away because (for some reason)
% we want to force it to be JPG format, not whatever other format they may have entered.
[~, baseFileNameNoExt, ext] = fileparts(baseFileName);
fullFileName = fullfile(folder, [baseFileNameNoExt, '.jpg']);
% Now save the JPG image.
imwrite(dblSubtractedImage{value1}, fullFileName);
guidata(hObject, handles);
end
I get the following error message:
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ng('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Index exceeds the number of array elements (1).
Error in ng>pushbutton4_Callback (line 280)
imwrite(dblSubtractedImage{value1}, fullFileName);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ng (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ng('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Warid Islam
Warid Islam on 14 Jun 2021
I have figured out the solution for the above problem. Thank you very much for your suggestions. It was really helpful.

Sign in to comment.

More Answers (1)

ytzhak goussha
ytzhak goussha on 9 Jun 2021
I think that the problem is that you are trying to resize an image in a cell array
value1=handles.value1; (this is the index of the image in the cell array i geuss)
orig_imgs1 = handles.InputImage1; (this is a cell array of images)
output_img1=handles.InputImage2; (this is a cell array of images)
handles.dblSubtractedImage = {};
[rowsorig_imgs1 colsorig_imgs1 numberOfColorChannelsorig_imgs1]=size(orig_imgs1);
[rowsoutput_img1 colsoutput_img1 numberOfColorChannelsoutput_img1]=size(output_img1);
if rowsoutput_img1 ~= rowsorig_imgs1 || colsorig_imgs1 ~= colsoutput_img1
output_img1 = imresize(output_img1{value1}, [rowsorig_imgs1 colsorig_imgs1]); (here you must specify which image you want to resize)
end
dblSubtractedImage = (orig_imgs1{value1}) - (output_img1{value1});
imshow(dblSubtractedImage,'parent',handles.axes3);
handles.dblSubtractedImage{end+1}= dblSubtractedImage;
guidata(hObject, handles);
end
  7 Comments
Warid Islam
Warid Islam on 12 Jun 2021
Hi Walter,
I tried but no image is being displayed.
Warid Islam
Warid Islam on 12 Jun 2021
I managed to solve the issue as I am getting a result in the 3rd image axes.
value1=handles.value1;
InputImage1 = handles.InputImage1{value1};
InputImage2=handles.InputImage2{value1};
InputImage2=imresize(InputImage2,0.5);
handles.dblSubtractedImage = {};
[rowsInputImage1 colsInputImage1 numberOfColorChannelsInputImage1]=size(InputImage1);
[rowsInputImage2 colsInputImage2 numberOfColorChannelsInputImage2]=size(InputImage2);
if rowsInputImage2 ~= rowsInputImage1 || colsInputImage1 ~= colsInputImage2
InputImage2 = imresize(InputImage2(value1), [rowsInputImage1 colsInputImage1]);
end
dblSubtractedImage = (InputImage1) - (InputImage2);
imshow(dblSubtractedImage,[],'parent',handles.axes3);
handles.dblSubtractedImage{end+1}= dblSubtractedImage;
guidata(hObject, handles);
end
However, I think the subtraction has not worked properly, as the result in axes 3 is the same as axes 1. Any suggestions would be appreciated.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!