MATLAB Answers

Unable to do subtraction of images in GUI

1 ビュー (過去 30 日間)
Warid Islam
Warid Islam 2021 年 6 月 9 日
コメント済み: Warid Islam 2021 年 6 月 14 日
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.

採用された回答

Image Analyst
Image Analyst 2021 年 6 月 12 日
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 件のコメント
Warid Islam
Warid Islam 2021 年 6 月 14 日
I have figured out the solution for the above problem. Thank you very much for your suggestions. It was really helpful.

サインインしてコメントする。

その他の回答 (1 件)

ytzhak goussha
ytzhak goussha 2021 年 6 月 9 日
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 件のコメント
Warid Islam
Warid Islam 2021 年 6 月 12 日
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.

サインインしてコメントする。

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by