Image Subtraction

3 views (last 30 days)
Colm
Colm on 13 Dec 2011
I am having trouble subtracting one image from another and returning negative values. Below is my code, any suggestions why no negative values are return for the variable 'frame3':
function pushbutton1_Callback(hObject, eventdata, handles)
prompt = {'Mono:'};
dlg_title = 'Mono:';%user chooses number of bits for camera
num_lines = 1;
def = {'16'};
inputs = str2num(char(inputdlg(prompt, dlg_title, num_lines, def)));
if (inputs == 8)
vid = videoinput('gige', 1, 'Mono8');
elseif (inputs == 10)
vid = videoinput('gige', 1, 'Mono10');
elseif (inputs == 12)
vid = videoinput('gige', 1, 'Mono12');
elseif (inputs == 14)
vid = videoinput('gige', 1, 'Mono14');
else
vid = videoinput('gige', 1, 'Mono16');
end
src = getselectedsource(vid);
vid.FramesPerTrigger = 2;
start(vid);%start acquisition
wait(vid,60)
data = getdata(vid);
frame1 = data(:,:,:,1);
frame2 = data(:,:,:,2);
axes(handles.axes1);
imshow(frame1);
axes(handles.axes2);
imshow(frame2);
frametempt=imabsdiff(frame1, frame2);
axes(handles.axes4);
imshow(frametempt);
I = 1.4142135623730950488016887242097;
frame3 = imdivide(frametempt, I);%qualitive noise calc.
axes(handles.axes3);
imshow(frame3);
Mint = min(frame3);
Maxt = max(abs(frame3));
Min = min(Mint);
Max = max(Maxt);
set(handles.text3, 'String',num2str(Min));
set(handles.text4, 'String',num2str(Max));
fid5 = fopen('DF.bin', 'w');
fwrite(fid5, frame3, 'int16');
fclose(fid5);
fid6 = fopen('frame1.bin', 'w');
fwrite(fid6, frame1, 'int16');
fclose(fid6);
fid7 = fopen('frame2.bin', 'w');
fwrite(fid7, frame2, 'int16');
fclose(fid7);
fid8 = fopen('frametempt.bin', 'w');
fwrite(fid8, frametempt, 'int16');
fclose(fid8);
axes(handles.axes3);
imshow(samevals);

Accepted Answer

Image Analyst
Image Analyst on 13 Dec 2011
I don't knwo what imabsdiff does (no MATLAB on my current computer) but it sounds like it takes the absolute value. To get negative values, you need to cast your frames to single or double before the subtraction or else it will leave them as uint8 which will clip any values that would otherwise be less than zero.
differenceImage = single(frame1) - single(frame2);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!