Why running same code on the same image gives different result every time?

Hi, I am running a watershed segmentation on one image, but I found it gave me different result every time when I ran the same code on the same image. Can anyone help me? Thank you.

Respuestas (1)

Whenever this happens, it means that some piece of the algorithm involved probably required starting values, usually for a search of some kind. In that case, a random starting point is probably generated, ergo you will get potentially a slightly different result based on where the search originated. Not all such searches will always end in the same solution, if multiple solutions exist. Or the solution might be subtly different, but still within tolerances that were deemed acceptable.
While I don't claim to have looked up the specifics of the algorithm involved, I have no doubt that a varying result is due to a random start somewhere inside that algorithm.

5 comentarios

Thank you for your answer. Would you please have a look at my code if you don't mind? I am so blind at this problem, haven't found a starting point.If you could have a look, that would be much appreciated.
clc;clear all;close all; rand('seed',0) afm = imread('A.tif');
figure, imshow(afm), title('Surface Image');
se = strel('disk', 15); Itop = imtophat(afm, se); Ibot = imbothat(afm, se); figure, imshow(Itop, []), title('top-hat image');
figure, imshow(Ibot, []), title('bottom-hat image');
I0 = imsubtract(imadd(Itop, afm), Ibot); figure, imshow(I0), title('original + top-hat - bottom-hat');
J = imnoise(I0,'gaussian',0,0.025);
I = wiener2(J,[5 6]);
hy = fspecial('sobel'); hx = hy'; Iy = imfilter(double(I), hy, 'replicate'); Ix = imfilter(double(I), hx, 'replicate'); gradmag = sqrt(Ix.^2 + Iy.^2); figure imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
se = strel('disk',10); Io = imopen(I, se); figure imshow(Io), title('Opening (Io)')
Ie = imerode(I, se, 5); Iobr = imreconstruct(Ie, I); figure imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se); figure imshow(Ioc), title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se); Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr); figure imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
fgm = imregionalmax(Iobrcbr); figure imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)')
I2 = I; I2(fgm) = 255; figure imshow(I2), title('Regional maxima superimposed on original image (I2)')
se2 = strel(ones(5,5)); fgm2 = imclose(fgm, se2); fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20); I3 = I; I3(fgm4) = 255; figure imshow(I3) title('Modified regional maxima superimposed on original image (fgm4)')
bw = Iobrcbr; figure imshow(bw), title('Thresholded opening-closing by reconstruction (bw)')
D = bwdist(bw); DL = watershed(D); bgm = DL == 0; figure imshow(bgm), title('Watershed ridge lines (bgm)')
gradmag2 = imimposemin(gradmag, bgm | fgm4);
L = watershed(gradmag2);
I4 = I; I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255; figure imshow(I4) title('Markers and object boundaries superimposed on original image (I4)')
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle'); figure imshow(Lrgb) title('Colored watershed label matrix (Lrgb)')
figure imshow(I) hold on himage = imshow(Lrgb); himage.AlphaData = 0.3; title('Lrgb superimposed transparently on original image')
cc = L;
props = regionprops(cc, 'Area', 'Perimeter'); allAreas = [props.Area]; [max,index] = max(allAreas); props(index) = [];
allAreas = [props.Area]; allPerimeters = [props.Perimeter]; circularities = (4 * pi * allAreas) / allPerimeters .^2;
figure histogram(allAreas) title('Histogram of Aggregate Area');
stats = regionprops('table',cc,'EquivDiameter', 'Area', 'Extrema','Perimeter', 'Eccentricity','Centroid', 'MajorAxisLength','MinorAxisLength', 'Solidity') T = stats; filename = 'A.xlsx'; writetable(T,filename,'Sheet',3,'Range','A1')
Jan
Jan el 30 de Ag. de 2017
Editada: Jan el 30 de Ag. de 2017
@Tian Tian: Did you see the "{} Code" button? Select the code and press it. Afterwards your code is much easier to read and this is a good idea for posting in the forum.
The solution is easy:
J = imnoise(I0,'gaussian',0,0.025);
You add random noise to the image. Of course this can change the results randomly.
@Tian Tian - Almost at the very first line I see random noise involved. So what is surprising about a variable result?
Adam
Adam el 30 de Ag. de 2017
Editada: Adam el 30 de Ag. de 2017
rand('seed',0)
is not a syntax I can see documented for my Matlab (R2017a). I assume it is old syntax prior to the 'rng' function. However, it does appear to cause random numbers to be produced from the same seed each time. What happens after that though I have no idea because the code is unreadable.

Iniciar sesión para comentar.

Preguntada:

el 30 de Ag. de 2017

Comentada:

el 30 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by