Segmenting brain images (Dicom format) using region-growing algorithm
I don't have the Image Processing Toolbox, so I can't test your code, but I'll go through what I can.

Never do diff = 10; diff is a useful builtin function in Matlab, so using it as a variable name will cause the function to be useless. The same goes with max, sum, size and so on.

value1 = sum(sum(J &gt; 0)); is a bit faster than value1 = sum(J(:)&gt;0);, so with regards to performance you did the right thing. In my opinion however, sum(J(:)&gt;0) is a bit cleaner, and it can also be scaled to more dimensions.

Instead of elseif N &lt;= 10 you can simply do else, as they mean the same thing in this context.

If it takes 4-5 iterations pretty consistently, then maybe you can increase the initial value of th to e.g. th = 40. It will result in fewer iterations for most images, so a faster algorithm. You might benefit from guessing a good value, run the algorithm, if you don't hit convergence, jump to a lower value. This will perform better for most of the images, but will be slower for some. In total, it's likely faster.