Different output for same code in two different computers
8 views (last 30 days)
Show older comments
Hello,
I am using the following MATLAB code:
a=im2uint8(a);
agray=im2gray(a);
figure;tiledlayout('flow','Padding','tight','TileSpacing','tight')
aadjust=imadjust(imsharpen(agray,'Radius',10,'Amount',2));
nexttile;imshow(aadjust); axis on; title("Adjusted Image")
[Rows Columns] = size(aadjust);
bwout = imbinarize(aadjust);
bwout(1:40,:)= 0;
tempend=find(agray(:,floor(Columns/2))==0);
tempend(tempend<floor(Rows/2))=[];
tempend=min(tempend);
bwout(tempend-20:Rows,:)= 0;
bwout=bwareaopen(bwout,10000);
nexttile;imshow(bwout);axis on; title("Image binarized")
where "a" is an input image I provide to MATLAB. The problem is that I am using the exact same code in another computer with the exact same image but I am getting a slightly different output image "bwout", and both computers are using MATALAB R2022b with Windows.
Is there any explanation for why is this happenning?
I attach the two images I'm getting in both computers, you'll notice slight differences in some pixels. After further processing those small differences become a huge difference so I need to have the same output in both computers. Any idea why is this happenning??
![Image A](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1365429/Image%20A.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1365434/image.png)
7 Comments
DGM
on 25 Apr 2023
Edited: DGM
on 25 Apr 2023
It's not clear where the difference is initially occurring, so I'm focusing on the observable difference and working backwards:
% this is the image returned from imsharpen()
imwrite(asharp(720:790,220:320),'asharp.png')
% this is the image returned from imadjust()
imwrite(aadjust(720:790,220:320),'aadjust.png')
% this is the image returned from imbinarize() prior to any edits
imwrite(bwout0(720:790,220:320),'bwout0.png')
% this is the final binary image after the call to bwareaopen()
imwrite(bwout(720:790,220:320),'bwout.png')
The resulting images are attached.
FWIW, I'm using R2019b, Linux, AMD. My results are apparently the same as your second image.
Joss Knight
on 25 Apr 2023
I'm not saying there definitely is no bug, but different computers do give different answers, this is just the way floating point computations work. If your processors have different numbers of cores or different vector processing architectures then the way they parallelize your code will lead to order-of-computation related differences (or sometimes completely different algorithms will be used). This can be especially noticeable in image processing code where a mask is being computed based on a threshold.
You might get your two MATLABs to behave the same by calling maxNumCompThreads(1) on both before running your code.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!